Skip to content

Commit 182d42c

Browse files
authored
Merge pull request #7 from i-VRESSE/no-subcommands
Support parser without sub-commands
2 parents 958c994 + 1badbad commit 182d42c

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ In case you feel like you've made a valuable contribution, but you don't know ho
4646
To create a release you need write permission on the repository.
4747

4848
1. Check the author list in [`CITATION.cff`](CITATION.cff)
49-
2. Bump the version in `mkdocs_rich_argparse/__init__.py, pyproject.toml`.
49+
2. Bump the version in `mkdocs_rich_argparse/__init__.py`.
5050
3. Go to the [GitHub release page](https://github.com/i-VRESSE/mkdocs_rich_argparse/releases)
5151
4. Press draft a new release button
5252
5. Fill version, title and description field

README.dev.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ coverage report
5151

5252
`coverage` can also generate output in HTML and other formats; see `coverage help` for more information.## Running linters locally
5353

54+
## Linting and formatting
55+
5456
For linting and sorting imports we will use [ruff](https://docs.astral.sh/ruff/). Running the linters requires [uv](https://docs.astral.sh/uv) installed.
5557

5658
```shell
@@ -61,6 +63,8 @@ uvx ruff check .
6163
uvx ruff check . --fix
6264
```
6365

64-
### (3/3) GitHub
66+
For formatting we also use ruff
6567

66-
Don't forget to also make a [release on GitHub](https://github.com/i-VRESSE/mkdocs_rich_argparse/releases/new).
68+
```shell
69+
uvx ruff format .
70+
```

src/mkdocs_rich_argparse/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from rich.text import Text
1818
from rich_argparse import RichHelpFormatter
1919

20-
__version__ = "0.1.2"
20+
__version__ = "0.1.3"
2121

2222
logger = mkdocs.plugins.get_plugin_logger(__name__)
2323

@@ -56,6 +56,11 @@ def argparser_to_markdown(parser: argparse.ArgumentParser, heading: str = "CLI R
5656
]
5757

5858
subparsers_actions = [action for action in parser._actions if isinstance(action, argparse._SubParsersAction)]
59+
60+
# Only process subcommands if they exist
61+
if not subparsers_actions:
62+
return "\n".join(lines)
63+
5964
current_subparsers_action = subparsers_actions[0]
6065

6166
for sub_cmd_name, sub_cmd_parser in current_subparsers_action.choices.items():

tests/test_plugin.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,38 @@ def test_argparser_to_markdown_with_color(monkeypatch: pytest.MonkeyPatch, sampl
9393

9494
assert len(result) > 3500
9595
assert '<span style="color: #008080; text-decoration-color: #008080">' in result
96+
97+
98+
@pytest.fixture
99+
def sample_parser_without_subcommands():
100+
parser = argparse.ArgumentParser(
101+
prog="myprogram", description="This is my program.", formatter_class=RichHelpFormatter
102+
)
103+
parser.add_argument("--verbose", action="store_true", help="Enable verbose mode")
104+
return parser
105+
106+
107+
def test_argparser_to_markdown_without_subcommands(sample_parser_without_subcommands: argparse.ArgumentParser):
108+
result = argparser_to_markdown(sample_parser_without_subcommands, heading="My Program CLI")
109+
110+
expected = dedent("""\
111+
# My Program CLI
112+
Documentation for the `myprogram` script.
113+
```console
114+
myprogram --help
115+
```
116+
<pre style="font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace">
117+
<code style="font-family:inherit" class="nohighlight">
118+
Usage: myprogram [-h] [--verbose]
119+
120+
This is my program.
121+
122+
Options:
123+
-h, --help show this help message and exit
124+
--verbose Enable verbose mode
125+
126+
</code>
127+
</pre>
128+
""")
129+
130+
assert result == expected

0 commit comments

Comments
 (0)