I am trying to parse a command line like this
cmd {command [COMMAND_OPTS]}
cmd a {1,2}
cmd b
cmd c
Among the commands{a,b,c}, when the command is "a", there might be a COMMAND_OPTS(choices) for "a", say{1,2}, b or c won't have any arguments. And here is what I tried:
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-cmd', nargs = '+', choices = ['a', 'b', 'c'])
# sub_parser = parser.add_subparsers()
# parse_a = sub_parser.add_parser('a')
# parser_a.add_argument("a", default = "1", choices = ["1", "2"])
args = parser.parse_args()
if args.cmd:
print args.cmd
How to parse this with Python Argparse? It seems the subparser is not intended for this problem..
Thanks,