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.

Optionally argcomplete is supported.

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. Optionally, subcommand name can be passed as parameter:

@command('name')
def test():
    ...

Subcommands can be groupped passing group_name paramenter:

@command(group_name="group")
def bar()
    ...

@command(group_name="group")
def baz()
    ...

This two functions will be called from command line as group bar and group baz

Each positional argument of the decorated function will be a positional paramenter.

Each optional argument will be an optional flag.

Type hints are used to covert types from command line string. If no type is defined, paramenter are handled as strings.

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 typing.Optional type as hint with the parameter type, e.g. Optional[str]

Function docstring is used to set command's help and description. To set arguments help string, add a line in docstring like

@param argname : argument help

Exacmple:

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

@command
def two(name:Optional[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        enable debug output
--value VALUE  (default 'default')
--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 group(name:str, help:str = "", description:str = "")

Set command group help and description

If a group named name does not exists, is created

Can be used also as a context manager. Each command defined in context will be added to the group

with group('file', help="Manage files", description="Functions to manage files"):
    @command
    def ls():
        ...
    
    @command
    def rm():
        ...

def run(prog:str=None, usage:str=None, description:str=None, **kwargs)

Run your application.

Builds the command line parse, with given arguments, and execute the requested function. It's a shorthand for

parser = build_parser(prog, usage, description, **kwargs)
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 Distributions

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

Built Distribution

climatik-0.4.1-py38-none-any.whl (18.3 kB view details)

Uploaded Python 3.8

File details

Details for the file climatik-0.4.1-py38-none-any.whl.

File metadata

  • Download URL: climatik-0.4.1-py38-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.63.1 CPython/3.10.3

File hashes

Hashes for climatik-0.4.1-py38-none-any.whl
Algorithm Hash digest
SHA256 c1be41c1e59b33cf641b3b063fd24b9821c7cc9d6aa492363a9c1d15166eddbc
MD5 a2984e71a70f50bdc0ffeb59677f2644
BLAKE2b-256 5e902c49ac48a6124fbca9729f7dc244aec29f334ec7d6637fcdd9dfaa2deaf9

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