argh icon indicating copy to clipboard operation
argh copied to clipboard

args, kwargs not working?

Open robnagler opened this issue 7 years ago • 3 comments

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.

robnagler avatar Mar 24 '17 19:03 robnagler

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')

halloleo avatar Apr 05 '17 03:04 halloleo

Thanks. What I want is a universal function that checks its own args or passes it on to another function that checks the args.

robnagler avatar Apr 05 '17 15:04 robnagler

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.

robnagler avatar Apr 05 '17 15:04 robnagler

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.

neithere avatar Feb 08 '23 00:02 neithere