CLI application argument parser
Project description
# commandparse
Module to parse command based CLI application.
Usage:
* Subclass the Command class
* Add a method with a name such as `prefix_commandname` with kwargs as required argument
* Create a an ArgumentParser and declare a subparser per command
* Register the commands in a dictionary
* Use the dispatch_command function with the commands and args returned by `parser.parse_args()`
```
parser = ArgumentParser(...)
[...]
sub = parser.add_subparsers(title="commands", dest="command", description="available commands")
# Registering commands
commands = {}
for command, method in Subclass.get_commands(prefix="prefix_"):
Subclass.set_subparser_for(command, method, sub)
commands[command] = method
[...]
args = parser.parse_args()
if args.command:
cmd = Subclass(...)
cmd.dispatch_command(commands, args)
else:
parser.print_usage()
```
See example.py for a more complete example. For a real world application using this lib, see: https://github.com/franc-pentest/ldeep
Module to parse command based CLI application.
Usage:
* Subclass the Command class
* Add a method with a name such as `prefix_commandname` with kwargs as required argument
* Create a an ArgumentParser and declare a subparser per command
* Register the commands in a dictionary
* Use the dispatch_command function with the commands and args returned by `parser.parse_args()`
```
parser = ArgumentParser(...)
[...]
sub = parser.add_subparsers(title="commands", dest="command", description="available commands")
# Registering commands
commands = {}
for command, method in Subclass.get_commands(prefix="prefix_"):
Subclass.set_subparser_for(command, method, sub)
commands[command] = method
[...]
args = parser.parse_args()
if args.command:
cmd = Subclass(...)
cmd.dispatch_command(commands, args)
else:
parser.print_usage()
```
See example.py for a more complete example. For a real world application using this lib, see: https://github.com/franc-pentest/ldeep
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
commandparse-1.0.0.tar.gz
(3.8 kB
view hashes)
Built Distribution
Close
Hashes for commandparse-1.0.0-py2-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae1c89c0974ecf8190ea68552e60b5f5ae79080bd269757f170c661fafd0cf24 |
|
MD5 | 38f8c198f2ca187244dc513a93b019e8 |
|
BLAKE2b-256 | d69ac7976b2506ac62716f70f488cc1f2be2a06029936471fdbd9b2d12e3d1c2 |