Skip to main content

Construct a comand line interface based on a function or class

Project description

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.

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

clifun-0.0.2.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

clifun-0.0.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file clifun-0.0.2.tar.gz.

File metadata

  • Download URL: clifun-0.0.2.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Linux/5.15.11

File hashes

Hashes for clifun-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f6672e99386c8684887401f8e09fd42f8b1879fa9671352f57853b292d2075bb
MD5 6ba4561007e4884a9aba42a6f8a4a780
BLAKE2b-256 d0e7f926ca4bcd51e471a7a20bd2da7e3fbac62db24b9a34a0adf8a9b6c6f68f

See more details on using hashes here.

File details

Details for the file clifun-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: clifun-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.9 Linux/5.15.11

File hashes

Hashes for clifun-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 da1e6d111bd252c71c664627cd58f9eda5f31a928ecb233cfdc84de730ddc2a2
MD5 3b9c59000f81eb64e867cfac54e5f769
BLAKE2b-256 9fc6b04b735b3335a4bf3d00edddc7b97cd97613516339bd2ef0d02a5ed385a9

See more details on using hashes here.

Supported by

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