Skip to main content

Create command line interface from function definitions.

Project description

Create command line interface from function definitions.

Each function will be a subcommand of your application.

Climatik define a function decorator which parse function definition and build a subcommand command line parser.

Docs

def command(fnc:Callable)

Build subcommand from function

Subcommand name will be the function name and arguments are parsed to build the command line.

Each positional argument will be a positional paramenter.

Each optional argument will be an optional flag.

Type hints are used to covert types from command line string.

An argument with bool type is converted to an optional flag parameter (with default sematic as "False")

To create an optional positional paramenter, use the Positional custom type as hint, which optionally can get a type too, eg Positional[str]

Function docstring is used to set command's help and description.

@command
def one(name, debug:bool, value="default", switchoff=True):
    "First subcommand"
    ...

@command
def two(name:Positional[str] = None, long_param = None):
    "Second subcommand"
    ...

gives:

$ script -h
usage: script [-h] {one,two} ...

positional arguments:
{one,two}
    one       First subcommand
    two       Second subcommand

optional arguments:
-h, --help  show this help message and exit

$ script one -h
usage: script one [-h] [--debug] [--value VALUE] [--switchoff] name

First subcommand

positional arguments:
name

optional arguments:
-h, --help     show this help message and exit
--debug
--value VALUE
--switchoff

$ script two -h
usage: script two [-h] [--long-param LONG_PARAM] [name]

Second subcommand

positional arguments:
name

optional arguments:
-h, --help            show this help message and exit
--long-param LONG_PARAM

def run()

Run your application.

Builds the argparser and execute the requested function. It's a shorthand for

parser = build_parser()
execute(parser)

def execute(parser:argparse.ArgumentParser)

Execute command line from given parser

def get_parser(*args, **kwargs) -> argparse.ArgumentParser:

Build the command line parser

Arguments are passed to argparse.ArgumentParser constructor

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

climatik-0.1.0.tar.gz (15.7 kB view hashes)

Uploaded Source

Built Distribution

climatik-0.1.0-py3-none-any.whl (16.2 kB view hashes)

Uploaded Python 3

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