lebrice / SimpleParsing

Simple, Elegant, Typed Argument Parsing with argparse
MIT License
399 stars 49 forks source link

argparse.SUPPRESS-like functionality for advanced uses #124

Closed idoby closed 2 years ago

lebrice commented 2 years ago

Interesting! Can you give me an example use-case for this? And how is this different than just parsing the class and then using dataclasses.asdict on the result?

idoby commented 2 years ago

The use case is somewhat esoteric, I admit, but this is a feature that is available in argparse (and therefore SP) add_argument, so now it can be used with add_arguments as well.

I'm combining configs from multiple sources, so I need to know exactly what was specified on the command line, since the command line is neither the top priority or lowest priority source I have. This way I can enable specifying each part of the dataclass from a different source and assemble the DC at the end of the entire process.

It sounds complicated but it makes keeping track of my ML experiments super easy.

idoby commented 2 years ago

(It also affects whether the config MUST be specified on the cmdline, help text etc)

idoby commented 2 years ago

I want to test it a little bit more with nested DCs before merging this

idoby commented 2 years ago

OK seems to work fine. @lebrice I wrote a small hack when dealing with the boolean test cases there, is there a better way to accomplish this?

idoby commented 2 years ago

@lebrice I'm pretty happy with this now, I'd appreciate your review.

idoby commented 2 years ago

add a suppressed dataclass using add_arguments, as well as another dataclass with a non-suppressed default using add_arguments.

a test for conflicts between two such argument groups (i.e. doing parser.add_arguments(SomeClass, "foo", default=argparse.SUPPRESS), followed by parser.add_arguments(SomeClass, "bar") .

Rebased onto master and added these tests. No bugs seem to have emerged this time.

idoby commented 2 years ago

We can have a zoom call and I'll show you :)