mpkocher/pydantic-cli

Option parser mixin patterns

xkortex opened this issue · 0 comments

I've been want for something that lets me reuse flags across multiple subcommand, between projects, etc. The native argparse makes this a little tricky, especially if you want custom pydantic data classes. I have also wanted a click-like ease of experience but without all the decorators and deep stack traces clouding my logs.

I spent a bit of time hacking on the idea and came up with some interesting patterns. There's still a lot to iron out, a lot of the ergonomics could be improved with more introspection, but I thought I would at least throw this over the wall and see if there's any interest in it.

The overall concept is leveraging metaprogramming to create type annotations which double as both type hints and the option parsing metadata. This is by no means the best way of doing it but I think there's promise here in the general idea.

If this doesn't fit with the vibe of this project, I might spin it out into its own thing, but figure I'd check in here and avoid fragmentation if possible. As you mention, argparse's api is a bit thorny, so this tries to sidestep some of that.

This is a suuuuper rough sketch:

https://gist.github.com/xkortex/c59058f9245104d54b4075ef96692554