Skip to main content

knopt

Build command line applications by inspecting function signatures. Inspired by optfunc.

Quick start

To create a simple command line application, annotate a function with knopt.main:

from knopt import main

@main
def cli(input, output, verbose=False):
    if verbose:
        print('input:', input)
        print('output:', output)

Command line arguments are automatically parsed using argparse and passed to the cli function.

To use other features of the argparse library, you can use annotations on arguments. Pure type annotations work as expected:

from knopt import main

@main
def double(number: int):
    print(number * 2)

but for more control, you can use knopt.arg as an annotation and include options for argparse:

from knopt import main, arg

@main
def search(directory: arg(help="directory to search"),
           invert: arg('v', help="invert match") = False,
           pattern: arg('e', help="search pattern") = ''):
    """search files in a directory"""
    print("searching",
          ("~" if invert else "") + repr(pattern),
          "in", directory)

Note that the docstring of search is used as a description for the program.

Commands

You can also create a program accepting multiple commands by using the knopt.main annotation on a class. Every method of the class is converted to a command. For example:

from knopt import main

@main
class Program:
    """An example program with commands"""

    def __init__(self, verbose: arg('v') = False):
        self.verbose = verbose
        self.files = []

    def add(self, files: arg(nargs='*')):
        """add files to a repository"""
        if self.verbose:
            print('adding', files)

    def commit(self, all: arg('a') = False):
        """commit files"""
        if self.verbose:
            print('committing')

This creates a program with two commands add and commit, and a global option -v/--verbose.

Release files for knopt 0.3.1

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

Built distribution (wheel)

Table of built distributions (wheels) for knopt 0.3.1
File Interpreter ABI Platform
knopt-0.3.1-py3-none-any.whl Python 3 none any Details

Release files / knopt-0.3.1-py3-none-any.whl

Download URL knopt-0.3.1-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0253248b140e5d00fcc5767d20545f91bd408a694d2e9c056fd1d022f9dea076
BLAKE2b-256 checksum
How to use checksums
95468c788261151da793001177bcef5bc4160789b7a7194e61a576f190c06630
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release history Release notifications | RSS feed

This release

0.3.1 This release

1 release file

0.3

1 release file

0.2.1

1 release file

0.2

1 release file

0.1.1

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