Skip to content

feat: john's ugly vertical help formatter :) #2191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 22 additions & 0 deletions gitlab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,33 @@ def cls_to_gitlab_resource(cls: RESTObject) -> str:
return dasherized_lowercase.lower()


class VerticalHelpFormatter(argparse.HelpFormatter):
def format_help(self) -> str:
result = super().format_help()
output = ""
for line in result.splitlines(keepends=True):
# All of our resources are on one line and wrapped inside braces.
# For example: {application,resource1,resource2}
# We then put each resource on its own line to make it easier to read.
if line.strip().startswith("{"):
leading_whitespace = line.split("{")[0]
line = (
leading_whitespace
+ f",\n{leading_whitespace}".join(
line.strip().strip("{}").split(",")
)
+ "\n"
)
output += line
return output


def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
add_help=add_help,
description="GitLab API Command Line Interface",
allow_abbrev=False,
formatter_class=VerticalHelpFormatter,
)
parser.add_argument("--version", help="Display the version.", action="store_true")
parser.add_argument(
Expand Down
4 changes: 3 additions & 1 deletion gitlab/v4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:

for cls in sorted(classes, key=operator.attrgetter("__name__")):
arg_name = cli.cls_to_gitlab_resource(cls)
object_group = subparsers.add_parser(arg_name)
object_group = subparsers.add_parser(
arg_name, formatter_class=cli.VerticalHelpFormatter
)

object_subparsers = object_group.add_subparsers(
title="action",
Expand Down