diff --git a/simple_parsing/help_formatter.py b/simple_parsing/help_formatter.py index 5e5e3811..2dcdb290 100644 --- a/simple_parsing/help_formatter.py +++ b/simple_parsing/help_formatter.py @@ -79,7 +79,12 @@ def _get_metavar_for_type(self, t: type) -> str: return get_metavar(t) or str(t) def _get_help_string(self, action: Action) -> Optional[str]: - help = super()._get_help_string(action=action) + if action.required and action.default is None: + # A required argument with no default has no default to show. argparse's + # ArgumentDefaultsHelpFormatter would still append "(default: None)". + help = argparse.HelpFormatter._get_help_string(self, action=action) + else: + help = super()._get_help_string(action=action) if help is not None: help = help.replace(TEMPORARY_TOKEN, "") return help diff --git a/test/test_help_required_default.py b/test/test_help_required_default.py new file mode 100644 index 00000000..13b4851e --- /dev/null +++ b/test/test_help_required_default.py @@ -0,0 +1,19 @@ +from dataclasses import dataclass + +from simple_parsing import ArgumentParser + + +@dataclass +class Options: + list_items: list[str] # SOMETHING + + +def test_required_field_without_default_does_not_show_default_none(capsys): + parser = ArgumentParser(add_option_string_dash_variants=True) + parser.add_arguments(Options, dest="options") + + parser.print_help() + help_text = capsys.readouterr().out + + assert "SOMETHING" in help_text + assert "(default: None)" not in help_text diff --git a/test/test_issue_48.py b/test/test_issue_48.py index 904e9996..f164b39b 100644 --- a/test/test_issue_48.py +++ b/test/test_issue_48.py @@ -39,11 +39,11 @@ def test_issue_48(): -s str, --start_date str Start date from which to collect data about base users. Input in iso format (YYYY-MM-DD). The date is - included in the data (default: None) + included in the data -e str, --end_date str End date for collecting base users. Input in iso format (YYYY-MM-DD). The date is included in the data. - Should not be before `start_date` (default: None) + Should not be before `start_date` """ ), )