Automatically generate a simple CLI.
Project description
FunCLI 
Automatically generate a simple CLI.
Installation
pip install funcli
Basic Usage
def main(*args: int):
"""
Adds numbers together.
:param args: some numbers
"""
print("Sum:", sum(args))
if __name__ == '__main__':
import funcli
funcli.main()
# $ python sum.py 1 2 3
# Sum: 6
--help
is autogenerated by the backend (argparse):
$ python sum.py -h
usage: sum.py [-h] [args [args ...]]
Adds numbers together.
positional arguments:
args some numbers
optional arguments:
-h, --help show this help message and exit
Advanced Usage
from typing import Optional, List
def foo(): pass
def bleep(): pass
def bloop(a, second_arg: int = 0, *c: str, d: Optional[List[float]] = None):
print("Args:", a, second_arg, c, d)
if __name__ == '__main__':
import funcli
funcli.main({ 'foo': foo, 'bar': { bleep, bloop } })
# $ python advanced.py bar bloop arg0 c0 c1 --second-arg=987 --d 42 -0.3
# Args: arg0 987 ('c0', 'c1'), [42, -0.3]
Note that underscores in variable names are automatically converted to dashes
in the command-line argument (second_arg
-> --second-arg
). You can disable
this by passing dash = False
to funcli.main
or funcli.run
.
Reference
funcli.main(spec = None, dash = True)
Sugar. spec
defaults to the main
function from the caller's scope.
Calls funcli.run
on spec
, and calls sys.exit
with the return value.
If dash == True
, underscores in Python parameters will be changed to dashes
in the corresponding command-line arguments.
funcli.run(spec, args = None, converters = None, dash = True)
spec
is either a callable, a sequence of callables, or a dict mapping strings to nestedspecs
args
default tosys.argv[1:]
converters
is a mapping from types (or whatever you want to use as annotations) to a function that parses a command-line argument- if
dash == True
, underscores in Python parameters will be changed to dashes in the corresponding command-line arguments
Given functions foo
, bar
, baz
, here are some sample invocations:
funcli.run(foo, ['arg0']) # Calls foo('arg0')
funcli.run({ foo, bar }, ['bar', 'arg0']) # Calls bar('arg0')
funcli.run({ 'beep': foo, 'bloop': [bar, baz] }, ['beep', 'arg0']) # Calls foo('arg0')
funcli.run({ 'beep': foo, 'bloop': [bar, baz] }, ['bloop', 'bar', 'arg0']) # Calls bar('arg0')
bool
arguments
Non-optional bool values should be passed as True
and False
on the command line.
Optional bool values, on the other hand, must be omitted.
def f(warnings: bool = False): ...
funcli.run(f, ['--warnings']) # f(warnings = True)
Note: currently, if the default value is True
, there is no way to pass False
.
Converters
Built-in converters handle int
, float
, bool
, and pathlib.Path
. Unannotated args are kept as a str
.
Basic sequence types (list
/List[T]
, set
/Set[T]
, tuple
/Tuple[T, ...]
, Iterable[T]
, Sequence[T]
) are supported, but only as optional arguments, e.g.:
def f(mylist: List[int] = []): ...
funcli.run(f, ['--mylist', '1', '2']) # f([1, 2])
Optional[T]
is supported, but there's currently no way to explicitly pass None
values.
Notes
Because of argparse
limitations:
**kwargs
aren't supported; if your function has them, they'll always be empty- optional arguments cannot be positional;
f(a = 'default')
has to be invoked aspython foo.py --a=nondefault
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 Distributions
Built Distribution
File details
Details for the file funcli-0.7.1-py3-none-any.whl
.
File metadata
- Download URL: funcli-0.7.1-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
688b7b7935d569f3754bd293c378b4ae8729cc6dd2e84a8c08a50d82128254c9
|
|
MD5 |
ec0cf994aab974a5ab47b77b8b5e5879
|
|
BLAKE2b-256 |
6c139b230ce652c0d05e6e600bfc0c904e4c814af092807ea925fdead9b348e3
|