painless-software / python-cli-test-helpers

Useful helpers for writing tests for your Python CLI program.
https://python-cli-test-helpers.readthedocs.io
GNU General Public License v3.0
28 stars 4 forks source link

Use docopt-dispatch in Docopt example #41

Open bittner opened 1 year ago

bittner commented 1 year ago

An alternative to using docopt-ng, or just docopt, may be docopt-dispatch. It makes it largely unnecessary to implement argument dispatching logic on your own and hence renders the Docopt CLI example source code more readable.

Example

"""Awesome CLI.

...

Usage:
  awesome (-h | --help | --version)
  awesome login --user <user> --pass <pass>

Commands:
  login   Authorize a user with username and password
"""

from docopt_dispatch import dispatch

def main():
    """The CLI entrypoint."""
    dispatch(__doc__, version=__version__)

@dispatch.on("login")
def login_command(**kwargs):
    """Authorize a user with username and password.""" 
    username = kwargs["user"]
    password = kwargs["pass"]
    ...

Related