shithub: aubio

Download patch

ref: f5921b9e238b9c308b199a7e8e2b75becf058e9d
parent: fb1c5e2054eb859d412e1e28cb9c045c8219854c
author: Paul Brossier <piem@piem.org>
date: Sat Oct 27 14:43:43 EDT 2018

[cmd] [py2] workaround for -V to really show version

--- a/python/lib/aubio/cmd.py
+++ b/python/lib/aubio/cmd.py
@@ -499,7 +499,24 @@
 
 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)