Skip to main content

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

Metadata

Release files for doc-cli 0.0.4

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

Source distribution (sdist)

Source distribution for doc-cli 0.0.4
File Size Uploaded
doc-cli-0.0.4.tar.gz 10.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for doc-cli 0.0.4
File Interpreter ABI Platform
doc_cli-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / doc-cli-0.0.4.tar.gz

Download URL doc-cli-0.0.4.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e558f08ff2538433b68d920f9a5a786f80873975603784f4dc3f0dfdac2d30ab
BLAKE2b-256 checksum
How to use checksums
c0754d359fc90d9732817de4d27e8f1f3751ddd878fbebd27631403db5ebe6d7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0b1 CPython/3.7.4 Linux/4.4.0-18362-Microsoft

Release files / doc_cli-0.0.4-py3-none-any.whl

Download URL doc_cli-0.0.4-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd72179249e6a156ae8f9d96dddf71dfcdd86a750469518a604e8e885f46bbd4
BLAKE2b-256 checksum
How to use checksums
bd58ca1981cd841e132cec8ea7cedcb0a16526cbe798aeb43fceea2e59ca9c9a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.0b1 CPython/3.7.4 Linux/4.4.0-18362-Microsoft

Release history Release notifications | RSS feed

This release

0.0.4 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.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