Clize
Clize is a Python library that simplifies the creation of command-line interfaces (CLIs) by directly transforming Python functions into CLIs. It automatically infers CLI parameters from function signatures and generates comprehensive `--help` messages from docstrings. The library is actively maintained, with the current stable version being 5.0.2.
Warnings
- breaking The `bytes` annotation (converter) now re-encodes the corresponding parameter back to bytes, changing its behavior from previous versions.
- deprecated The `convert_default` argument to `clize.parser.value_converter` is deprecated. Its functionality should now be handled via `clize.Parameter.cli_default`.
- gotcha Using mutable objects (e.g., lists, dictionaries) as default arguments in Python functions passed to `clize.run` can lead to unexpected shared state. Python evaluates default arguments once when the function is defined, not on each call.
- gotcha Prior to v5.0.1, using a dictionary in the `alt=` parameter of `clize.run` did not work correctly.
Install
-
pip install clize
Imports
- run
from clize import run
- Parameter
from clize import Parameter
Quickstart
from clize import run
def hello(name: str = 'world', *, capitalize: bool = False):
"""Greets the world or a specific person.
:param name: The name to greet.
:param capitalize: Whether to capitalize the name.
"""
if capitalize:
name = name.title()
print(f"Hello, {name}!")
if __name__ == '__main__':
run(hello)