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

Pass custom HelpFormatter to subparsers #336

Closed
zrlf opened this issue Jan 9, 2025 · 3 comments · Fixed by #339
Closed

Pass custom HelpFormatter to subparsers #336

zrlf opened this issue Jan 9, 2025 · 3 comments · Fixed by #339

Comments

@zrlf
Copy link
Contributor

zrlf commented Jan 9, 2025

When using a custom HelpFormatter, subparsers are still using the default SimpleHelpFormatter.

To Reproduce

from simple_parsing import ArgumentParser
from dataclasses import dataclass

@dataclass
class Sub:
   foo: str

@dataclass
class Foo:
   subcommand: Union[Sub, SomeOther]
   bar: int = 123

if __name__ == "__main__":
   parser = ArgumentParser(formatter_class=CustomHelpFormatter)
   parser.add_arguments(Foo, "foo")
   args = parser.parse_args(["sub", "-h"])

Expected behavior
Any subparser should inherit the formatter.

Actual behavior
Default is used

@zrlf
Copy link
Contributor Author

zrlf commented Jan 9, 2025

Looks like a simple fix. In line 1023, replacing with subparser = subparsers.add_parser(subcommand, formatter_class=parser.formatter_class) fixes this issue.

for subcommand, dataclass_type in self.subparsers_dict.items():
logger.debug(f"adding subparser '{subcommand}' for type {dataclass_type}")
subparser = subparsers.add_parser(subcommand)

@lebrice
Copy link
Owner

lebrice commented Jan 29, 2025

Hey @zrlf , nice catch! Would you like to make a PR? :)

@zrlf
Copy link
Contributor Author

zrlf commented Jan 30, 2025

sure :)

zrlf added a commit to zrlf/SimpleParsing that referenced this issue Jan 30, 2025
lebrice pushed a commit to zrlf/SimpleParsing that referenced this issue Jan 31, 2025
lebrice added a commit that referenced this issue Feb 3, 2025
* Pass custom help formatter to subparsers (#336)

* Fix formatting in field_wrapper.py

Signed-off-by: Fabrice Normandin <[email protected]>

---------

Signed-off-by: Fabrice Normandin <[email protected]>
Co-authored-by: Fabrice Normandin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants