Skip to content

Commit 765f43c

Browse files
committed
fix(cli): fix action display in --help when there are few actions
fix #2656
1 parent b4951cd commit 765f43c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

gitlab/cli.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import re
55
import sys
6+
import textwrap
67
from types import ModuleType
78
from typing import (
89
Any,
@@ -50,11 +51,20 @@ def format_help(self) -> str:
5051
for line in result.splitlines(keepends=True):
5152
# All of our resources are on one line and wrapped inside braces.
5253
# For example: {application,resource1,resource2}
54+
# except if there are few resources, then the line and help text
55+
# are collapsed on the same line
56+
# For exemple: {list} Action to execute on the GitLab resource.
5357
# We then put each resource on its own line to make it easier to read.
5458
if line.strip().startswith("{"):
55-
choices = line.strip().strip("{}").split(",")
56-
choices_str = f"\n{indent}".join(choices)
57-
line = f"{indent}{choices_str}\n"
59+
choices, help_str = line.split("}", 1)
60+
if help_str.strip():
61+
help_str = f"{' ' * (len(choices) + 1)}{help_str}"
62+
else:
63+
help_str = None
64+
choices_str = textwrap.indent(choices.strip(" {").replace(",", "\n"), indent)
65+
line = f"{choices_str}\n"
66+
if help_str:
67+
line += f"{help_str}" # includes a \n
5868
output += line
5969
return output
6070

tests/functional/cli/test_cli.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def test_resource_help_prints_actions_vertically(script_runner):
4646
assert ret.returncode == 0
4747

4848

49+
def test_resource_help_prints_actions_vertically_only_one_action(script_runner):
50+
ret = script_runner.run(["gitlab", "event", "--help"])
51+
assert """action:\n list\n""" in ret.stdout
52+
assert ret.returncode == 0
53+
54+
4955
@pytest.mark.script_launch_mode("inprocess")
5056
@responses.activate
5157
def test_defaults_to_gitlab_com(script_runner, resp_get_project, monkeypatch):

0 commit comments

Comments
 (0)