rlane-libcli


Namerlane-libcli JSON
Version 1.0.6 PyPI version JSON
download
home_pageNone
SummaryCommand line interface framework
upload_time2024-10-26 02:34:10
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseMIT
keywords argparse cli color markdown python
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## libcli

Command line interface framework.

The `libcli` package, built on
[argparse](https://docs.python.org/3/library/argparse.html), 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](https://github.com/Delgan/loguru) and
[logging](https://docs.python.org/3/library/logging.html).

* 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](https://github.com/kislyuk/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.




            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "rlane-libcli",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "argparse, cli, color, markdown, python",
    "author": null,
    "author_email": "Russel Lane <russel@rlane.com>",
    "download_url": "https://files.pythonhosted.org/packages/af/4c/3a6a1900f9e86f352d79f58fd3e7cc7afd434c0efe318f63937dc625563d/rlane_libcli-1.0.6.tar.gz",
    "platform": null,
    "description": "## libcli\n\nCommand line interface framework.\n\nThe `libcli` package, built on\n[argparse](https://docs.python.org/3/library/argparse.html), provides\nfunctions and features that are common to or desired by many command\nline applications:\n\n* Colorized help output, with `prog -h`, or `prog --help`.\n\n* Help output in `Markdown` format, with `prog --md-help`.\n\n* Print all help (top command, and all subcommands), with `prog -H`, or\n`prog --long-help`. (For commands with subcommands).\n\n* Configure logging, with `-v`, or `--verbose`\n(`\"-v\"`=INFO, `\"-vv\"`=DEBUG, `\"-vvv\"`=TRACE). Integrated\nwith [loguru](https://github.com/Delgan/loguru) and\n[logging](https://docs.python.org/3/library/logging.html).\n\n* Print the current version of the application, with `-V`, or `--version`;\nuses value from application's package metadata.\n\n* Load configuration from a file, *before* parsing the command line,\nwith `--config FILE`.  (Well, it parsed at least that much.. and \"-v\"\nfor debugging \"--config\" itself.)  This allows values from the config\nfile to be available when building the `argparse.ArgumentParser`, for\nsetting defaults, or including within help strings of arguments/options.\n\n* Print the active configuration, after loading the config file, with `--print-config`.\n\n* Print the application's URL, with `--print-url`;\nuses value from application's package metadata.\n\n* Integrate with [argcomplete](https://github.com/kislyuk/argcomplete),\nwith `--completion`.\n\n* Automatic inclusion of all common options, above.\n\n* Normalized help text of all command line arguments/options.\n\n    * Force the first letter of all help strings to be upper case.\n    * Force all help strings to end with a period.\n\n* Provides a function `add_default_to_help` to consistently include\na default value in an argument/option's help string.\n\n* Supports single command applications, and command/sub-commands applications.\n\n\n### class BaseCLI\n\nCommand line interface base class.\n\n$ cat minimal.py\n\n    from libcli import BaseCLI\n    class HelloCLI(BaseCLI):\n        def main(self) -> None:\n            print(\"Hello\")\n    if __name__ == \"__main__\":\n        HelloCLI().main()\n\n$ python minimal.py -h\n\n    Usage: minimal.py [-h] [-v] [-V] [--print-config] [--print-url] [--completion [SHELL]]\n\n    General Options:\n      -h, --help            Show this help message and exit.\n      -v, --verbose         `-v` for detailed output and `-vv` for more detailed.\n      -V, --version         Print version number and exit.\n      --print-config        Print effective config and exit.\n      --print-url           Print project url and exit.\n      --completion [SHELL]  Print completion scripts for `SHELL` and exit (default: `bash`).\n\n$ cat simple.py\n\n    from libcli import BaseCLI\n\n    class HelloCLI(BaseCLI):\n\n        def init_parser(self) -> None:\n            self.parser = self.ArgumentParser(\n                prog=__package__,\n                description=\"This program says hello.\",\n            )\n\n        def add_arguments(self) -> None:\n            self.parser.add_argument(\n                \"--spanish\",\n                action=\"store_true\",\n                help=\"Say hello in Spanish.\",\n            )\n            self.parser.add_argument(\n                \"name\",\n                help=\"The person to say hello to.\",\n            )\n\n        def main(self) -> None:\n            if self.options.spanish:\n                print(f\"Hola, {self.options.name}!\")\n            else:\n                print(f\"Hello, {self.options.name}!\")\n\n    if __name__ == \"__main__\":\n        HelloCLI().main()\n\n$ python simply.py -h\n\n    Usage: simple.py [--spanish] [-h] [-v] [-V] [--print-config] [--print-url]\n                     [--completion [SHELL]] name\n\n    This program says hello.\n\n    Positional Arguments:\n      name                  The person to say hello to.\n\n    Options:\n      --spanish             Say hello in Spanish.\n\n    General Options:\n      -h, --help            Show this help message and exit.\n      -v, --verbose         `-v` for detailed output and `-vv` for more detailed.\n      -V, --version         Print version number and exit.\n      --print-config        Print effective config and exit.\n      --print-url           Print project url and exit.\n      --completion [SHELL]  Print completion scripts for `SHELL` and exit (default: `bash`).\n\n\n\n### class BaseCmd\n\nBase command class; for commands with subcommands.\n\n$ cat complex.py\n\n    from libcli import BaseCLI, BaseCmd\n\n    class EnglishCmd(BaseCmd):\n\n        def init_command(self) -> None:\n\n            parser = self.add_subcommand_parser(\n                \"english\",\n                help=\"Say hello in English\",\n                description=\"The `%(prog)s` command says hello in English.\",\n            )\n\n            parser.add_argument(\n                \"name\",\n                help=\"The person to say hello to.\",\n            )\n\n        def run(self) -> None:\n            print(f\"Hello {self.options.name}!\")\n\n    class SpanishCmd(BaseCmd):\n\n        def init_command(self) -> None:\n\n            parser = self.add_subcommand_parser(\n                \"spanish\",\n                help=\"Say hello in Spanish\",\n                description=\"The `%(prog)s` command says hello in Spanish.\",\n            )\n\n            parser.add_argument(\n                \"name\",\n                help=\"The person to say hello to.\",\n            )\n\n        def run(self) -> None:\n            print(f\"Hola {self.options.name}!\")\n\n    class HelloCLI(BaseCLI):\n\n        def init_parser(self) -> None:\n            self.parser = self.ArgumentParser(\n                prog=__package__,\n                description=\"This program says hello.\",\n            )\n\n        def add_arguments(self) -> None:\n            self.add_subcommand_classes([EnglishCmd, SpanishCmd])\n\n        def main(self) -> None:\n            if not self.options.cmd:\n                self.parser.print_help()\n                self.parser.exit(2, \"error: Missing COMMAND\")\n            self.options.cmd()\n\n    if __name__ == \"__main__\":\n        HelloCLI().main()\n\n$ python complex.py -H\n\n    ---------------------------------- COMPLEX.PY ----------------------------------\n\n    usage: complex.py [-h] [-H] [-v] [-V] [--print-config] [--print-url]\n                      [--completion [SHELL]]\n                      COMMAND ...\n\n    This program says hello.\n\n    Specify one of:\n      COMMAND\n        english             Say hello in English.\n        spanish             Say hello in Spanish.\n\n    General options:\n      -h, --help            Show this help message and exit.\n      -H, --long-help       Show help for all commands and exit.\n      -v, --verbose         `-v` for detailed output and `-vv` for more detailed.\n      -V, --version         Print version number and exit.\n      --print-config        Print effective config and exit.\n      --print-url           Print project url and exit.\n      --completion [SHELL]  Print completion scripts for `SHELL` and exit\n                            (default: `bash`).\n\n    ------------------------------ COMPLEX.PY ENGLISH ------------------------------\n\n    usage: complex.py english [-h] name\n\n    The `complex.py english` command says hello in English.\n\n    positional arguments:\n      name        The person to say hello to.\n\n    options:\n      -h, --help  Show this help message and exit.\n\n    ------------------------------ COMPLEX.PY SPANISH ------------------------------\n\n    usage: complex.py spanish [-h] name\n\n    The `complex.py spanish` command says hello in Spanish.\n\n    positional arguments:\n      name        The person to say hello to.\n\n    options:\n      -h, --help  Show this help message and exit.\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Command line interface framework",
    "version": "1.0.6",
    "project_urls": {
        "Homepage": "https://github.com/russellane/libcli"
    },
    "split_keywords": [
        "argparse",
        " cli",
        " color",
        " markdown",
        " python"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c256c932c500332b21955ca157ce38853c19f016889db785a7751033e8abb292",
                "md5": "1967528741c3ba99123d89b87478c1d8",
                "sha256": "62fe3753777aa87aa2fadcd4b4927717e2efa2e6f43dfe05db1e212036235b0c"
            },
            "downloads": -1,
            "filename": "rlane_libcli-1.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1967528741c3ba99123d89b87478c1d8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 21206,
            "upload_time": "2024-10-26T02:34:09",
            "upload_time_iso_8601": "2024-10-26T02:34:09.441528Z",
            "url": "https://files.pythonhosted.org/packages/c2/56/c932c500332b21955ca157ce38853c19f016889db785a7751033e8abb292/rlane_libcli-1.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "af4c3a6a1900f9e86f352d79f58fd3e7cc7afd434c0efe318f63937dc625563d",
                "md5": "70eefc69411523359ab554193ceddb82",
                "sha256": "21531485eb677c880e953a1d245825233caad3adbb2db6a380b2af9abe2c5be0"
            },
            "downloads": -1,
            "filename": "rlane_libcli-1.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "70eefc69411523359ab554193ceddb82",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 18715,
            "upload_time": "2024-10-26T02:34:10",
            "upload_time_iso_8601": "2024-10-26T02:34:10.499288Z",
            "url": "https://files.pythonhosted.org/packages/af/4c/3a6a1900f9e86f352d79f58fd3e7cc7afd434c0efe318f63937dc625563d/rlane_libcli-1.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-10-26 02:34:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "russellane",
    "github_project": "libcli",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "rlane-libcli"
}
        
Elapsed time: 0.62682s