Skip to main content

Automatic command line apps

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

knopt-0.3.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file knopt-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: knopt-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for knopt-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0253248b140e5d00fcc5767d20545f91bd408a694d2e9c056fd1d022f9dea076
MD5 06a4fc2fce8043fffd7268b7c993222a
BLAKE2b-256 95468c788261151da793001177bcef5bc4160789b7a7194e61a576f190c06630

See more details on using hashes here.

Supported by

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