I want this functionality:
$ python program.py add Peter 'Peter' was added to the list of names.
I can achieve this with --add instead of add as follows:
import argparse parser = argparse.ArgumentParser() parser.add_argument("--add", help="Add a new name to the list of names", action="store") args = parser.parse_args() if args.add: print "'%s' was added to the list of names." % args.add else: print "Just executing the program baby."
Thus:
$ python program.py --add Peter 'Peter' was added to the list of names.
But when I change --add to add , it is no longer optional, how can I let it be optional but not have these signs -- ? (preferably also using the argparse library)
python command-line-arguments argparse
Bentley4
source share