Decli: Declarative CLI Tool
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
- gotcha Decli explicitly disallows nesting groups inside exclusive groups and will raise a `ValueError` if attempted. While `argparse` might allow this with broken help messages or non-functional exclusion, `decli` prevents it to maintain integrity.
- gotcha Decli is a wrapper around `argparse`, and while it simplifies definition, users still benefit greatly from understanding core `argparse` concepts (e.g., `action`, `nargs`, `const`, `dest`). Many `decli` dictionary keys directly map to `argparse` parameters, and their behavior is inherited from `argparse`.
Install
-
pip install decli
Imports
- cli
from decli import cli
Quickstart
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)