pydantic / pydantic-settings

Settings management using pydantic
https://docs.pydantic.dev/latest/usage/pydantic_settings/
MIT License
643 stars 65 forks source link

Support for suppressing fields from the CLI --help message #431

Closed Geo5 closed 1 month ago

Geo5 commented 1 month ago

In stdlib argparse you are able to set the help message to argparse.SUPPRESS to hide the entry from the generated --help output.

Would it be possible to support this in pydantic-settings as well? It seems to work, if you add a

if help==SUPPRESS:
  pass
elif ...

here https://github.com/pydantic/pydantic-settings/blob/main/pydantic_settings/sources.py#L1885 and setting the field description to argparse.SUPPRESS

hramezani commented 1 month ago

@kschwab Please take a look when you have time.

kschwab commented 1 month ago

@Geo5, I presume just like argparse this should be applied on a per field setting? e.g.:

class Settings(BaseSettings):
    field: CliSuppress[int] = Field(description='Hide this field from help')

This is not hard to add, I can put up a PR if the above looks reasonable. Obviously, "field" still gets parsed at the CLI and is only hidden from CLI --help text.

Geo5 commented 1 month ago

Yes exactly, it is only hidden from CLI --help, but still gets parsed. Do i understand the library correctly, that if this would be added via a type parameter (e.g. CliSuppress) it would not be possible to set this dynamically at runtime? The use case is, that i sometime want to show/hide some options depending on the environment (windows/linux, installed somewhere or not, etc), which with arparse would be something like:

parser.add_argument("--foo", help=argparse.SUPPRESS if platform=="linux" else "This is the foo argument")
kschwab commented 1 month ago

That's a great point. I added support for both in #436. e.g.:

class Settings(BaseSettings, use_attribute_docstrings=True):
    field: CliSuppress[int]
    """Hide this field from help"""

    foo: int = Field(description=CLI_SUPPRESS if platform=="linux" else "This is the foo argument")
Geo5 commented 1 month ago

Looks great, thank you!