Skip to main content

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().

Signature

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

Release files for commandopt 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for commandopt 0.2.1
File Size Uploaded
Commandopt-0.2.1.tar.gz 4.0 kB Details

Release files / Commandopt-0.2.1.tar.gz

Download URL Commandopt-0.2.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
89db15ad950b7b5eceaeb9977828918ca1544b37c4bdde4b26e0cf26ab975ae2
BLAKE2b-256 checksum
How to use checksums
0bd90c3e828d784210470bb200daacd47abae1139fc188d50e876b1102da624c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.2.1 This release

1 release file

0.2.0

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page