Skip to main content

A command line helper library for extensible subcommands

Project description

A wrapper for argparse that provides decorator-based subcommand support.

Commands can be defined separately from their actual main functions, enabling faster import times.

Basic Usage

from subparse import CLI

cli = CLI(version='0.0')
cli.add_generic_option('--quiet', action='store_true',
                       help='turn of debugging')

@cli.command(__name__ + ':foo_main')
def foo(parser):
    """
    a short description ending in a period.

    a longer description
    """
    parser.add_argument('--bar', action='store_true',
                        help='turn on bar')

def foo_main(app, args):
    pass

args = cli.parse()
app = MyApp(args)
cli.dispatch(args, context=app)

Lazy Decorators

Commands can be defined lazily and picked up later. This removes ordering restrictions between the commands and the cli object.

# myapp/info.py

from subparse import command

@command('myapp.info:foo_main')
def foo(parser):
    """perform foo"""
cli = CLI()
cli.load_commands('myapp.info')

Entry Points

Commands may also be defined in external modules and loaded via entry points.

from subparse import cli

cli = CLI()
cli.load_commands_from_entry_point('myapp.commands')

An extension application would then define the external module that should be searched for commands. Again this allows the commands themselves to be defined independently of the main functions, improving import speed.

[myapp.commands]
barpkg = barpkg.commands
# barpkg/commands.py

from subparse import command

@command('barpkg.bar')
def bar(parser):
    """perform bar"""
# barpkg/bar.py

def main(app, args):
    pass

0.2 (2013-08-06)

  • Underscores in function names are converted to dashes in their respective subcommand names.

  • Add CLI.add_generic_options API.

  • Add a new help subcommand, allowing for myapp help foo.

  • Allow relative imports in the subcommand specification.

0.1 (2013-08-05)

  • Initial Commits

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

subparse-0.3.tar.gz (6.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page