args parser subcommand

Solutions on MaxInterview for args parser subcommand by the best coders in the world

showing results for - "args parser subcommand"
Giacomo
03 Feb 2016
1>>> # create the top-level parser
2>>> parser = argparse.ArgumentParser(prog='PROG')
3>>> parser.add_argument('--foo', action='store_true', help='foo help')
4>>> subparsers = parser.add_subparsers(help='sub-command help')
5>>>
6>>> # create the parser for the "a" command
7>>> parser_a = subparsers.add_parser('a', help='a help')
8>>> parser_a.add_argument('bar', type=int, help='bar help')
9>>>
10>>> # create the parser for the "b" command
11>>> parser_b = subparsers.add_parser('b', help='b help')
12>>> parser_b.add_argument('--baz', choices='XYZ', help='baz help')
13>>>
14>>> # parse some argument lists
15>>> parser.parse_args(['a', '12'])
16Namespace(bar=12, foo=False)
17>>> parser.parse_args(['--foo', 'b', '--baz', 'Z'])
18Namespace(baz='Z', foo=True)
19
similar questions
queries leading to this page
args parser subcommand