A command subparser utility.


Keywords
command-line, subcommands, subparser, argparse
License
MIT
Install
pip install subcomm==0.3.3

Documentation

Subcomm

https://travis-ci.org/jpmelos/subcomm.svg?branch=master

Subcomm provides a clear API for creating subcommands.

Supports Python: 3.5, 3.6, 3.7

Installation

$ pip install subcomm

Basic usage

Create the command parser:

parser = SubcommParser(description='Command parser.')

When declaring commands, use the @parser.command decorator:

@parser.command(help='Run the command start.')
def start(file):
    return 0
start.parser.add_argument('file')

You can customize the subparser by accessing it as an attribute of the function: start.parser, so you can add arguments to the subcommand or any other parser behavior using the default argparse API. Arguments will be passed to the command handler by the names they would have in parser.parse_args().

You call the handlers by doing:

parser.run()

The value returned by the handler will be returned to the caller. If you are running a script and you want to return something consistent with the return values of the handlers, do:

sys.exit(parser.run())

If the code is in a script called run.py, then you can run it using:

python run.py command

That's the same thing as calling the command function.

If you want a default command, so you can just call python run.py, then you can declare:

@parser.default(help='Help for the subcommand.')
def start(args):
    pass

You can only declare one default command at a time.

Development

Install for development and with dependencies:

$ pip install -e .[dev]

Asserting code quality

Of course we use our own API, so:

$ python dev.py quality

Running tests

$ python dev.py test

This will report code coverage as well.

To run tests with tox, run:

$ tox

Packaging for PyPI distribution

$ python dev.py package

The build will happen if the repository passes the quality tests and is clean: no untracked files are found. If not, you can see what's wrong with git clean -dfx --dry-run and remove or track and commit those files.

Builds will be available at dist, in wheel and source.

Contribute

Pull requests are welcome.