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('description of app')

Advanced Use

Intermediate Base Classes

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.

Default Command

When the user does not provide any argument on the command-line, the default action is to raise an Exception which states “you must select a sub-command”. You can provide a default command to run instead, via the default argument to main() (or add_subcommands()). For example:

if __name__ == '__main__':
    MyCmd.main('description', default='help')

The above code will run the help subcommand when no subcommand is specified. Note that in this case, the default sub-command may not receive all of its expected arguments.

Shortest Prefix Aliasing

subc has an optional feature which allows the user to specify a subcommand by the shortest prefix which uniquely identifies the subcommand, or any longer prefix thereof. As an example, imagine a git command with the following sub-commands: clone, checkout, commit, cherry-pick. The shortest prefix aliasing would allow you to run “git clone” by executing git cl, since only “clone” begins with “cl”. You could also execute “git clone” with a longer prefix like git clo. The feature can be enabled by setting shortest_prefix to true in main() or add_subcommands().

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.

Source Distribution

subc-0.7.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

subc-0.7.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file subc-0.7.1.tar.gz.

File metadata

  • Download URL: subc-0.7.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for subc-0.7.1.tar.gz
Algorithm Hash digest
SHA256 e69df8bab63c035f2832ac3f576ff7e474eb827255ebd28b6b0cb1e3ce5a57df
MD5 301d9caf1ad3bb3edf25082a16e4cd3d
BLAKE2b-256 b7f58c8f6a38ab1c8d4522dd13fcafd80406527a19ff873399fa9a1e19423d65

See more details on using hashes here.

File details

Details for the file subc-0.7.1-py3-none-any.whl.

File metadata

  • Download URL: subc-0.7.1-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.8

File hashes

Hashes for subc-0.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7bb18820377d7739501b513aa0108b1c69d3e3fb452f8aad0d13856f6ac3a5a8
MD5 e35feaddd00685fb67f77d6a9971f3c9
BLAKE2b-256 e38b5cf06143918c22b443883f402d28d5b9a3cef366711eea3e2bb5ac48e423

See more details on using hashes here.

Supported by

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