Skip to main content

Build Config classes that can be quickly turned into CLI specifications

Project description

DocCli

Python 3.6+ utility to build Classes that can be easily modified to create a Python Argparse specification. The goal of this is to couple a CLI specification to a config class, making them quicker to build and less likely to break.

This leans heavily on the Decli library to generate Argparse objects.

Creating CLI Objects

CLI objects can be created automatically from a class definition, reading in default values and descriptions from type hints and the docstring.

from doccli import DocCliParser

class CliTool:
    command_name = "cli-tool"
    def __init__(self, _non_cli_param: str, param_a: str, param_b, param_c: int = 5):
        """This is the command description

        Args:
            _non_cli_param (str): Underscore leading params aren't included in the
                                  CLI specs
            param_a: A required parameter
            param_b: Note that the type needs to come from the annotation
            param_c: This one has a default
        """

        self.param_a = param_a
        self.param_b = param_b
        self.param_c = param_c

        self.non_cli_param = non_cli_param

# To create the argparse object:
if __name__ == "__main__":
    args = DocliParser(CliTool).parse_args()
    ...

See examples for more examples, including how to create CLIs with subcommands.

Config File Helpers

DocCli also provides the ConfigUtil class which can be used to automatically create Yaml config files. These include the ability to:

  • Infer config specifications from the __init__ method
  • Convert an instantiated object into a dictionary
    • This will ignore config values that have the same value as their default
  • Ability to nest Config objects in other objects, to create centralised config files
    • A sub-config object can still be instantiated from the super YAML file

Objects inheriting from the ConfigUtil class can set the following class level variables:

  • config_key: str
    • Parameters for this object. Defaults to the class name
  • sub_config_list: List
    • A list of Config Classes that also inherit from ConfigUtil, and are children of this class
  • flatten_sub_configs: bool
    • Defaults to True. When reading from/writing to a dict, sub_configs will either be recorded as sub-dictionaries, or at the same level as the config items for the current dictionary.

Using them together

These tools can be used together to create a config class that can:

  • Generate a CLI parser
  • Initiate from a CLI parser, while also filling in unsupplied values from a Config file
  • Write supplied values to a config file so they can be remembered later

DocliParser has the method parse_args_with_config_file, which will attempt to fill in any unprovided arguments with values provided in the Config File. Note that this will only work if:

  • The variables are stored as top-level keys in the file
  • The variables are stored in a sub-dictionary under the same key as the subcommand name
  • If the subcommand class inherits from ConfigUtil and the variables are stored in a sub-dictionary under the same key as the ConfigUtil.config_key variable

An example of this can be seen in examples

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

doc-cli-0.0.4.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

doc_cli-0.0.4-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file doc-cli-0.0.4.tar.gz.

File metadata

  • Download URL: doc-cli-0.0.4.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0b1 CPython/3.7.4 Linux/4.4.0-18362-Microsoft

File hashes

Hashes for doc-cli-0.0.4.tar.gz
Algorithm Hash digest
SHA256 e558f08ff2538433b68d920f9a5a786f80873975603784f4dc3f0dfdac2d30ab
MD5 b260bb5ebe6b9459b006bedfef5ace18
BLAKE2b-256 c0754d359fc90d9732817de4d27e8f1f3751ddd878fbebd27631403db5ebe6d7

See more details on using hashes here.

File details

Details for the file doc_cli-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: doc_cli-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.0b1 CPython/3.7.4 Linux/4.4.0-18362-Microsoft

File hashes

Hashes for doc_cli-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 dd72179249e6a156ae8f9d96dddf71dfcdd86a750469518a604e8e885f46bbd4
MD5 b1d513be37a912e9e50d5ddf314a4e8f
BLAKE2b-256 bd58ca1981cd841e132cec8ea7cedcb0a16526cbe798aeb43fceea2e59ca9c9a

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