Skip to content

Commit 42aaf72

Browse files
committed
Allow commands with no options
1 parent 0ba4d85 commit 42aaf72

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1515
- Remove explicit reference to `click.BaseCommand` and `click.MultiCommand` objects in anticipation of their deprecation. (Pull #82)
1616
- Properly ensure whitespace is trimmed from the usage string. (Pull #83)
1717
- Propagate `context_settings` to `click.Context`-like objects. (Pull #79)
18+
- Allow commands with no options. (Pull #84)
1819

1920
## 0.8.1 - 2023-09-18
2021

mkdocs_click/_docs.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,10 @@ def _make_plain_options(ctx: click.Context, show_hidden: bool = False) -> Iterat
252252
# First line is redundant "Options"
253253
option_lines = option_lines[1:]
254254

255-
if not option_lines: # pragma: no cover
256-
# We expect at least `--help` to be present.
257-
raise RuntimeError("Expected at least one option")
255+
# It's possible to define a command with no options, especially common when
256+
# forwarding arguments to an external process.
257+
if not option_lines:
258+
return
258259

259260
yield "**Options:**"
260261
yield ""
@@ -335,6 +336,12 @@ def _make_table_options(ctx: click.Context, show_hidden: bool = False) -> Iterat
335336

336337
options = [param for param in ctx.command.get_params(ctx) if isinstance(param, click.Option)]
337338
options = [option for option in options if not option.hidden or show_hidden]
339+
340+
# It's possible to define a command with no options, especially common when
341+
# forwarding arguments to an external process.
342+
if not options:
343+
return
344+
338345
option_rows = [_format_table_option_row(option) for option in options]
339346

340347
yield "**Options:**"

pyproject.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,13 @@ dependencies = [
6969
]
7070
[tool.hatch.envs.test.scripts]
7171
test = [
72-
"pytest -q {args}",
72+
"pytest {args}",
7373
"bash .tools/test.sh {args}",
7474
]
75+
[tool.hatch.envs.test.overrides]
76+
platform.windows.scripts = [
77+
{ key = "test", value = "pytest {args}" },
78+
]
7579

7680
[tool.hatch.envs.types]
7781
dependencies = [

tests/test_docs.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def hello_ascii_art():
4242
"""
4343

4444

45+
@click.command(context_settings={"help_option_names": []})
46+
def hello_no_options():
47+
"""
48+
Hello, world!
49+
"""
50+
51+
4552
HELLO_EXPECTED = dedent(
4653
"""
4754
# hello
@@ -64,6 +71,21 @@ def hello_ascii_art():
6471
).lstrip()
6572

6673

74+
HELLO_EXPECTED_NO_OPTIONS = dedent(
75+
"""
76+
# hello
77+
78+
Hello, world!
79+
80+
**Usage:**
81+
82+
```text
83+
hello [OPTIONS]
84+
```
85+
"""
86+
).lstrip()
87+
88+
6789
def test_basic():
6890
output = "\n".join(make_command_docs("hello", hello))
6991
assert output == HELLO_EXPECTED
@@ -97,6 +119,11 @@ def test_basic_ascii_art():
97119
assert output == HELLO_EXPECTED
98120

99121

122+
def test_basic_no_options():
123+
output = "\n".join(make_command_docs("hello", hello_no_options))
124+
assert output == HELLO_EXPECTED_NO_OPTIONS
125+
126+
100127
@click.command()
101128
@click.option("-d", "--debug", help="Include debug output")
102129
@click.option("--choice", type=click.Choice(["foo", "bar"]), default="foo")

0 commit comments

Comments
 (0)