clifun
Because cli's should be fun(ctions) ;).
import clifun
def my_program(a: int, b: str = "not provided"):
print(f"Running some code with: a={a}, b={b}")
if __name__ == "__main__":
clifun.call(my_program)
That's all it takes. Clifun will inspect your function and collect the values it needs it from command line arguments, environment variables, or config files, and then call it.
python examples/function --a 1
Running some code with: a=1, b=not provided
You can even run functions in any module without modifying the module at all
python clifun.py examples/module.py my_program --a 1
if you have the code checked out. Or if you pip installed
python -m clifun some_module.py function ...
Or if you have environment variables defined
export A=1
export B=hi
python example.py
again yields without you having to provide values
Basic(a=1, b='hi')
clifun also supports nested objects (or functions taking complex objects as inputs)
from typing import Optional
import datetime as dt
import attr
import clifun
@attr.s(auto_attribs=True, frozen=True)
class Foo:
a: dt.datetime
b: Optional[str] = None
@attr.s(auto_attribs=True, frozen=True)
class Bar:
f: Foo
c: int
def my_program(f: Foo, c: int):
print(Bar(f, c))
if __name__ == "__main__":
bar = clifun.call(my_program)
You specify values for the fields in the nested class by referring to them with a their field name in the outer class
python examples/advanced.py --c 1 --f.a 2020-01-01 --f.b hi
Bar(f=Foo(a=datetime.datetime(2021, 1, 1, 0, 0), b='hi'), c=1)
You can also supply one or more json formatted config files. Provide the name(s) of these files as positional arguments. clifun will search them, last file first, for any keys fields that are not provided at the command line before searching the environment.
python examples/advanced.py --c 1 examples/foo.json
Bar(f=Foo(a=datetime.datetime(2021, 1, 1, 0, 0), b='str'), c=1)
clifun is inspired by clout, but I wanted to try being a bit more opinionated to make both the library and code using it simpler.
Metadata
Release files for clifun 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| clifun-0.0.2.tar.gz | 6.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| clifun-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.7 kB
Release files / clifun-0.0.2.tar.gz
| Download URL | clifun-0.0.2.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f6672e99386c8684887401f8e09fd42f8b1879fa9671352f57853b292d2075bb
|
|
BLAKE2b-256 checksum How to use checksums |
d0e7f926ca4bcd51e471a7a20bd2da7e3fbac62db24b9a34a0adf8a9b6c6f68f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.9.9 Linux/5.15.11
|
Release files / clifun-0.0.2-py3-none-any.whl
| Download URL | clifun-0.0.2-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
da1e6d111bd252c71c664627cd58f9eda5f31a928ecb233cfdc84de730ddc2a2
|
|
BLAKE2b-256 checksum How to use checksums |
9fc6b04b735b3335a4bf3d00edddc7b97cd97613516339bd2ef0d02a5ed385a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.12 CPython/3.9.9 Linux/5.15.11
|