SimpleParsing
SimpleParsing copied to clipboard
Add support for optional nested parameter groups
Is your feature request related to a problem? Please describe.
I would like to have a set of parameters that are optional, but if you give one of the set, you must follow the rules for the rest of them. For example, the following code doesn't work - you get asked to provide an input of type B
which is impossible from the CLI.
from simple_parsing import ArgumentParser
from dataclasses import dataclass
from typing import *
@dataclass
class A:
a: str
b: str
@dataclass
class B:
a: str
b: str
@dataclass
class MyCli:
A: A
B: Optional[B]
parser = ArgumentParser()
parser.add_arguments(MyCli, dest="args")
args = parser.parse_args()
Gives help output:
usage: scratch_1.py [-h] [-B B] -a str -b str
scratch_1.py: error: the following arguments are required: -a/--a, -b/--b
Describe the solution you'd like
In the above example, I would like A (and all it's sub-parameters) to be mandatory. And I would like B to be optional; but if B.a
is given, then B.b
is also mandatory.
Another way to explain this is that B is a feature flag, where if that flag is given then you must also provide a set of parameters for that feature.
Describe alternatives you've considered
Alternative is that I make all parameters of B optional, and validate myself in a __post_init__
or similar to re-create the behaviour of all being mandatory if any one is given. But it seems that SimpleParsing is the right place to solve this, rather than require the user to do such a workaround.
Thanks for posting this! Indeed, I agree, and I have also been thinking about this. I'll get back to you in the next few days.
Thanks again!
Hi, great library, thanks ! I seem to be meeting the same issue. Has this been solved with the mentioned MR ? Thanks.
Hi @jhagege , Yes this was partially fixed by #34, in that optional parameter groups should work, as long as you aren't nesting them more than one level down the "tree".
See this test here for an example.