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.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| subc-0.4.0.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| subc-0.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.0 kB
Release files / subc-0.4.0.tar.gz
| Download URL | subc-0.4.0.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c6085495e9db06c33824bda97a80f34387c321c69c1ce3f77f93c006ae4826a3
|
|
BLAKE2b-256 checksum How to use checksums |
07f222222bc7229cca0455d4901b9ca25e7eb230514378c27ab09bc3e2c5b48c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
|
Release files / subc-0.4.0-py3-none-any.whl
| Download URL | subc-0.4.0-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
068445e232600ebcee9792dc5b1abd984233efba4a8b8c2b329728e7bb2e7275
|
|
BLAKE2b-256 checksum How to use checksums |
505a83327f227ce8bf68811baf1429dfda9b0b31cd185804c0d888c5d6e5b1f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1
|