Skip to content

Commit 005ba93

Browse files
weakcamelJohnVillalovosnejch
committed
feat(cli): add a custom help formatter
Add a custom argparse help formatter that overrides the output format to list items vertically. The formatter is derived from argparse.HelpFormatter with minimal changes. Co-authored-by: John Villalovos <john@sodarock.com> Co-authored-by: Nejc Habjan <nejc.habjan@siemens.com>
1 parent 1cf5932 commit 005ba93

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

gitlab/cli.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@
5050
__F = TypeVar("__F", bound=Callable[..., Any])
5151

5252

53+
class VerticalHelpFormatter(argparse.HelpFormatter):
54+
def format_help(self) -> str:
55+
result = super().format_help()
56+
output = ""
57+
indent = self._indent_increment * " "
58+
for line in result.splitlines(keepends=True):
59+
# All of our resources are on one line and wrapped inside braces.
60+
# For example: {application,resource1,resource2}
61+
# We then put each resource on its own line to make it easier to read.
62+
if line.strip().startswith("{"):
63+
choices = line.strip().strip("{}").split(",")
64+
choices_str = f"\n{indent}".join(choices)
65+
line = f"{indent}{choices_str}\n"
66+
output += line
67+
return output
68+
69+
5370
def register_custom_action(
5471
cls_names: Union[str, Tuple[str, ...]],
5572
mandatory: Tuple[str, ...] = (),
@@ -110,6 +127,7 @@ def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser:
110127
parser = argparse.ArgumentParser(
111128
add_help=add_help,
112129
description="GitLab API Command Line Interface",
130+
formatter_class=VerticalHelpFormatter,
113131
allow_abbrev=False,
114132
)
115133
parser.add_argument("--version", help="Display the version.", action="store_true")

gitlab/v4/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,9 @@ def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
377377

378378
for cls in sorted(classes, key=operator.attrgetter("__name__")):
379379
arg_name = cli.cls_to_gitlab_resource(cls)
380-
object_group = subparsers.add_parser(arg_name)
380+
object_group = subparsers.add_parser(
381+
arg_name, formatter_class=cli.VerticalHelpFormatter
382+
)
381383

382384
object_subparsers = object_group.add_subparsers(
383385
title="action",

tests/functional/cli/test_cli.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ def test_config_error_with_help_prints_help(script_runner):
3434
assert ret.returncode == 0
3535

3636

37+
def test_global_help_prints_resources_vertically(script_runner):
38+
ret = script_runner.run("gitlab", "--help")
39+
assert """resource:\n application\n application-appearance\n""" in ret.stdout
40+
assert ret.returncode == 0
41+
42+
43+
def test_resource_help_prints_actions_vertically(script_runner):
44+
ret = script_runner.run("gitlab", "project", "--help")
45+
assert """action:\n list\n get""" in ret.stdout
46+
assert ret.returncode == 0
47+
48+
3749
@pytest.mark.script_launch_mode("inprocess")
3850
@responses.activate
3951
def test_defaults_to_gitlab_com(script_runner, resp_get_project, monkeypatch):

0 commit comments

Comments
 (0)