Introducing typer-scion: inherit CLI options in subcommands
TLDR
typer-scion lets typer subcommands use options defined by their parent command. Mark an option with
Inheritable() and decorate subcommands with @inherit to receive it.
The typer library makes it a breeze to build a command-line application from ordinary Python functions. After years of
using it for my own CLI apps, though, one point has caused me a lot of irritation. Because it relies on click, it
doesn't do a great job with "interspersed options." That is, if a parent command takes an option, there's not a good
way to let you pass that option through a subcommand.
For example, I usually have a global verbosity flag to make the CLI produce debug output as it operates. I usually have the main callback function for the app accept this option. That means, to enable verbose output, I have to pass that option to the root command like so:
Many CLI apps you encounter out in the world allow you to pass root-level options after the subcommands, and this
feels quite natural. That's especially true if you have a relatively complex command to re-issue with only the output
verbosity changed. Ideally, you just hit the up-arrow key and add a --verbose to the end of the last command
This doesn't work with typer out of the box, and there hasn't been a good way to do this without:
- hacky workarounds
- duplicating the options on all subcommands hooking back into the parent callback logic
- just giving up and accepting the mild irritation
typer-scion solves the problem in an ergonomic and explicit manner. The parent callback still owns the option, but a
child subcommand can opt into "inheriting" the parent option.





