Skip to content
Merged
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
2 changes: 2 additions & 0 deletions changelog.d/+prog-subcommand-aliases.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Apply the `:prog:` override once per sub-command when the sub-command has aliases, instead of prefixing the new name
repeatedly when it contains the original program name.
9 changes: 9 additions & 0 deletions roots/test-prog-subcommand-aliases/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from __future__ import annotations

import sys
from pathlib import Path
from typing import Final

sys.path.insert(0, str(Path(__file__).parent))
extensions: Final[list[str]] = ["sphinx_argparse_cli"]
nitpicky: Final[bool] = True
4 changes: 4 additions & 0 deletions roots/test-prog-subcommand-aliases/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. sphinx_argparse_cli::
:module: parser
:func: make
:prog: python -m tool
10 changes: 10 additions & 0 deletions roots/test-prog-subcommand-aliases/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from __future__ import annotations

from argparse import ArgumentParser


def make() -> ArgumentParser:
parser = ArgumentParser(prog="tool")
run = parser.add_subparsers().add_parser("run", aliases=["r", "go"], help="run help")
run.add_subparsers().add_parser("local", aliases=["l"], help="run locally")
return parser
2 changes: 1 addition & 1 deletion src/sphinx_argparse_cli/_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def __init__(self, parser: ArgumentParser) -> None:
def _update_sub_parser_prog(parser: ArgumentParser, old_prog: str, new_prog: str) -> None:
if (sub_action := _sub_parser_action(parser)) is None:
return
for sub_parser in sub_action.choices.values():
for sub_parser in {id(choice): choice for choice in sub_action.choices.values()}.values():
sub_parser.prog = sub_parser.prog.replace(old_prog, new_prog, 1)
_update_sub_parser_prog(sub_parser, old_prog, new_prog)

Expand Down
9 changes: 9 additions & 0 deletions tests/sphinx_argparse_cli/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,15 @@ def test_prog_subcommands(build_outcome: str) -> None:
assert "my-tool foo" in build_outcome


@pytest.mark.sphinx(buildername="text", testroot="prog-subcommand-aliases")
def test_prog_subcommand_aliases(build_outcome: str) -> None:
assert [line.strip() for line in build_outcome.splitlines() if line.startswith(" ") and "[-h]" in line] == [
"python -m tool [-h] {run,r,go} ...",
"python -m tool run [-h] {local,l} ...",
"python -m tool run local [-h]",
]


@pytest.mark.sphinx(buildername="text", testroot="multiword-prog")
def test_multiword_prog(build_outcome: str) -> None:
assert "python -m build positional arguments" in build_outcome
Expand Down