Skip to content

Commit

Permalink
Don't display defaults if the default value is None.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPugh committed Dec 4, 2023
1 parent 95083cd commit 9c1cf7d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions cyclopts/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SilentRich:
"""Dummy object that causes nothing to be printed."""

def __rich_console__(self, console, options):
# This generator yields nothing, so 'rich' will print nothing for this object.
# This generator yields nothing, so ``rich`` will print nothing for this object.
if False:
yield

Expand Down Expand Up @@ -251,7 +251,9 @@ def is_short(s):
if choices:
help_components.append(rf"[dim]\[choices: {choices}][/dim]")

if param.show_default and not is_required(parameter):
if not is_required(parameter) and (
param.show_default or (param.show_default is None and parameter.default is not None)
):
default = ""
if isclass(type_) and issubclass(type_, Enum):
default = parameter.default.name.lower().replace("_", "-")
Expand Down
5 changes: 3 additions & 2 deletions cyclopts/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ def validator(type_, value: Any) -> None:
Defaults to ``parse`` value (``True``).
"""

show_default: bool = field(default=True)
show_default: Optional[bool] = field(default=None)
"""
If a variable has a default, display the default in the help page.
Defaults to ``True``.
Defaults to ``None``, which is similar to ``True``, but will not display the default if it's ``None``.
"""

show_choices: bool = field(default=True)
Expand Down
3 changes: 3 additions & 0 deletions docs/source/vs_typer/help_defaults/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ Cyclopts fixes this:
# │ * SRC,--src File to compress. [required] │
# │ DST,--dst Path to save compressed data to. [default: out.zip] │
# ╰────────────────────────────────────────────────────────────────────╯
Additionally, if the default value is ``None``, cyclopts's default configuration will **not** display ``[default: None]``.
Typically ``None`` is a sentinel value who's true value gets set inside the function.
2 changes: 1 addition & 1 deletion tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def cmd(
expected = dedent(
"""\
╭─ Parameters ───────────────────────────────────────────────────────╮
│ FOO,--foo,--empty-foo Docstring for foo. [default: None]
│ FOO,--foo,--empty-foo Docstring for foo.
╰────────────────────────────────────────────────────────────────────╯
"""
)
Expand Down

0 comments on commit 9c1cf7d

Please sign in to comment.