Decli: Declarative CLI Tool

0.6.3 · active · verified Fri Apr 10

Decli is a minimal wrapper around Python's `argparse` module, designed to simplify the creation of command-line interfaces (CLIs) using a declarative approach. It allows developers to define arguments, subcommands, and groups through Python dictionaries, promoting a clear separation between CLI definition and application logic. The library aims to reduce boilerplate commonly associated with `argparse`, making it easier to build complex CLIs. The current version is 0.6.3, and it is actively maintained.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic CLI with a subcommand and an optional global argument using `decli`. The `cli` function takes a dictionary specifying the program's structure, arguments, and command functions. The example shows how to parse arguments and execute the associated function based on simulated command-line input.

from decli import cli

data = {
    "prog": "mycli",
    "description": "A simple command-line tool.",
    "arguments": [
        {
            "name": "--verbose",
            "action": "store_true",
            "help": "Enable verbose output"
        }
    ],
    "commands": [
        {
            "name": "greet",
            "description": "Greets the specified person.",
            "arguments": [
                {
                    "name": "name",
                    "help": "The name to greet"
                }
            ],
            "func": lambda args: print(f"Hello, {args.name}!" + (" (verbose)" if args.verbose else ""))
        }
    ]
}

parser = cli(data)
# To simulate command-line input, you can pass a list of strings
# For real execution, use parser.parse_args()
# Example: python mycli greet World --verbose

# Simulate 'greet World --verbose'
args = parser.parse_args(['greet', 'World', '--verbose'])
args.func(args)

# Simulate 'greet Alice'
args = parser.parse_args(['greet', 'Alice'])
args.func(args)

view raw JSON →