Skip to main content

Construct a comand line interface based on a function or class

Project description

clattr

Easily make a command line interface from any interface in your code. How easily? This easy:

import clattr

def my_program(a: int, b: str = "not provided"):
  print(f"Running some code with: a={a}, b={b}")

if __name__ == "__main__":
  clattr.call(my_program)

That's all it takes. Clattr 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

If you want to think in a more data oriented design, you can have clattr construct a data object for you and use it as you please.

import attr
import clattr


@attr.s(auto_attribs=True, frozen=True)
class Basic:
    a: int
    b: str = "not provided"

def my_program(data: Basic):
    # Your actual program will go here. For this example we just print the input.
    print(data)


if __name__ == "__main__":
    data = clattr.call(Basic)
    my_program(data)

This could be invoked as

python examples/basic.py --a 1 --b hi

clattr will construct this object

Basic(a=1, b='hi')

Which you can then pass into the rest of your code as you please. The example simply prints it and then exits.

Or if you have environment variables defined

export A=1
export B=hi
python example.py

again yields

Basic(a=1, b='hi')

clattr also supports nested objects (or functions taking complex objects as inputs)

from typing import Optional
import datetime as dt

import attr
import clattr


@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(data: Bar):
    print(data)

if __name__ == "__main__":
    bar = clattr.call(Bar)
    my_program(bar)

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=1, 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. `clattr`` 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=1, b='str'), c=1)

clattr 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

clattr-0.0.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

clattr-0.0.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file clattr-0.0.1.tar.gz.

File metadata

  • Download URL: clattr-0.0.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.9

File hashes

Hashes for clattr-0.0.1.tar.gz
Algorithm Hash digest
SHA256 3c2e60aa312d22783c144c1de83177be6aee1444cc8fa378dd311ab753f5de4a
MD5 8a659cf831efa92ecbdc1cec7d82cdd5
BLAKE2b-256 a11b34edc2a85576d123452d817b6ad7a05ec6a82ec71c4595826098fe4d3267

See more details on using hashes here.

File details

Details for the file clattr-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: clattr-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.9

File hashes

Hashes for clattr-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5f3942e2e108304fbd627d80a786c1f0d5b395b9164291bffa50056b0c5a8499
MD5 3ebe65a505332db9b7545d38afc6d304
BLAKE2b-256 dbbf0159f454549075cec4cd1cd059b52a23f1a71c1832cdb4b4004a0e04e8e5

See more details on using hashes here.

Supported by

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