3 ms·
I've tried just about every python cli parser out there: argparse, click, Cleo, docopt, fire, hydra, typer, a few others. Nothing has been a slam dunk for me th
by kortex 5y ago
I've tried just about every python cli parser out there: argparse, click, Cleo, docopt, fire, hydra, typer, a few others. Nothing has been a slam dunk for me the way Clap for rust or cobra for go have been. But I've found a good enough system.
My criteria are: well supported, composable, sub commands, reusable options, sane stack traces.
My current pattern: Cleo plus pydantic. I can create a module of commonly used flags and options that I can share among commands. Commands are first class objects. I can use them as entry points. I can readily parse the options into a static structure with defaults read from env or a file using pydantic. This is feed to runner/API functions, with minimal logic in the command class itself. The one thing that is lacking is I wish I could define pydantic models that double as commands. That is a pretty specific nit to pick.
Overall I love it. Poetry runs on it so I think it's got a long support life. It's beautiful. Easy to use. Just the right amount of opinionated for me. Supports auto complete. Built in commonly used flags like version, quiet, and verbose levels.
I find click and any of the decorator based systems super obtuse and difficult to compose. They add tons of noise to stack traces.
Docopt is way too string based. I like static types.
Argparse is... surprisingly not bad. The main drawback is reuse is not very easy and it's hard to write composable structures.