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

Open
zrlf opened this issue Jan 9, 2025 · 1 comment
Open

Pass custom HelpFormatter to subparsers #336

zrlf opened this issue Jan 9, 2025 · 1 comment

Comments

@zrlf
Copy link

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
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)

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

No branches or pull requests

1 participant