Turn a dict of arguments into cli commands, ideal companion of docopt.
Project description
commandopt
Turn a dict of arguments into cli commands, ideal companion of docopt.
Why ?
Using the commandopt.commandopt
decorator, you are able to declare commands to be
executed depending on the input arguments of your app (required or optional).
It reduces the boilerplate code in your main()
.
Declaration
def commandopt(mandopts: List, opts: List = None):
# ...
Call
@commandopt(mandatory_arguments, optional_arguments)
def xxxx(*args, **kwargs):
Example usage
#myapp/myapp.py
"""Naval Fate.
Usage:
naval_fate.py ship new <name>...
naval_fate.py new-ship [<name>]
naval_fate.py --version
Options:
--version Show version.
"""
from commandopt import Command
from docopt import docopt
import myapp.commands.ship
if __name__ == '__main__':
arguments = docopt(__doc__, version='Naval Fate 2.0')
run = Command(arguments) # get the registered function
run(arguments) # execute the function
# or
# run = Command(arguments, call=True)
#myapp/commands/ship.py
from commandopt import commandopt
class ShipCommand:
@commandopt(["ship", "new", "<name>"])
@commandopt(["new-ship"], ["<name>"])
def new(arguments):
"""You can stack the decorator if you want."""
name = arguments["<name>"] or "case when name is empty"
# ... your code here
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
Commandopt-0.2.0.tar.gz
(2.8 kB
view details)
File details
Details for the file Commandopt-0.2.0.tar.gz
.
File metadata
- Download URL: Commandopt-0.2.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1371909132c1178df3f474121f02fd38c548e8b7ed91141c43e70cde915b3a0 |
|
MD5 | 68b198a601cd54c8a06d6aa73c21c8ed |
|
BLAKE2b-256 | 29a6f2d2de0b6f74f8b4d3cf6031aa31e41e680654af294c2fcf1942319c6186 |