Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion simple_parsing/help_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/test_help_required_default.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/test_issue_48.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
"""
),
)
Expand Down