โŒจ๏ธ
โŒจ๏ธ Build CLI Tools with Larz ยท LARZ-801 ยท Session 2 of 5

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.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

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.

๐Ÿ“Œ Worked example
From boilerplate to one decorator. The old way to make a script into a CLI meant dozens of lines of argument-parser setup - declaring each argument, its type, its help text - before writing any actual logic. With larzcli you write the function that does the work, add @command, and it reads the arguments, types and help straight from your function's signature and docstring. A useful tool that used to be buried under parsing boilerplate becomes just the logic plus one line - so you build (and share) small tools far more often, because the friction is gone.

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 functionlarzcli makes
Parameter nameAn argument / --option
Type hint (int, bool...)Parsing + validation
Default valueOptional flag with that default
DocstringThe --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.

๐ŸŽฏ
CLI flag > environment variable > default. A good tool lets a user set something via a flag for one run, or an environment variable for their whole session, or fall back to a sensible default - and the more specific always wins. larzcli builds this 12-factor-style layering in, so your tool behaves the way users expect without you wiring up the precedence by hand. And it's testable: parse()/invoke() don't call sys.exit, so you can unit-test your CLI.

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.

Session 2 of 5 complete

Next up: Beautiful Terminal Output

Continue to Session 3 โ†’

Related lessons

More in this course

Course home → · Browse all courses →