You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Better help message with optional dataclasses #368
I haven't figured out a way but is there a way to show the dataclasses arguments in the help message (even when the dataclass is optional) and mark the group as optional?
@bilelomrani1 thanks for pointing this out. Certainly there should be a way to get the details of the dataclass. This should be similar to how it is being done for subclasses. Though there are two cases to handle:
Optional dataclasses, e.g. Optional[DataClass]
Mixture of classes, e.g. Union[int, DataClass1, DataClass2, BaseForSubclasses1]
In the first case it could be an argument --*.help that does not expect any value and would print the details for the dataclass. This means that for the example above, people would need to call the CLI as python main.py --config.help. Would be nicer if it were shown in the normal help, though this might be overly complicated to implement, since the parser wouldn't really have these arguments.
For the second case, for certain it would be a --*.help that expects a class name or class import path. So the behavior would be just like it is now for subclasses.
$ python example.py -h
usage: example.py [-h] [--config CONFIG] --config.a A [--config.b B]
options:
-h, --help Show this help message and exit.
Config(a: int, b: float = 2.0):
--config CONFIG Path to a configuration file.
--config.a A (required, type: int)
--config.b B (type: float, default: 2.0)
To make meaningful help messages use docstrings as shown below
fromdataclassesimportdataclassfromjsonargparseimportArgumentParserfromjsonargparseimportset_docstring_parse_optionsset_docstring_parse_options(attribute_docstrings=True)
@dataclassclassConfig:
a: int"""Number of integers."""b: float=2.0"""Number of floats."""if__name__=="__main__":
parser=ArgumentParser()
parser.add_argument("--config", type=Config)
print(parser.parse_args())
$ python example.py -h
usage: example.py [-h] [--config CONFIG] --config.a A [--config.b B]
options:
-h, --help Show this help message and exit.
Config(a: int, b: float = 2.0):
--config CONFIG Path to a configuration file.
--config.a A Number of integers. (required, type: int)
--config.b B Number of floats. (type: float, default: 2.0)
With pull request #783 just merged, the behavior is as proposed in #368 (comment). If there is a single nested dataclass, then there will be a --*.help option that doesn't expect any value. For multiple nested dataclasses, the --*.help option expects a class name or class path.
Activity
mauvilsa commentedon Sep 4, 2023
@bilelomrani1 thanks for pointing this out. Certainly there should be a way to get the details of the dataclass. This should be similar to how it is being done for subclasses. Though there are two cases to handle:
Optional[DataClass]Union[int, DataClass1, DataClass2, BaseForSubclasses1]In the first case it could be an argument
--*.helpthat does not expect any value and would print the details for the dataclass. This means that for the example above, people would need to call the CLI aspython main.py --config.help. Would be nicer if it were shown in the normal help, though this might be overly complicated to implement, since the parser wouldn't really have these arguments.For the second case, for certain it would be a
--*.helpthat expects a class name or class import path. So the behavior would be just like it is now for subclasses.bilelomrani1 commentedon Sep 4, 2023
Thank you @mauvilsa, great idea, adding an argument seems very reasonable to me indeed.
jerrymatjila commentedon Nov 17, 2023
Here is the solution
$ python example.py -h usage: example.py [-h] [--config CONFIG] --config.a A [--config.b B] options: -h, --help Show this help message and exit. Config(a: int, b: float = 2.0): --config CONFIG Path to a configuration file. --config.a A (required, type: int) --config.b B (type: float, default: 2.0)To make meaningful help messages use docstrings as shown below
$ python example.py -h usage: example.py [-h] [--config CONFIG] --config.a A [--config.b B] options: -h, --help Show this help message and exit. Config(a: int, b: float = 2.0): --config CONFIG Path to a configuration file. --config.a A Number of integers. (required, type: int) --config.b B Number of floats. (type: float, default: 2.0)Argument to print help for dataclasses nested in types (#368).
mauvilsa commentedon Oct 9, 2025
With pull request #783 just merged, the behavior is as proposed in #368 (comment). If there is a single nested dataclass, then there will be a
--*.helpoption that doesn't expect any value. For multiple nested dataclasses, the--*.helpoption expects a class name or class path.