Skip to main content

DocCli

Python 3.10+ 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 sid-doc-cli 0.0.5

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

Source distribution (sdist)

Source distribution for sid-doc-cli 0.0.5
File Size Uploaded
sid_doc_cli-0.0.5.tar.gz 9.5 kB Details

Built distribution (wheel)

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

Total release size: 20.3 kB

Release files / sid_doc_cli-0.0.5.tar.gz

Download URL sid_doc_cli-0.0.5.tar.gz
Size 9.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0210be7ddf566cfa31623632525e14389237b3ea0e47a0331f33cbe92172955a
BLAKE2b-256 checksum
How to use checksums
98cda34cd740313fe8763cd744a477ebbaf02947569ac7a645ee1ec8811b6f50
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / sid_doc_cli-0.0.5-py3-none-any.whl

Download URL sid_doc_cli-0.0.5-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cb3c94c26945009aee15b94a3afdfdbecf5e5b317e524b38511ba92e7c257195
BLAKE2b-256 checksum
How to use checksums
d68081ef2e650fa3b800976897dce2bd72df2d567b1e73d968d76c5370aa0fff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

0.0.5 This release

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