我想要这个功能:
$ python program.py add Peter
'Peter' was added to the list of names.
我可以用--add而不是add这样来实现这一点:
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."
这样:
$ python program.py --add Peter
'Peter' was added to the list of names.
但是当我改成--add它add不再是可选的时候,我怎么还能让它是可选的却没有那些--标志呢?(最好也使用argparse图书馆)