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
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
cmcaine-cli-0.0.1.tar.gz
(3.9 kB
view hashes)
Built Distribution
Close
Hashes for cmcaine_cli-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07c02b53dc7d5527c10ef1b3a658e481836bb23cb2b408fef7fcf29e8cd29c29 |
|
MD5 | 0632cf3a495b4f0dff4dbb7834b3d132 |
|
BLAKE2b-256 | 317af450d62d889174b6888d2f75f654cfba8bf9d382206ead2a2c15f4993496 |