Skip to main content

A library for setting up cli applications

Project description

clilib

clilib is a python library that does some automatic setup for command-line applications as well as includes various utilities that can be used in many applications. This can be used to rapidly deploy cli applications in python.

Full documentation

Quickstart

To quickly turn your function or class into a CLI application based on its arguments, defaults, annotations, and docstrings, you will need a file that looks something like this:

from clilib.util.logging import Logging
from clilib.builders.app import EasyCLI


class TestCommand:
    """
    A test command class
    """
    def __init__(self, debug: bool = False):
        """
        :param debug: Add additional debugging output.
        """
        self.debug = debug
        self.logger = Logging("TestCommand", debug=debug).get_logger()

    ...


if __name__ == "__main__":
    e = EasyCLI(TestCommand)

For a working example (an application included with the library that uses EasyCLI) check out clilib.util.wheel.

It's that easy! You can now execute this script from the command line, and you'll be able to pass any arguments your class or function requires.

EasyCLI will register each non-private method in a class (sans init) as a subcommand with the same name whereas the init method defines the base arguments. Arguments with defaults are added as flags, arguments without defaults are positionals, and any class within your class is treated as a subcommand (with its own methods being added as subcommands to itself)

EasyCLI requires a docstring in order to populate the help information for your command line application. EasyCLI will fail without the docstring present. This is intentional, because it makes you document your code more consistently :)

With the above out of the way, EasyCLI will produce something similar to the following when passed a class:

usage: testapp.py [-h] [-d] {hello,goodbye} ...

A test command class

optional arguments:
  -h, --help       show this help message and exit
  -d, --debug      Add additional debugging output.

subcommands:
  Available Subcommands

  {hello,goodbye}
    hello          Say hello
    goodbye        Say goodbye

Some EasyCLI option highlights:

  • enable_logging (default False) will enable file logging of the CLI generation (by default, to /var/log/clilib/EasyCLI.log)
  • print_return (default False) will enable printing the return statement of the method your command resolves to.
  • dump_json (default True) will dump a list or dict return value to json before printing it. (only effective if print_return is true)

Notes:

You should keep in mind when using EasyCLI that it is built to provide a simple, quick command line application. Some things to try and remember when using EasyCLI is that you should do your best to adhere to standard python naming conventions (PEP8), particularly lowercase, underscored method names are best for EasyCLI. EasyCLI will replace underscores in subcommand names and argument names with hyphens, and will ignore private class methods (methods whose names start with an underscore).

You may also tell EasyCLI to ignore a method in your class by putting ":easycli_ignore:" in its docstring.

SearchableDict

SearchableDict is a class that works just like a regular dict with the added functionality of being able to get and set values based on simple dot separated pathing.

Example:

>>> from clilib.util.dict import SearchableDict
>>> d = SearchableDict()
>>> d.set_path("foo.bar", "baz")
>>> d.get_path("foo.bar")
'baz'
>>> d.get_path("foo.baz", "None!")
'None!'

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

clilib-3.7.4-py3-none-any.whl (36.8 kB view details)

Uploaded Python 3

File details

Details for the file clilib-3.7.4-py3-none-any.whl.

File metadata

  • Download URL: clilib-3.7.4-py3-none-any.whl
  • Upload date:
  • Size: 36.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for clilib-3.7.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8bd089bd99c21a0036beb885db4dd85a93c6ae0f26c5a745e570ef7f8548b673
MD5 8fbaa812b6edd32e55b5146aa5c24017
BLAKE2b-256 fab610c5d5ea268e77347a6397c21a4427c1e455ce4494b14e8c5dab63d22a58

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