argh
argh copied to clipboard
args, kwargs not working?
import argh
def run(*args, **kwargs):
pass
parser = argh.ArghParser()
parser.add_commands([run])
parser.dispatch()
When I run this, I get:
$ python x.py run f -f
usage: x.py [-h] {run} ...
x.py: error: unrecognized arguments: -f
What am I missing? Thanks.
I'm just starting with argh
, but from the docu I conclude, that argh
needs to be told the options either in thye function signature or in a decorator. So if you want to go the **kwarg
route, try decoration your function run
with the following:
@argh.arg('-f', help='the f option')
Thanks. What I want is a universal function that checks its own args or passes it on to another function that checks the args.
Note that it doesn't make sense to have this case fail like it does:
def run(*args, **kwargs):
pass
If the programmer wanted the function to fail for no arguments, this is how she'd write it:
def run():
pass
The case of accepting args and kwargs is special, and the "pythonic" way of thinking about this function is "this function accepts anything". That's what argh
should do in this case, too.
I agree that the most "pythonic" way would be to convert --foo 1
to {'foo': 1}
. However, if it was that simple, we wouldn't need argparse. Unfortunately it's impossible to convert a b --foo c d
to a dictionary without making fragile assumptions. Is --foo
a flag (store_true
) with c
and d
being positional arguments? Is c
the value for --foo
? Are c
and d
values for --foo
(nargs='*'
)? It's too complicated.