Skip to main content

CLI sub-command library

Project description

This is a tiny library to help you write CLI applications with many sub-commands.

Installation

pip install subc

Use

Create your own command subclass for your application:

class MyCmd(subc.Command):
    pass

Then, write commands in your application which sub-class this:

class HelloWorld(MyCmd):
    name = 'hello-world'
    description = 'say hello'
    def run(self):
        print('hello world')

Finally, use your application-level subclass for creating the argument parser and running your application:

if __name__ == '__main__':
    MyCmd.main()

Advanced Use

You may find yourself wanting to create intermediate subclasses for your application, in order to share common functionality. For example, you might create a class for all commands which handle a single file as an argument:

class FileCmd(MyCmd):
    def add_args(self, parser):
        parser.add_args('file', help='the single file')

You can do that, so long as your intermediate subclasses are not executable. For example, given the following class hierarchy:

MyCmd*
|- FileCmd*
|  |- AppendLineCmd
|  |- RemoveLineCmd
|- DoSomethingElseCmd

The non-leaf commands (marked with an asterisk) will not be included as executable commands. Only leaf classes will be executable.

subc is a very simple library. If you have other advanced uses, read the code.

License

This project is released under the Revised BSD license. See LICENSE.txt for details.

Project details


Download files

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

Files for subc, version 0.3.0
Filename, size File type Python version Upload date Hashes
Filename, size subc-0.3.0-py2.py3-none-any.whl (3.0 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size subc-0.3.0.tar.gz (3.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page