-
-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better help message with optional dataclasses #368
Comments
@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:
In the first case it could be an argument For the second case, for certain it would be a |
Thank you @mauvilsa, great idea, adding an argument seems very reasonable to me indeed. |
Here is the solution from dataclasses import dataclass
from jsonargparse import ArgumentParser
@dataclass
class Config:
a: int
b: float = 2.0
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 (required, type: int)
--config.b B (type: float, default: 2.0) To make meaningful help messages use docstrings as shown below from dataclasses import dataclass
from jsonargparse import ArgumentParser
from jsonargparse import set_docstring_parse_options
set_docstring_parse_options(attribute_docstrings=True)
@dataclass
class Config:
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) |
In the following example
the help message looks like this:
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?
The goal is to give the user more clues about what's expected but still yield
Namespace(config=None)
when nothing is passed from the CLI.Thank you for your help!
The text was updated successfully, but these errors were encountered: