Skip to main content

Easy CLIs from function inspection

Project description

CLI

An extremely easy to use library to generate python CLIs from functions through introspection.

Automatically generate the equivalent of this:

import argparse
parser = argparse.ArgumentParser(description="Generate a cryptographic token with a given entropy.")
parser.add_argument('method', nargs='?', default='xkcd', choices=('xkcd', 'short'))
parser.add_argument('entropy', nargs='?', default=70, type=int)
args = parser.parse_args()

if args.method == 'xkcd':
    print(xkcd(args.entropy))
else:
    print(alphanumeric(args.entropy))

from this:

from cli import Choice, cli

def token(method:Choice('xkcd', 'short')='xkcd', entropy=70):
    "Generate a cryptographic token with a given entropy."
    if method == 'xkcd':
	return xkcd(entropy)
    else:
	return alphanumeric(entropy)

cli(token)()

Keyword arguments (optional or mandatory) are supported as is one varargs argument per function. Arguments will be automatically converted into the type of their default argument (if it is not None) or their type annotation.

You can even generate CLIs for a whole module (or any other object with function attributes):

import example
from cli import cli

# convert_numbers automatically converts strings to int, float or complex so
# you don't have to annotate all the functions in a module. YMMV.
cli(example, convert_numbers=True)()

# You can get a reference to the current module with sys.modules[__name__]

Code quality

The code is very short, clearly documented inline and all advertised features are tested.

Reference

def example(positional, arguments):
    pass

def defaults(normally_one=1):
    pass

def typed(positional:bool, positional2:int):
    pass

def keyword(positional, *, keyword1=False, keyword2='default_filename'):
    pass

def mandatory_keywords(positional, *, keyword1, keyword2):
    pass

def varargs(pos1, pos2, *rest):
    pass

def choice_from_list(person:Choice('Ann', 'Bob', 'Charlie'))
    pass

Project details


Download files

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

Source Distribution

cmcaine-cli-0.0.1.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

cmcaine_cli-0.0.1-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page