I want to use argeparse module in following way,
from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument('-b', dest='binKey', type=str) args = parser.parse_args() # I will make use of args.binKey option in this space print args.binKey # After that I want to add -d option to the arguments parser.add_argument('-d', dest='dir', type=str) args = parser.parse_args() With this example, I can only provide -b option as shown below in the help text.
$$ python test.py -h usage: test.py [-h] [-b BINKEY] optional arguments: -h, --help show this help message and exit -b BINKEY What I want is I should be able to provide both options while running this code and those options should also by visible in --help option
NOTE: I know that I can provide both options before executing parse_args() once, but that is not the way I want to use my parser.
parse_args()for each argument. Youaddall arguments at once, and then callparse_args()once after you defined all of them... Try going over the Argparse Tutorial to better understand how to use itparse_argsis only called after you added all arguments you needparse_argsmultiple times (though aparse_known_argsmay be safer). But a-hwill be caught by the first call (and exit). You could turn off thehelpwhen setting up theparserand add the '-h' argument yourself later.