Skip to main content

CLI application commands parser

Project description

# commandparse

Module to parse command based CLI application.


* 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)

See for a more complete example. For a real world application using this lib, see:

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for commandparse, version 1.0.8
Filename, size File type Python version Upload date Hashes
Filename, size commandparse-1.0.8.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page