Skip to main content

libcli

Command line interface framework.

The libcli package, built on argparse, provides functions and features that are common to or desired by many command line applications:

  • Colorized help output, with prog -h, or prog --help.

  • Help output in Markdown format, with prog --md-help.

  • Print all help (top command, and all subcommands), with prog -H, or prog --long-help. (For commands with subcommands).

  • Configure logging, with -v, or --verbose ("-v"=INFO, "-vv"=DEBUG, "-vvv"=TRACE). Integrated with loguru and logging.

  • Print the current version of the application, with -V, or --version; uses value from application's package metadata.

  • Load configuration from a file, before parsing the command line, with --config FILE. (Well, it parsed at least that much.. and "-v" for debugging "--config" itself.) This allows values from the config file to be available when building the argparse.ArgumentParser, for setting defaults, or including within help strings of arguments/options.

  • Print the active configuration, after loading the config file, with --print-config.

  • Print the application's URL, with --print-url; uses value from application's package metadata.

  • Integrate with argcomplete, with --completion.

  • Automatic inclusion of all common options, above.

  • Normalized help text of all command line arguments/options.

    • Force the first letter of all help strings to be upper case.
    • Force all help strings to end with a period.
  • Provides a function add_default_to_help to consistently include a default value in an argument/option's help string.

  • Supports single command applications, and command/sub-commands applications.

class BaseCLI

Command line interface base class.

$ cat minimal.py

from libcli import BaseCLI
class HelloCLI(BaseCLI):
    def main(self) -> None:
        print("Hello")
if __name__ == "__main__":
    HelloCLI().main()

$ python minimal.py -h

Usage: minimal.py [-h] [-v] [-V] [--print-config] [--print-url] [--completion [SHELL]]

General Options:
  -h, --help            Show this help message and exit.
  -v, --verbose         `-v` for detailed output and `-vv` for more detailed.
  -V, --version         Print version number and exit.
  --print-config        Print effective config and exit.
  --print-url           Print project url and exit.
  --completion [SHELL]  Print completion scripts for `SHELL` and exit (default: `bash`).

$ cat simple.py

from libcli import BaseCLI

class HelloCLI(BaseCLI):

    def init_parser(self) -> None:
        self.parser = self.ArgumentParser(
            prog=__package__,
            description="This program says hello.",
        )

    def add_arguments(self) -> None:
        self.parser.add_argument(
            "--spanish",
            action="store_true",
            help="Say hello in Spanish.",
        )
        self.parser.add_argument(
            "name",
            help="The person to say hello to.",
        )

    def main(self) -> None:
        if self.options.spanish:
            print(f"Hola, {self.options.name}!")
        else:
            print(f"Hello, {self.options.name}!")

if __name__ == "__main__":
    HelloCLI().main()

$ python simply.py -h

Usage: simple.py [--spanish] [-h] [-v] [-V] [--print-config] [--print-url]
                 [--completion [SHELL]] name

This program says hello.

Positional Arguments:
  name                  The person to say hello to.

Options:
  --spanish             Say hello in Spanish.

General Options:
  -h, --help            Show this help message and exit.
  -v, --verbose         `-v` for detailed output and `-vv` for more detailed.
  -V, --version         Print version number and exit.
  --print-config        Print effective config and exit.
  --print-url           Print project url and exit.
  --completion [SHELL]  Print completion scripts for `SHELL` and exit (default: `bash`).

class BaseCmd

Base command class; for commands with subcommands.

$ cat complex.py

from libcli import BaseCLI, BaseCmd

class EnglishCmd(BaseCmd):

    def init_command(self) -> None:

        parser = self.add_subcommand_parser(
            "english",
            help="Say hello in English",
            description="The `%(prog)s` command says hello in English.",
        )

        parser.add_argument(
            "name",
            help="The person to say hello to.",
        )

    def run(self) -> None:
        print(f"Hello {self.options.name}!")

class SpanishCmd(BaseCmd):

    def init_command(self) -> None:

        parser = self.add_subcommand_parser(
            "spanish",
            help="Say hello in Spanish",
            description="The `%(prog)s` command says hello in Spanish.",
        )

        parser.add_argument(
            "name",
            help="The person to say hello to.",
        )

    def run(self) -> None:
        print(f"Hola {self.options.name}!")

class HelloCLI(BaseCLI):

    def init_parser(self) -> None:
        self.parser = self.ArgumentParser(
            prog=__package__,
            description="This program says hello.",
        )

    def add_arguments(self) -> None:
        self.add_subcommand_classes([EnglishCmd, SpanishCmd])

    def main(self) -> None:
        if not self.options.cmd:
            self.parser.print_help()
            self.parser.exit(2, "error: Missing COMMAND")
        self.options.cmd()

if __name__ == "__main__":
    HelloCLI().main()

$ python complex.py -H

---------------------------------- COMPLEX.PY ----------------------------------

usage: complex.py [-h] [-H] [-v] [-V] [--print-config] [--print-url]
                  [--completion [SHELL]]
                  COMMAND ...

This program says hello.

Specify one of:
  COMMAND
    english             Say hello in English.
    spanish             Say hello in Spanish.

General options:
  -h, --help            Show this help message and exit.
  -H, --long-help       Show help for all commands and exit.
  -v, --verbose         `-v` for detailed output and `-vv` for more detailed.
  -V, --version         Print version number and exit.
  --print-config        Print effective config and exit.
  --print-url           Print project url and exit.
  --completion [SHELL]  Print completion scripts for `SHELL` and exit
                        (default: `bash`).

------------------------------ COMPLEX.PY ENGLISH ------------------------------

usage: complex.py english [-h] name

The `complex.py english` command says hello in English.

positional arguments:
  name        The person to say hello to.

options:
  -h, --help  Show this help message and exit.

------------------------------ COMPLEX.PY SPANISH ------------------------------

usage: complex.py spanish [-h] name

The `complex.py spanish` command says hello in Spanish.

positional arguments:
  name        The person to say hello to.

options:
  -h, --help  Show this help message and exit.

Metadata

Release files for rlane-libcli 1.0.12

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

Source distribution (sdist)

Source distribution for rlane-libcli 1.0.12
File Size Uploaded
rlane_libcli-1.0.12.tar.gz 23.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rlane-libcli 1.0.12
File Interpreter ABI Platform
rlane_libcli-1.0.12-py3-none-any.whl Python 3 none any Details

Total release size: 50.2 kB

Release files / rlane_libcli-1.0.12.tar.gz

Download URL rlane_libcli-1.0.12.tar.gz
Size 23.6 kB
Tags Source
SHA-256 checksum
How to use checksums
3ac1c01f704112880b9f04e4851045949474b627cc855ac1362eddc55fb041ed
BLAKE2b-256 checksum
How to use checksums
fc7f15f535541065fd64bac495eed372eb42cd0813dda1b368fbf13a828f62b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.13.0

Release files / rlane_libcli-1.0.12-py3-none-any.whl

Download URL rlane_libcli-1.0.12-py3-none-any.whl
Size 26.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7726e28b046ede25b5b84a197fe5b9bb8c45e49d7a9da23322342d7cd3867238
BLAKE2b-256 checksum
How to use checksums
86907492c40c25c09f4ac2563dd8ef7b6cc0bfbd64671b7849a28969beeefb43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.13.0

Release history Release notifications | RSS feed

This release

1.0.12 This release

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

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