A quick review of `tyro`, a CLI library.
Posted by HommeMusical@reddit | Python | View on Reddit | 28 comments
I recently discovered https://brentyi.github.io/tyro/
I've used `typer` for many years, so much that I wrote a band-aid project to fix up some of its feature deficiencies: https://pypi.org/project/dtyper/
I never used `click` but it apparently provides a full-featured CLI platform. `typer` was written on top of `click` to use Python type annotations on functions to automatically create the CLI. And it was a revolution when it came out - it made so much sense to use the same mechanism for both purposes.
However, the fact that `typer` built around a function call means that the state that it delivers to you is a lot of parameters in a flat scope.
Many real-world CLIs have dozens or even hundreds of parameters that can be set from the command line, so this rapidly becomes unwieldy.
My `dtyper` helped a bit by allowing you to use a `dataclass`, and fixed a couple of other issues, but it was artificial, worked only on `dataclass` and none of the other data class types, and had only one level, and was incorrectly typed. (It spun off work I was doing elsewhere, it was very useful to me at the time.)
`tyro` seems to fix all of the issues. It lets you use functions, almost any sort of data class, nested data classes, even constructors to automatically build a CLI.
So far my one complaint is that the simplest possible CLI, [a command that takes zero or more filenames, is obscure](https://github.com/brentyi/tyro/issues/439).
But I found the way to do it neatly, it's more a documentation issue.
Looking at some of my old projects, there would have been whole chunks of code which would never have been written, passing command line flags down to sub-objects. (No, I won't rewrite them, they work fine.)
Verdict: so far so good. If it continues to work as advertised I'll probably use it in new development.
28 Comments
AutoModerator@reddit
Erelde@reddit
Erelde@reddit
brentyi@reddit
Erelde@reddit
brentyi@reddit
Erelde@reddit
HommeMusical@reddit (OP)
Erelde@reddit
HommeMusical@reddit (OP)
Erelde@reddit
HommeMusical@reddit (OP)
HommeMusical@reddit (OP)
SSJ3@reddit
shatGippity@reddit
brentyi@reddit
HommeMusical@reddit (OP)
DanCardin@reddit
PresentBasket8994@reddit
AndydeCleyre@reddit
Klutzy_Bird_7802@reddit
HommeMusical@reddit (OP)
Broolucks@reddit
HommeMusical@reddit (OP)
Broolucks@reddit
Klutzy_Bird_7802@reddit
doolio_@reddit
just4nothing@reddit