Skip to main content

calf: Command Argument Loading Function for Python

Calf lets you replace your command argument parsing code by simply writing a function. The command argument parser is configured with a proper docstring, and perhaps some annotations and default values for the parameters.

At present the docstring needs to be written in Google style, but the design is that it is easy to swap the parsing function with a different parsing a different docstring style string. In fact, the design is that you can customize a very wide range of characteristics of the module.

This package shamelessly stole a lot of ideas from plac, but hopes to be more focused on creating comfortable command line interfaces rather than becoming a Swiss knife for programs with text-only user interface.

Basic example

Hello-world looks like this:

def hello(name) -> None:
    """Say hello

    Args:

        name: name of to say hello to

    """
    print('Hello,', name)

if __name__ == '__main__':
    import calf
    calf.call(hello)

You can run this with:

hello.py Isaac

Here name is a positional command line argument: a normal function argument always maps to a positional command line argument. If you want a keyword argument instead, you can replace the function like this:

def hello(*, name: str = 'Isaac') -> None:
    """Say hello

    Args:

        name: (-n) name of to say hello to

    """
    print('Hello,', name)

Then the program is run like one of the following:

hello.py
hello.py --name Cathy
hello.py -n Cathy

Now name is an option: a keyword-only function argument always maps to a function. Note also that -n in the docstring describing the argument, leading in parentheses, becomes the short option name. It is usually a good idea to allow options not to be specified, by providing a default value. Positional arguments can also be provided a default value, but it doesn't mix with variable arguments as described below. In this version we are explicit about the type of the argument.

There is an exception: any boolean function argument becomes a default-off flag. I failed to find a natural way to have a default-on flag, so it is not provided.

Variable arguments and keyword arguments can also be used. Variable arguments will become a list of the specified type:

def do_sum(*args: int) -> None:
    """Sum numbers"""
    print('Sum =', sum(args, 0))

Keyword arguments causes command line arguments like "=" to be stolen from the var-arg and form a map.

Advanced capability

You can have your function accept other types. Calf would normally use one positional argument or option for each function argument, and whatever string you specified in the argument will be passed to the type you specified (via default argument or annotation) as constructor. But you can extend calf by creating a subclass of "selector" which selects function arguments based on name and type. It then specifies how to create "loader" to handle the function argument, and a loader an use multiple command line arguments. See composite.py in the docs directory to see how this is done.

Other parts of the module can also be overridden. See the design document in the docs directory to understand the design and do all sorts of things with calf.

Release files for calf 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for calf 0.1
File Size Uploaded
calf-0.1.tar.gz 13.5 kB Details

Release files / calf-0.1.tar.gz

Download URL calf-0.1.tar.gz
Size 13.5 kB
Tags Source
SHA-256 checksum
How to use checksums
6d8b4787b96a4805f45364bd564e5d804ae3f51d2d6d4b79f6eb2d92708737be
BLAKE2b-256 checksum
How to use checksums
2bf63fafb9ef8679433661752666bb4d9ec412877a720672f1d0a274100498e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.5.3

Release history Release notifications | RSS feed

0.5

2 release files

0.4.1

2 release files

0.4

2 release files

0.3.3

1 release file

0.3.2

2 release files

0.3.1

3 release files

0.3

2 release files

0.2

3 release files

0.1.6

2 release files

0.1.5

2 release files

This release

0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page