Skip to content

Commit b028757

Browse files
feat: john's ugly vertical help formatter :)
1 parent 510ec30 commit b028757

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

gitlab/cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import os
2323
import re
2424
import sys
25+
import textwrap
2526
from types import ModuleType
2627
from typing import Any, Callable, cast, Dict, Optional, Tuple, Type, TypeVar, Union
2728

@@ -106,11 +107,36 @@ def cls_to_gitlab_resource(cls: RESTObject) -> str:
106107
return dasherized_lowercase.lower()
107108

108109

110+
class VerticalHelpFormatter(argparse.HelpFormatter):
111+
def format_help(self) -> str:
112+
result = super().format_help()
113+
output = ""
114+
for line in result.splitlines(keepends=True):
115+
if line.strip().startswith("{"):
116+
leading_whitespace = line.split("{")[0]
117+
resources = ", ".join(line.strip().strip("{}").split(","))
118+
line = (
119+
textwrap.fill(
120+
resources,
121+
initial_indent=leading_whitespace,
122+
subsequent_indent=leading_whitespace,
123+
break_on_hyphens=False,
124+
)
125+
+ "\n"
126+
)
127+
output += line
128+
129+
else:
130+
output += line
131+
return output
132+
133+
109134
def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser:
110135
parser = argparse.ArgumentParser(
111136
add_help=add_help,
112137
description="GitLab API Command Line Interface",
113138
allow_abbrev=False,
139+
formatter_class=VerticalHelpFormatter,
114140
)
115141
parser.add_argument("--version", help="Display the version.", action="store_true")
116142
parser.add_argument(

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",

0 commit comments

Comments
 (0)