argh icon indicating copy to clipboard operation
argh copied to clipboard

Conversion of underscores to dashes in argument names

Open anntzer opened this issue 10 years ago • 3 comments

Define foo.py as

#!/usr/bin/env python
import argh

@argh.dispatch_command
def main(a_b, *c_d):
    pass

Then

$ ./foo.py -h
usage: foo.py [-h] a-b [c_d [c_d ...]]

positional arguments:
  a-b         None
  c_d         None

optional arguments:
  -h, --help  show this help message and exit

While a minor issue, it seems more consistent if the c_d argument was printed as c-d.

anntzer avatar Nov 13 '14 21:11 anntzer

I would like this, too.

srkunze avatar Oct 08 '15 22:10 srkunze

Similar issue: having an argument whose name starts with an underscore crashes argh (because it tries to pass "--" / "---foo" to argparse). I think this could be instead a mechanism either for specifying arguments not available on the command line, or the underscore could be removed in case the user wants to define an option whose name would collide with a builtin or a keyword.

anntzer avatar Mar 10 '16 20:03 anntzer

Hi @anntzer,

First of all, thank you for your effort.

So, given this example:

@argh.dispatch_command
def main(a_b, _c_d, *e_f):
    return a_b, _c_d, *e_f

...in its current state, argh handles it this way:

$ python issue79.py -h
usage: issue79.py [-h] a_b _c_d [e_f [e_f ...]]

positional arguments:
  a_b         -
  _c_d        -
  e_f         -

optional arguments:
  -h, --help  show this help message and exit

$ python issue79.py hello world extra plus
hello
world
('extra', 'plus')

That is, argh doesn't crash and displays all positional argument names with underscore (which could be replaced with a hyphen, but at least it's consistent).

However, this small example does crash argh the way you described

@argh.dispatch_command
def main(_foo=5):
    return _foo

Traceback:

Traceback (most recent call last):
  File "[...]argh/argh/assembling.py", line 307, in set_default_command
    action = parser.add_argument(*dest_or_opt_strings, **draft)
  File "/usr/lib/python3.5/argparse.py", line 1316, in add_argument
    kwargs = self._get_optional_kwargs(*args, **kwargs)
  File "/usr/lib/python3.5/argparse.py", line 1466, in _get_optional_kwargs
    raise ValueError(msg % option_string)
ValueError: dest= is required for options like '---foo'

I suggest that we keep these as separate issues. I'll probably need more time to check this and your patch.

neithere avatar Aug 01 '16 10:08 neithere