Skip to content

Better help message with optional dataclasses #368

@bilelomrani1

Description

@bilelomrani1

In the following example

from dataclasses import dataclass
from typing import Optional

from jsonargparse import ArgumentParser


@dataclass
class Config:
    a: int
    b: float = 2.0


if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("--config", type=Optional[Config], default=None, required=False)
    print(parser.parse_args())

the help message looks like this:

$ python main.py --help
usage: main.py [-h] [--config CONFIG]

options:
  -h, --help       Show this help message and exit.
  --config CONFIG  (type: Optional[Config], default: null)

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?

$ python main.py --help
usage: main.py [-h] [--config CONFIG] --config.a A [--config.b B]

options:
  -h, --help       Show this help message and exit.

Config docstring (optional):
  --config CONFIG  Path to a configuration file.
  --config.a A     (required, type: int)
  --config.b B     (type: float, default: 2.0)

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions