From 5be060d1f1e904e157b9fe6d221046652a0291cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Mon, 16 Nov 2015 11:27:27 -0500 Subject: [PATCH 1/2] add a --no-progress flag to forcibly disable progress info --progress isn't a "toggle" anymore, in that it will never disable progress information: always enable it. example: $ borg create ~/test/borg2::test$(date +%s) . ; echo ^shows progress reading files cache processing files ^shows progress $ borg create ~/test/borg2::test$(date +%s) . < /dev/null; echo ^no progress reading files cache processing files ^no progress $ borg create --progress ~/test/borg2::test$(date +%s) . < /dev/null; echo ^progress forced reading files cache processing files ^progress forced $ borg create --no-progress ~/test/borg2::test$(date +%s) . ; echo ^no progress reading files cache processing files ^no progress we introduce a ToggleAction that can be used for other options, but right now is just slapped in there near the code, which isn't that elegant. inspired by: http://stackoverflow.com/questions/11507756/python-argparse-toggle-flags note that this is supported out of the box by click: http://click.pocoo.org/5/options/#boolean-flags fixes #398 --- borg/archiver.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/borg/archiver.py b/borg/archiver.py index 9d65bec7b..c1de05374 100644 --- a/borg/archiver.py +++ b/borg/archiver.py @@ -761,6 +761,19 @@ def build_parser(self, args=None, prog=None): See the output of the "borg help patterns" command for more help on exclude patterns. """) + class ToggleAction(argparse.Action): + """argparse action to handle "toggle" flags easily + + toggle flags are in the form of ``--foo``, ``--no-foo``. + + the ``--no-foo`` argument still needs to be passed to the + ``add_argument()`` call, but it simplifies the ``--no`` + detection. + """ + def __call__(self, parser, ns, values, option): + """set the given flag to true unless ``--no`` is passed""" + setattr(ns, self.dest, not '--no' in option) + subparser = subparsers.add_parser('create', parents=[common_parser], description=self.do_create.__doc__, epilog=create_epilog, @@ -769,8 +782,9 @@ def build_parser(self, args=None, prog=None): subparser.add_argument('-s', '--stats', dest='stats', action='store_true', default=False, help='print statistics for the created archive') - subparser.add_argument('-p', '--progress', dest='progress', const=not sys.stderr.isatty(), - action='store_const', default=sys.stdin.isatty(), + subparser.add_argument('-p', '--progress', '--no-progress', + dest='progress', default=sys.stdin.isatty(), + action=ToggleAction, nargs=0, help="""toggle progress display while creating the archive, showing Original, Compressed and Deduplicated sizes, followed by the Number of files seen and the path being processed, default: %(default)s""") From 9b1ca5c1eba9b353fa63dca582fdc5a7b4785dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Mon, 16 Nov 2015 15:26:50 -0500 Subject: [PATCH 2/2] force --no to be at the start of option --- borg/archiver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/borg/archiver.py b/borg/archiver.py index c1de05374..93de2931a 100644 --- a/borg/archiver.py +++ b/borg/archiver.py @@ -772,7 +772,7 @@ class ToggleAction(argparse.Action): """ def __call__(self, parser, ns, values, option): """set the given flag to true unless ``--no`` is passed""" - setattr(ns, self.dest, not '--no' in option) + setattr(ns, self.dest, option.startswith('--no-')) subparser = subparsers.add_parser('create', parents=[common_parser], description=self.do_create.__doc__,