Package for creating complex command line argument trees using argparse
Project description
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argparsetree-0.0.6.tar.gz.
File metadata
- Download URL: argparsetree-0.0.6.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ddec3a55c2ca7fe3a0a2dc771aadffca9296b3548c7038a4c8b9285366925fc
|
|
| MD5 |
ba0a93c1a2013050cd44a29de1af7283
|
|
| BLAKE2b-256 |
65f9bc760526c45f6d3d796a130a6eb549b298b2f02f3ccb32fcbad39993a905
|
File details
Details for the file argparsetree-0.0.6-py2.py3-none-any.whl.
File metadata
- Download URL: argparsetree-0.0.6-py2.py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa59ddc1b88a85d940419927d83e82637d317258ece6f0526b4b619fb72741a6
|
|
| MD5 |
ec1748f6001f8c1e6ffe7d8fe68bb417
|
|
| BLAKE2b-256 |
82ebfcfe7705f48b4ad7601a8a78260083cbb3f3be2e13e74a8753e10723a7fb
|