argparse presence of switch

Dhimant Patel drp4kri at gmail.com
Tue Jan 12 12:58:59 EST 2021


Its what I searched for on this group.

I want to have an argument's presence only - value is not required.
For example, my program main.py needs to know if "-r" is present when program is invoked. 
So the value command line would be:
(1) python3 main.py -r 
or...
(1) python3 main.py

I tried following:
parser.add_argument('-r','--register', help='Register it')

Since argument started with "-" it should be optional, but absence of it causes following error:

main.py: error: argument -r/--register: expected one argument

I added 
parser.add_argument('-r','--register', default=None,  help='Register it')

No avail - same error.

If I apply -r on the command line, it still expects a value!

Not sure what should I do?

Any help appreciated- 

Thanks!






More information about the Python-list mailing list