Skip to content

Commit

Permalink
Fix displaying default if required=True and no default is present. Ad…
Browse files Browse the repository at this point in the history
…dresses #143
  • Loading branch information
BrianPugh committed Apr 9, 2024
1 parent 4c4668d commit 6aa976b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cyclopts/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ def create_parameter_help_panel(group: "Group", iparams, cparams: List[Parameter
env_vars = " ".join(cparam.env_var)
help_components.append(rf"[dim]\[env var: {env_vars}][/dim]")

if not cparam.required and (
cparam.show_default or (cparam.show_default is None and iparam.default is not None)
if cparam.show_default or (
cparam.show_default is None and iparam.default not in {None, inspect.Parameter.empty}
):
default = ""
if isclass(type_) and issubclass(type_, Enum):
Expand Down
5 changes: 3 additions & 2 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ API
:type: Optional[bool]
:value: None

Parameter must be supplied.
Defaults to required if parameter does not have a default from the function signature.
Indicates that the parameter must be supplied on the help-page.
Does **not** directly enforce whether or not a parameter must be supplied; only influences the help-page.
Defaults to inferring from the function signature; i.e. ``False`` if the parameter has a default, ``True`` otherwise.

.. attribute:: show
:type: Optional[bool]
Expand Down
28 changes: 28 additions & 0 deletions tests/test_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,34 @@ def cmd(
assert actual == expected


def test_help_print_parameter_required(app, console):
@app.command(help="Cmd help string.")
def cmd(
foo: Annotated[str, Parameter(required=False, help="Docstring for foo.")],
*,
bar: Annotated[str, Parameter(required=True, help="Docstring for bar.")],
):
pass

with console.capture() as capture:
app.help_print(["cmd"], console=console)

actual = capture.get()
expected = dedent(
"""\
Usage: app cmd [ARGS] [OPTIONS]
Cmd help string.
╭─ Parameters ───────────────────────────────────────────────────────╮
│ FOO,--foo Docstring for foo. │
│ * --bar Docstring for bar. [required] │
╰────────────────────────────────────────────────────────────────────╯
"""
)
assert actual == expected


def test_help_print_function_defaults(app, console):
@app.command(help="Cmd help string.")
def cmd(
Expand Down

0 comments on commit 6aa976b

Please sign in to comment.