neithere / argh

An argparse wrapper that doesn't make you say "argh" each time you deal with it.
http://argh.rtfd.org
GNU Lesser General Public License v3.0
369 stars 55 forks source link

Issue with underscores in positional arguments since 0.30.0 #208

Closed kaetir closed 10 months ago

kaetir commented 10 months ago

Since 0.30.0 having underscores in positional arguments with @arg decorator breaks argparse

The parameter get considered as an optional kwargs.

Poc with the error

from argh import arg, dispatch_command

@arg("foo_bar", help="fooooo")
def main(foo_bar):
    return foo_bar

if __name__ == "__main__":
    dispatch_command(main)

Error

Traceback (most recent call last):
  File "/tmp/venv/lib/python3.11/site-packages/argh/assembling.py", line 361, in set_default_command
    action = parser.add_argument(
             ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/argparse.py", line 1436, in add_argument
    kwargs = self._get_optional_kwargs(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/argparse.py", line 1571, in _get_optional_kwargs
    raise ValueError(msg % args)
ValueError: invalid option string 'foo-bar': must start with a character '-'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/proof.py", line 8, in <module>
    dispatch_command(main)
  File "/tmp/venv/lib/python3.11/site-packages/argh/dispatching.py", line 401, in dispatch_command
    set_default_command(parser, function)
  File "/tmp/venv/lib/python3.11/site-packages/argh/assembling.py", line 367, in set_default_command
    raise AssemblingError(
argh.exceptions.AssemblingError: main: cannot add 'foo_bar' as foo-bar/foo_bar: invalid option string 'foo-bar': must start with a character '-'

Without the decorator the error does not occur.

neithere commented 10 months ago

Thank you for a reproducible example, @kaetir!

The fix will be shipped in v0.30.3 right away.