Package for creating complex command line argument trees using argparse.
Basic Usage
You can create a single command by creating a class extending argparsetree.BaseCommand and overriding the add_arguments and action_methods. For example:
from argparsetree import BaseCommand
class MyCommand(BaseCommand):
def add_arguments(parser):
parser.add_arguments('foo', help='Some description of "foo"')
def action(args):
print(args.foo)
The parser argument passed to add_arguments is an argparse.ArgumentParser object. Similarly the args parameter passed to the action method is a argparse.Namespace object generated by calling parse_args on the generated argument parser.
Additionally the description property can be set on the command class, this will be used when building the help message.
If a return value other than None is returned from action this will be used as the return code from the run function. If no value is returned (or the value is None) the value is assumed to be 0.
Once you have created your command you can use it by creating a script similar to:
#!/usr/bin/env python
import sys
from mycli import MyCommand
if __name__ == '__main__':
sys.exit(MyCommand().run())
Nested Usage
Child commands can also be added to a command by specifying the sub_commands property. This is a dictionary that maps command names to command classes. For example:
#!/usr/bin/env python
import sys
from argparsetree import BaseCommand
class CleanFooCommand(BaseCommand):
description = 'Cleans up the foo object'
def add_args(self, parser):
parser.add_argument('target', help='The foo file to clean up')
parser.add_argument('-y', '--yes', help='Automatic answer yes to prompts', action='store_true')
def action(self, args):
# do cleaning
return 0
class CheckFooCommand(BaseCommand):
description = 'Checks the integrity of a foo object'
def add_args(self, parser):
parser.add_argument('target', help='The foo file to clean up')
parser.add_argument('-y', '--yes', help='Automatic answer yes to prompts', action='store_true')
def action(self, args):
# do cleaning
return 0
class FooCommand(BaseCommand):
description = 'Do things with foos'
sub_commands = {
'check': CheckFooCommand,
'clean': CleanFooCommand,
# more sub commands here
}
class RootCommand(BaseCommand):
description = 'My fancy CLI'
sub_commands = {
'foo': FooCommand,
# more sub commands here
}
if __name__ == '__main__':
sys.exit(RootCommand().run())
Running ./example.py would give the following output:
usage: My fancy CLI [-h] {foo} ...
positional arguments:
{foo}
foo Do things with foos
optional arguments:
-h, --help show this help message and exit
And running ./example.py foo gives:
usage: Do things with foos [-h] {check,clean} ...
positional arguments:
{check,clean}
check Checks the integrity of a foo object
clean Cleans up the foo object
optional arguments:
-h, --help show this help message and exit
An extended help message for each command (and sub-command) can be printed by adding the --help flag, So running ./example.py foo check --help gives:
usage: My fancy CLI foo check [-h] [-y] target Checks the integrity of a foo object positional arguments: target The foo file to clean up optional arguments: -h, --help show this help message and exit -y, --yes Automatic answer yes to prompts
Metadata
Release files for argparsetree 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| argparsetree-0.0.6.tar.gz | 4.7 kB | Without provenance | Details for argparsetree-0.0.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| argparsetree-0.0.6-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for argparsetree-0.0.6-py2.py3-none-any.whl |
Total release size: 11.0 kB
Release files / argparsetree-0.0.6.tar.gz
| Download URL | argparsetree-0.0.6.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8ddec3a55c2ca7fe3a0a2dc771aadffca9296b3548c7038a4c8b9285366925fc
|
|
BLAKE2b-256 checksum How to use checksums |
65f9bc760526c45f6d3d796a130a6eb549b298b2f02f3ccb32fcbad39993a905
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / argparsetree-0.0.6-py2.py3-none-any.whl
| Download URL | argparsetree-0.0.6-py2.py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
aa59ddc1b88a85d940419927d83e82637d317258ece6f0526b4b619fb72741a6
|
|
BLAKE2b-256 checksum How to use checksums |
82ebfcfe7705f48b4ad7601a8a78260083cbb3f3be2e13e74a8753e10723a7fb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for argparsetree 0.0.6
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.