Skip to content
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

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

Closed
Geo5 opened this issue Sep 30, 2024 · 5 comments · Fixed by #436
Closed

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

Geo5 opened this issue Sep 30, 2024 · 5 comments · Fixed by #436
Assignees

Comments

@Geo5
Copy link

Geo5 commented Sep 30, 2024

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
Copy link
Member

@kschwab Please take a look when you have time.

@kschwab
Copy link
Contributor

kschwab commented Oct 3, 2024

@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
Copy link
Author

Geo5 commented Oct 4, 2024

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
Copy link
Contributor

kschwab commented Oct 6, 2024

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
Copy link
Author

Geo5 commented Oct 7, 2024

Looks great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants