Skip to main content

subc

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.

Metadata

Release files for subc 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for subc 0.8.0
File Size Uploaded Provenance Actions
subc-0.8.0.tar.gz 9.0 kB Without provenance Details for subc-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for subc 0.8.0
File Interpreter ABI Platform Actions
subc-0.8.0-py3-none-any.whl Python 3 none any Details for subc-0.8.0-py3-none-any.whl

Total release size: 17.6 kB

Release files / subc-0.8.0.tar.gz

Download URL subc-0.8.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
15ea63fb00a92cddbf02bc602ef365258f763a0bb6b86ea32a2f1f63b5d19399
BLAKE2b-256 checksum
How to use checksums
c936026e3d60c6a3b590d76ec5030f33de7391628261334b1f12340f79054395
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / subc-0.8.0-py3-none-any.whl

Download URL subc-0.8.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc61db49f634399c17d8cf1c04053d74176a4a91c712394ac8260fb68eed6a64
BLAKE2b-256 checksum
How to use checksums
f05b0dd4df193edd622b7b08cd47aad293e149c99528d81e4046770a157262bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Security for subc 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page