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

Support for boolean flag pairs #141

Open tomasaschan opened 4 years ago

tomasaschan commented 4 years ago

A lot of CLIs with boolean flags have two versions of a flag, e.g. bq --help shows options like --[no]debug_trace, which allows the user to explicitly set either a true or a false value (or leave it to the CLI to use the default, if no flag is specified).

The CLI should error if a user specifies both --nodebug_trace and --debug_trace in the same invocation, and otherwise choose the value for the debug_trace flag as determined by the specified flags.

It would be awesome if argh supported this!

neithere commented 3 years ago

Looks like a good idea for a decorator.

integratebio commented 2 years ago

I set up argh like this:

def function(test=True):
    return 
import argh
parser = argh.ArghParser()
parser.add_commands([function])

The default of function's boolean argument test is set at True. So on the command line, if I would like to disable this boolean argument. How do I do it? If I run

python script.py function 

The default value (True) is taken.

If I run

python script.py function --test False

I get this error: error: unrecognized arguments: --test False.

I think argh should provide (automatic) prefixes for boolean arguments e.g. --no-test or --disable-test if test's default is set at True.

python script.py function --disable-test

As far as I know, such functionality does not exist already in argh. But it would be of common use. For example, argh of javascript has this functionality: https://www.npmjs.com/package/argh. I think it would be really great to have this functionality for python's argh.

neithere commented 1 year ago

I think it would make sense to make this configurable to avoid breaking changes and confusion. Not sure on which level and how exactly such configuration would take place.