I have a program that has many available options. For example a configuration option to change settings.
./app config -h
gives me the help using normal argparse subcommands
now i would like to add another subcommand to the config subcommand called list to list config values
./app config list
additionally that command should accept another option so that i could say
./app config list CATEGORY
only to list the config of one category
my code right now is basically this just with more commands
>>> parser = argparse.ArgumentParser()
>>> subparsers = parser.add_subparsers(title='subcommands',
... description='valid subcommands',
... help='additional help')
>>> subparsers.add_parser('foo')
>>> subparsers.add_parser('bar')
>>> parser.parse_args(['-h'])
usage: [-h] {foo,bar} ...
optional arguments:
-h, --help show this help message and exit
subcommands:
valid subcommands
{foo,bar} additional help
So far I could not find any way to use a subcommand in a subcommand. If this is possible, how? If not, is there any other way to accomplish this goal?
Thanks in Advance
#file: argp.py
import argparse
parser = argparse.ArgumentParser(prog='PROG')
parser_subparsers = parser.add_subparsers()
sub = parser_subparsers.add_parser('sub')
sub_subparsers = sub.add_subparsers()
sub_sub = sub_subparsers.add_parser('sub_sub')
sub_sub_subparsers = sub_sub.add_subparsers()
sub_sub_sub = sub_sub_subparsers.add_parser('sub_sub_sub')
Seems to work.
In [392]: run argp.py
In [393]: parser.parse_args('sub sub_sub sub_sub_sub'.split())
Out[393]: Namespace()
In [400]: sys.version_info
Out[400]: sys.version_info(major=2, minor=7, micro=2, releaselevel='final', serial=0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With