afterclick
728 × 90

Engineering

commandr: TellApart's decorator-based CLI builder for Python

from commandr import command, Run

@command('greet')
def SayGreeting(name, title='Mr.', times=1, caps_lock=False):
    """Greets someone by name."""
    greeting = 'Hi %s %s!' % (title, name)
    if caps_lock:
        greeting = greeting.upper()
    for _ in range(times):
        print(greeting)

if __name__ == '__main__':
    Run()

Run python example.py greet --name=John and the script prints Hi Mr. John!. That was the whole pitch of commandr, a small library TellApart’s engineers released as open source on GitHub and PyPI. No parser objects, no option lists to keep in sync with the function, no boilerplate. Write the function, add the decorator, call Run().

Why commandr existed

Kevin Ballard’s announcement opened with a complaint most Python teams of the time would have recognised. Every codebase collects scripts, and TellApart’s had plenty of them, each needing a command-line interface written with optparse. Writing those specifications was tedious, and the result duplicated what the function signature already said: which arguments exist, which are optional, what the defaults are.

commandr removed the duplication by reading the signature itself. The features the engineers listed follow from that one idea:

  • Types are inferred from default values and arguments are cast to match, so --times=3 arrives as an integer because the default is 1.
  • Help text is generated from the signature and the function’s docstring.
  • Boolean parameters become flags, so a caps argument gets --caps and --no_caps.
  • Commands can be grouped, which keeps a long list of subcommands readable.

The approach is sound, and it was early. Python 2 had no function annotations, so default values were the only type hint available, and commandr made the most of them.

The internal trick that never shipped

One detail from the announcement deserves more attention than it got. Inside TellApart, the engineers had combined commandr with Fabric, the SSH automation library, so that the same decorated function could be called from a developer’s laptop and executed on remote servers. One decorator gave you a local command; another made it a remote operation across a fleet.

They did not release that part, explaining that it was too tightly tied to the rest of their stack. It is the more interesting half of the idea. Today the same pattern shows up as typer commands wrapped around Ansible, Fabric 2 or a cloud provider’s SDK, but rarely with the same economy.

How commandr compares with argparse, click, typer and fire

The Python command-line world has moved a long way since commandr was written. Here is where each option sits.

Tool How the CLI is defined Type handling Best fit
argparse Explicit parser objects, standard library Declared per argument Scripts that must avoid dependencies
click Decorators per option and argument Declared per option Large CLIs with nested groups
typer Function signature with type hints Read from annotations New Python 3 tools
fire Any function, class or object Guessed from input values Quick internal tools, exploration
commandr Function signature plus one decorator Inferred from defaults Python 2 era scripts

argparse replaced optparse in the standard library with Python 2.7 and 3.2, and it remains the right choice when a script must run with nothing installed. Click, from the Pallets project, is the workhorse behind countless tools; it is verbose but precise. Fire, released by Google in 2017, goes furthest: it turns any object into a CLI without even a decorator, at the cost of loose typing.

Typer is the true successor to commandr’s idea. It reads the signature, but with real type annotations instead of default values, and it builds on click underneath. The same greeting looks like this:

import typer

app = typer.Typer()

@app.command()
def greet(name: str, title: str = "Mr.", times: int = 1, caps_lock: bool = False):
    """Greets someone by name."""
    greeting = f"Hi {title} {name}!"
    if caps_lock:
        greeting = greeting.upper()
    for _ in range(times):
        typer.echo(greeting)

if __name__ == "__main__":
    app()

Here name has no default, so typer makes it a positional argument, and caps_lock becomes the flag pair --caps-lock and --no-caps-lock. Apart from the hyphens, that is the behaviour commandr offered years earlier.

Under the hood the mapping was simple, which was the appeal. commandr read the function’s argument names and defaults with Python’s introspection, turned every argument into a --name option, inferred booleans from a False or True default, and passed the parsed values straight back into the call. Nothing about the command line lived anywhere except the function itself, so renaming a parameter renamed the flag, and adding a default made an option optional. For an internal tool that a handful of engineers ran from a terminal, that was exactly the right amount of ceremony.

What a team would pick now

For a new tool in 2026, use typer unless you have a reason not to. You get commandr’s signature-first approach, editor support from the type hints, shell completion and click’s ecosystem if you outgrow the defaults. Use plain argparse for single-file scripts that ship without dependencies, and keep fire for throwaway exploration.

commandr itself belongs to the Python 2 period, the same years in which TellApart’s engineers were pushing gevent and Python into high-throughput services. If you inherit a codebase that uses it, check that it runs on your interpreter before building more on top of it. The same impulse to remove launcher boilerplate also produced the company’s MrJerb launcher for mrjob.

The question commandr raised has never quite been settled: should a function’s signature be the only source of truth for its command line, or does a public CLI deserve its own explicit, reviewed definition?

Questions readers ask

How did commandr know an argument's type without annotations?

Python 2 had no function annotations, so commandr inferred types from default values and cast arguments to match. A parameter with a default of 1 arrived as an integer when passed as --times=3, and a False or True default turned it into a flag pair such as --caps and --no_caps. Help text came from the signature and the docstring, so the command line lived only in the function.

What did TellApart keep private when releasing commandr?

Internally, the engineers combined commandr with Fabric, the SSH automation library, so the same decorated function could run on a developer's laptop or execute on remote servers across a fleet. One decorator produced a local command and another turned it into a remote operation. They did not release that part because it was too tightly tied to the rest of their stack.

Should a new Python tool use commandr or typer?

Typer, in most cases. It follows the same signature-first idea but reads real type annotations, builds on click and adds editor support and shell completion. commandr belongs to the Python 2 period, so check that it runs on your interpreter before building on it. Plain argparse suits single-file scripts that must ship without dependencies, and fire is fine for throwaway exploration.

What happens to the command line when you rename a parameter in commandr?

The flag changes with it. commandr used Python's introspection to read a function's argument names and defaults, turned each argument into a --name option and passed the parsed values straight back into the call. Since nothing about the interface was stored anywhere else, renaming a parameter renamed its flag and giving a parameter a default made the option optional. For internal tools run by a handful of engineers, that was the right amount of ceremony.