Package: argparsetree
Package for creating complex command line argument trees using argparse.Basic UsageYou can create a single command by creating a class extendingargparsetree.BaseCommandand overriding theadd_argumentsandaction_methods. For example:from argparsetree import BaseCommand


class MyCommand(BaseCommand):
    def add_arguments(parser):
        parser.add_arguments('foo', help='Some description of "foo"')

    def action(args):
        print(args.foo)Theparserargument passed toadd_argumentsis anargparse.ArgumentParserobject. Similarly
theargsparameter passed to theactionmethod is aargparse.Namespaceobject generated by
callingparse_argson the generated argument parser.Additionally the description property can be set on the command class, this will be used when building
the help message.If a return value other thanNoneis returned fromactionthis will be used as the return code from therunfunction. If no value is returned (or the value isNone) the value is assumed to be 0.Once you have created your command you can use it by creating a script similar to:#!/usr/bin/env python
import sys

from mycli import MyCommand


if __name__ == '__main__':
    sys.exit(MyCommand().run())Nested UsageChild commands can also be added to a command by specifying thesub_commandsproperty. This is a dictionary
that maps command names to command classes. For example:#!/usr/bin/env python

import sys
from argparsetree import BaseCommand


class CleanFooCommand(BaseCommand):
    description = 'Cleans up the foo object'

    def add_args(self, parser):
        parser.add_argument('target', help='The foo file to clean up')
        parser.add_argument('-y', '--yes', help='Automatic answer yes to prompts', action='store_true')

    def action(self, args):
        # do cleaning
        return 0


class CheckFooCommand(BaseCommand):
    description = 'Checks the integrity of a foo object'

    def add_args(self, parser):
        parser.add_argument('target', help='The foo file to clean up')
        parser.add_argument('-y', '--yes', help='Automatic answer yes to prompts', action='store_true')

    def action(self, args):
        # do cleaning
        return 0


class FooCommand(BaseCommand):
    description = 'Do things with foos'
    sub_commands = {
        'check': CheckFooCommand,
        'clean': CleanFooCommand,
        # more sub commands here
    }


class RootCommand(BaseCommand):
    description = 'My fancy CLI'
    sub_commands = {
        'foo': FooCommand,
        # more sub commands here
    }


if __name__ == '__main__':
    sys.exit(RootCommand().run())Running./example.pywould give the following output:usage: My fancy CLI [-h] {foo} ...

positional arguments:
  {foo}
    foo       Do things with foos

optional arguments:
  -h, --help  show this help message and exitAnd running./example.py foogives:usage: Do things with foos [-h] {check,clean} ...

positional arguments:
  {check,clean}
    check        Checks the integrity of a foo object
    clean        Cleans up the foo object

optional arguments:
  -h, --help     show this help message and exitAn extended help message for each command (and sub-command) can be printed by adding the--helpflag,
So running./example.py foo check--helpgives:usage: My fancy CLI foo check [-h] [-y] target

Checks the integrity of a foo object

positional arguments:
  target      The foo file to clean up

optional arguments:
  -h, --help  show this help message and exit
  -y, --yes   Automatic answer yes to prompts