adishavit / argh

Argh! A minimalist argument handler.
BSD 3-Clause "New" or "Revised" License
1.33k stars 93 forks source link

how to set boolean arguments as False when default is True? #67

Closed integratebio closed 2 years ago

integratebio commented 3 years ago

I have a function with a boolean argument whose default is set at True. I set up argh like this:

def function(test=True):
    return 

import argh
parser = argh.ArghParser()
parser.add_commands([function])

So on the command line, I would like to disable the boolean argument (test) How do I do this? 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 tried to use prefixes (--no- and --disable-).

python script.py function --disable-test

That also did not work.

adishavit commented 3 years ago

Argh is a C++ command line parsing library. You are showing Python code. I don’t really know if you are asking about argh.

Regardless, a flag’s boolean state indicates whether or not it was specified in the command line. It does not indicate any semantic meaning it has in your program. If “—enable” or “—disable” are boolean flags, then True means they were specified. You may then do what you need with that information.