Skip to main content

A Python library for dynamically building CLI tools from YAML configurations.

Project description

Dynamic CLI Builder

Dynamic CLI Builder is a tool that simplifies the creation of interactive command-line interfaces (CLI) with minimal changes to your Python scripts, you can enable CLI functionality by adding methods to the registry and describing the CLI structure using YAML or JSON.

Features

  • Easy to use
  • Highly customizable
  • Supports multiple command structures
  • Interactive cli
  • custom rules
  • Logging

Installation

To install Dynamic CLI Builder, use the following command:

pip install dynamic-cli-builder

Usage

Here is a simple example to get you started:

Create Actions

Actions are basically function to be executed base on command.
For instance actions.py

def say_hello(name: str, age: int):
    print(f"Hello {name}!, you are {age} years old.")

# Action Registry
ACTIONS = {
    "say_hello": say_hello,
}

you can have multiple function registered

Create yaml or json config

config.yaml

description: "Dynamic CLI Builder Example"
commands:
  - name: say_hello
    description: "Say Hello..."
    args:
      - name: name
        type: str
        help: "Name of the user."
    action: say_hello
{
	"description": "Dynamic CLI JSON",
	"commands": [
		{
			"name": "say_hello",
			"description": "Say hello...",
			"args": [
				{
					"name": "name",
					"type": "str",
					"help": "Name of the User.",
					"rules": ""
				}
			],
			"action": "say_hello"
		}
	]
}

Add rules for custom validation

  • min, max validation
description: "Dynamic CLI Builder Example"
commands:
  - name: say_hello
    description: "Say Hello..."
    args:
      - name: name
        type: str
        help: "Name of the user."
        rules: ""
      - name: age
        type: int
        help: "Age of the user."
        rules:
          min: 1
          max: 99
    action: say_hello

In Json:

{
	"description": "Dynamic CLI JSON",
	"commands": [
		{
			"name": "say_hello",
			"description": "Say hello...",
			"args": [
				{
					"name": "name",
					"type": "str",
					"help": "Name of the User.",
					"rules": ""
				},
				{
					"name": "age",
					"type": "str",
					"help": "Age of the User.",
					"rules": {
						"min": 1,
						"max": 10
					}
				}
			],
			"action": "say_hello"
		}
	]
}
  • for more control, you could also use regex
description: "Dynamic CLI Builder Example"
commands:
  - name: say_hello
    description: "Say Hello..."
    args:
      - name: name
        type: str
        help: "Name of the user."
        rules: ""
        required: True
      - name: age
        type: int
        help: "Age of the user."
        rules:
          regex: "^[1-9][0-9]$"
        required: True
    action: say_hello

or json equivalent

{
    "description": "Dynamic CLI JSON",
    "commands": [
        {
            "name": "say_hello",
            "description": "Say hello...",
            "args": [
                {
                    "name": "name",
                    "type": "str",
                    "help": "Name of the User.",
                    "rules": "",
                    "required": true
                },
                {
                    "name": "age",
                    "type": "str",
                    "help": "Age of the User.",
                    "required": true
                    "rules": {
                        "regex": "^[1-9][0-9]$"
                    }
                }
            ],
            "action": "say_hello"

        }
    ]
}

Main file main.py

To bind this all together

from dynamic_cli_builder import run_builder
from actions import ACTIONS

run_builder('config.yaml', ACTIONS)

CLI Command

Global Help
python3 <name_of_main_file> -h

For Instance:

python3 main.py -h

command specific help

 python3 <name_of_main_file> <name_of_command> -h

For Instance:

python3 main.py say_hello --name world --age 99

You should see

Hello World!, you are 99 years old

Logging Mode

logging is set to false by default, to enable logging add -log to your command just after the file name

python3 main.py -log say_hello --name world --age 99

Output:

2025-01-29 12:08:19,518 - INFO - Building CLI with config.

2025-01-29 12:08:19,532 - INFO - Executing command: say_hello

Hello World!, you are 99 years old.

Interactive Mode

Interactive mode is set to false by default to enable interactive mode, add -im to your command For instance:

python3 main.py -im say_hello --name world --age 99

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

dynamic_cli_builder-0.1.24.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dynamic_cli_builder-0.1.24-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file dynamic_cli_builder-0.1.24.tar.gz.

File metadata

  • Download URL: dynamic_cli_builder-0.1.24.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for dynamic_cli_builder-0.1.24.tar.gz
Algorithm Hash digest
SHA256 315ebe74a7054a7fbdbb339883bc4e60a1cfca7aba761fb1e032af2169f69dd1
MD5 2d36ddd838af818d72a6bde0b4a611f0
BLAKE2b-256 dd1cfdc30856a30ae131a8b2c93d01d9523ceaa21d55727d8793e022b4d5db5d

See more details on using hashes here.

File details

Details for the file dynamic_cli_builder-0.1.24-py3-none-any.whl.

File metadata

File hashes

Hashes for dynamic_cli_builder-0.1.24-py3-none-any.whl
Algorithm Hash digest
SHA256 f7957b4d4ab9941f8f78414d5cfd21571de980e6a9892ec1ddfe81f8fcf285d1
MD5 16c2e70b44d710b7bcd37f1d3664c4a3
BLAKE2b-256 349ee11768a135254c5900ac5b0e6a47098c99a5e536f33b3a22a9ca2ee031e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page