From f5921b9e238b9c308b199a7e8e2b75becf058e9d Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Sat, 27 Oct 2018 18:43:43 +0200 Subject: [PATCH] [cmd] [py2] workaround for -V to really show version --- python/lib/aubio/cmd.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/python/lib/aubio/cmd.py b/python/lib/aubio/cmd.py index 1b386c1a..ae457eb1 100644 --- a/python/lib/aubio/cmd.py +++ b/python/lib/aubio/cmd.py @@ -499,7 +499,24 @@ class process_cut(process_onset): def main(): parser = aubio_parser() - args = parser.parse_args() + if sys.version_info[0] != 3: + # on py2, create a dummy ArgumentParser to workaround the + # optional subcommand issue. See https://bugs.python.org/issue9253 + # This ensures that: + # - version string is shown when only '-V' is passed + # - help is printed if '-V' is passed with any other argument + # - any other argument get forwarded to the real parser + parser_root = argparse.ArgumentParser(add_help=False) + parser_root.add_argument('-V', '--version', help="show version", + action="store_true", dest="show_version") + args, extras = parser_root.parse_known_args() + if args.show_version == False: # no -V, forward to parser + args = parser.parse_args(extras, namespace=args) + elif len(extras) != 0: # -V with other arguments, print help + parser.print_help() + sys.exit(1) + else: # in py3, we can simply use parser directly + args = parser.parse_args() if 'show_version' in args and args.show_version: sys.stdout.write('aubio version ' + aubio.version + '\n') sys.exit(0) -- 2.11.0