1. Andy Mikhailenko
  2. argh


Andy Mikhailenko  committed aeeb3d3

Fix issue #12: option `-h` is silently ignored if the parser automatically adds -h/--help.

  • Participants
  • Parent commits da4394b
  • Branches default

Comments (0)

Files changed (2)

File argh/assembling.py

View file
        will be merged into inferred ones. If an argument does not conform
        function signature, `ValueError` is raised.
+    .. note::
+       If the parser was created with ``add_help=True`` (which is by default),
+       option name ``-h`` is silently removed from any argument.
     if parser._subparsers:
         raise RuntimeError('Cannot set default command to a parser with '
     command_args = inferred_args or declared_args
     for a_args, a_kwargs in command_args:
+        if parser.add_help and '-h' in a_args:
+            a_args = [x for x in a_args if x != '-h']
         parser.add_argument(*a_args, **a_kwargs)
     if function.__doc__ and not parser.description:

File tests.py

View file
         self.assert_cmd_returns('command-deco-issue12 --fox 3', 'foo 1, fox 3\n')
         self.assert_cmd_fails('command-deco-issue12 -f 3', 'unrecognized')
-    @unittest.expectedFailure
     def test_regression_issue12_help_flag(self):
         """Issue #12: if an argument starts with "h", e.g. "--host",
         ArgumentError is raised because "--help" is always added by argh
         def ddos(host='localhost'):
-            print 'so be it!'
+            return 'so be it, {0}!'.format(host)
-        self.parser = DebugArghParser('PROG')
+        # no help → no conflict
+        self.parser = DebugArghParser('PROG', add_help=False)
+        self.assert_cmd_returns('-h', 'so be it,!\n')
+        # help added → conflict → short name ignored
+        self.parser = DebugArghParser('PROG', add_help=True)
+        self.parser.set_default_command(ddos)
+        self.assert_cmd_fails('-h', '')
     def test_declared_vs_inferred_merging(self):
         """ @arg merges into function signature if @command is applied.