Turn a Function Into a CLI
The core move: larzcli turns a plain Python function into a full command-line tool - arguments, help text and config layering - from its signature and type hints.
The heart of building a CLI is parsing arguments and generating help - traditionally tedious boilerplate. larzcli removes it: point it at a function, and it derives the command-line arguments from the function's signature and type hints, generates help text, and even layers configuration (CLI flags over environment variables over defaults). You write the logic; larzcli makes it a proper CLI.
A function becomes a command
Your function's parameters become CLI arguments and options; its type hints become validation; its docstring becomes help.
from larzcli import command
@command
def greet(name: str, times: int = 1, shout: bool = False):
"""Greet someone."""
msg = f'Hello, {name}!'
print((msg.upper() if shout else msg) + '\n' * 0)
# now from the terminal:
# greet Ada --times 3 --shout| From the function | larzcli makes |
|---|---|
| Parameter name | An argument / --option |
| Type hint (int, bool...) | Parsing + validation |
| Default value | Optional flag with that default |
| Docstring | The --help text |
You don't hand-write argument parsers or help - the function IS the specification, and larzcli reads it.
Config that layers
Real tools take settings from several places. larzcli layers them in the sensible order so a flag always wins over an env var, which wins over a default.
Build the command
- @command - a function becomes a CLI from its signature.
- Type hints - free parsing and validation.
- Config layering - CLI > env > default, built in.
- Subcommands + testable - build multi-command apps; parse()/invoke() for tests.
๐ฏ Key takeaways
- larzcli turns a plain function into a CLI - arguments from the signature, validation from type hints, help from the docstring.
- You write logic, not argument parsers - the function is the specification.
- It layers config the 12-factor way: CLI flag > environment variable > default.
- It supports subcommands and is testable (parse()/invoke() don't call sys.exit).
Frequently asked
How is this different from argparse or click?
argparse is powerful but verbose - you manually declare every argument, type and help string, which is a lot of boilerplate for simple tools. click reduces that with decorators but still has you annotate arguments explicitly. larzcli goes further by deriving the CLI directly from your function's existing signature, type hints and docstring, so a normal function becomes a command with almost no extra code, and it adds config layering (CLI > env > default) built in. It's also zero-dependency, unlike click. The philosophy is that your function already describes its inputs, so you shouldn't have to repeat that in argument-parser boilerplate.
Can I build a tool with multiple subcommands, like git?
Yes - larzcli supports grouping multiple commands into one app with subcommands (the 'tool subcommand --options' pattern you see in git, docker, etc.), while still deriving each subcommand from its function. So you can build anything from a single-purpose command to a multi-command application. And because its parse()/invoke() don't call sys.exit, you can unit-test each command's behaviour directly, which is often awkward with other CLI frameworks. Start with one command, and add subcommands as your tool grows into a small application.
Related lessons
Why Command-Line Tools Are a Superpower
CLIs are unglamorous and wildly underrated. A small script you run from the terminal can aโฆ
Build CLI Tools with LarzBeautiful Terminal Output
A tool that prints clear, well-formatted output feels professional and is easier to use. lโฆ
Build CLI Tools with LarzPackage & Ship Your CLI
A tool only helps others if they can install it. Packaging your CLI so it installs with piโฆ