Skip to main content

A low information-redundancy cli framework.

Project description


A low information-redundancy cli framework for a quick and dirty way of converting python scripts to a command line tool.

Inspired by fire and click.


pip install py-dispatch


import dispatch
import sys

def hello(name: str, verbose: bool, debug: bool, file: str = 'stdout'):
    '''Run the 'hello' command line interface.

    :v verbose: Run the command verbosly
    :name: Name of the person you are saying hello to.
    :file: Either stdout or stderr
    if debug:
        print(f'debugging with {name}', file=getattr(sys, file))
        print(f'hello, {name}', file=getattr(sys, file))

if __name__ == '__main__':
$ python --help
Run the 'hello' command line interface.

    hello [options]

        --name      Name of the person you are saying hello to.
    -v, --verbose   Run the command verbosly
        --file      Either stdout or stderr (default: stdout)
    -h, --help      Get help.


Arguments can be retrieved in two ways, either from Command.args or with positional only arguments. When a cli function is run, it is replaced with a Command object so the cli function can use the command in it's own body.

def cli(verbose: bool):

Running this cli with python hello --verbose these are some args will result in ['hello', 'these', 'are', 'some', 'args'].

The Other way to get arguments is to give the cli function a positional only argument at the beginning of the parameters list.

def cli(*args, verbose: bool):

Running this cli as before will have the same result. However, it only works when the args tuple is the first function parameter.

Properties of Flags

Because flags are specified by function arguments, the properties of flags are a little bit weird.

Boolean Flags

All boolean flags have a default of False.

A positional argument with no default and no type annotation is assumed to be a boolean flag and will default to a value of False.

def cli(verbose):
    if verbose:
        print('the verbose flag has been given')
        print('using default of False for verbose')

Flag Types

Dispatch uses type annotations to infer flag types and will use those annotations to convert the arguments given.

def cli(num: complex, decimal: float):

When the program is executed it will convert each argument to its type.

python --num=5+3j --decimal=5.9

For this command, the parser internals will eventually call complex('5+3j') and float('5.9') before giving the values as function arguments. What this means is that you can use any type as long it has an __init__ function that takes one argument. However there are exceptions

Project details

Release history Release notifications

Download files

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

Files for py-dispatch, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size py-dispatch-0.0.2.tar.gz (8.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page