shithub: aubio

Download patch

ref: 9cf6462eb4ac7a99f8f7f8b6215bfa90061eafec
parent: 31a3fd4363da1a4b13ef6550ba07fbba2ec1f3b2
parent: eb6f9d35cea006a49746042df1287552ad69471a
author: Paul Brossier <piem@piem.org>
date: Mon Oct 29 16:30:34 EDT 2018

Merge branch 'fix/py2cmd'

--- 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)
--- a/python/tests/test_aubio_cmd.py
+++ b/python/tests/test_aubio_cmd.py
@@ -19,13 +19,16 @@
 class aubio_cmd_utils(TestCase):
 
     def test_samples2seconds(self):
-        self.assertEqual(aubio.cmd.samples2seconds(3200, 32000), "0.100000\t")
+        self.assertEqual(aubio.cmd.samples2seconds(3200, 32000),
+                "0.100000\t")
 
     def test_samples2milliseconds(self):
-        self.assertEqual(aubio.cmd.samples2milliseconds(3200, 32000), "100.000000\t")
+        self.assertEqual(aubio.cmd.samples2milliseconds(3200, 32000),
+                "100.000000\t")
 
     def test_samples2samples(self):
-        self.assertEqual(aubio.cmd.samples2samples(3200, 32000), "3200\t")
+        self.assertEqual(aubio.cmd.samples2samples(3200, 32000),
+                "3200\t")
 
 if __name__ == '__main__':
     main()