From 8aa7830f0dc8a92576fbe8041419901036d9c369 Mon Sep 17 00:00:00 2001 From: Dan Helfman Date: Sun, 15 Nov 2020 13:39:15 -0800 Subject: [PATCH] Fix broken "--override" action in Python 3.7 and below. --- borgmatic/commands/arguments.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/borgmatic/commands/arguments.py b/borgmatic/commands/arguments.py index d4019ef..7516036 100644 --- a/borgmatic/commands/arguments.py +++ b/borgmatic/commands/arguments.py @@ -1,5 +1,5 @@ import collections -from argparse import ArgumentParser +from argparse import Action, ArgumentParser from borgmatic.config import collect @@ -102,6 +102,20 @@ def parse_global_arguments(unparsed_arguments, top_level_parser, subparsers): return top_level_parser.parse_args(remaining_arguments) +class Extend_action(Action): + ''' + An argparse action to support Python 3.8's "extend" action in older versions of Python. + ''' + + def __call__(self, parser, namespace, values, option_string=None): + items = getattr(namespace, self.dest, None) + + if items: + items.extend(values) + else: + setattr(namespace, self.dest, list(values)) + + def parse_arguments(*unparsed_arguments): ''' Given command-line arguments with which this script was invoked, parse the arguments and return @@ -111,6 +125,7 @@ def parse_arguments(*unparsed_arguments): unexpanded_config_paths = collect.get_default_config_paths(expand_home=False) global_parser = ArgumentParser(add_help=False) + global_parser.register('action', 'extend', Extend_action) global_group = global_parser.add_argument_group('global arguments') global_group.add_argument(