▲ | MrJohz 3 days ago | |||||||
Actually, I think argparse falls into the same trap that the author is talking about. You can define lots of invariants in the parser, and say that these two arguments can't be passed together, or that this argument, if specified, requires these arguments to also be specified, etc. But the end result is a namespace with a bunch of key-value pairs on it, and argparse doesn't play well with typing systems like mypy or pyright. So the rest of the tool has to assume that the invariants were correctly specified up-front. The result is that you often still this kind of defensive programming, where argparse ensures that an invariant holds, but other functions still check the same invariant later on because they might have been called a different way or just because the developer isn't sure whether everything was checked where they are in the program. What I think the author is looking for is a combination of argparse and Pydantic, such that when you define a parser using argparse, it automatically creates the relevant Pydantic classes that define the type of the parsed arguments. | ||||||||
▲ | bvrmn 3 days ago | parent | next [-] | |||||||
In general case generating CLI options from app models leads to horrible CLI UX. Opposite is also true. Working with "nice" CLI options as direct app models is horrendous. You need a boundary to convert nice opts into nice types. Like pydantic models could take argparse namespace and convert it to something manageable. | ||||||||
| ||||||||
▲ | sgarland 3 days ago | parent | prev | next [-] | |||||||
Precisely my thought. I love argparse, but you can really back yourself into a corner if you aren’t careful. | ||||||||
▲ | js2 3 days ago | parent | prev | next [-] | |||||||
> What I think the author is looking for is a combination of argparse and Pydantic Not quite that, but https://typer.tiangolo.com/ is fully type driven. | ||||||||
▲ | hahn-kev 3 days ago | parent | prev [-] | |||||||
It's almost like you want compile time type safety | ||||||||
|