Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit005ba93

Browse files
weakcamelJohnVillalovosnejch
committed
feat(cli): add a custom help formatter
Add a custom argparse help formatter that overrides the outputformat 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 parent1cf5932 commit005ba93

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+
classVerticalHelpFormatter(argparse.HelpFormatter):
54+
defformat_help(self)->str:
55+
result=super().format_help()
56+
output=""
57+
indent=self._indent_increment*" "
58+
forlineinresult.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+
ifline.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+
returnoutput
68+
69+
5370
defregister_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
forclsinsorted(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
assertret.returncode==0
3535

3636

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp