diff options
author | Waldek Maleska <w.maleska@gmail.com> | 2022-07-04 09:31:56 +0100 |
---|---|---|
committer | John Villalovos <john@sodarock.com> | 2022-07-28 07:09:08 -0700 |
commit | 005ba93074d391f818c39e46390723a0d0d16098 (patch) | |
tree | 0c64c57bf7871856b9ad6f5545a2e964c462ccf3 /gitlab/cli.py | |
parent | 1cf59323194b2352bd1c1313415cd09bbdddcc5f (diff) | |
download | gitlab-005ba93074d391f818c39e46390723a0d0d16098.tar.gz |
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>
Diffstat (limited to 'gitlab/cli.py')
-rw-r--r-- | gitlab/cli.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py index 9793964..fd519c3 100644 --- a/gitlab/cli.py +++ b/gitlab/cli.py @@ -50,6 +50,23 @@ custom_actions: Dict[str, Dict[str, Tuple[Tuple[str, ...], Tuple[str, ...], bool __F = TypeVar("__F", bound=Callable[..., Any]) +class VerticalHelpFormatter(argparse.HelpFormatter): + def format_help(self) -> str: + result = super().format_help() + output = "" + indent = self._indent_increment * " " + 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("{"): + choices = line.strip().strip("{}").split(",") + choices_str = f"\n{indent}".join(choices) + line = f"{indent}{choices_str}\n" + output += line + return output + + def register_custom_action( cls_names: Union[str, Tuple[str, ...]], mandatory: Tuple[str, ...] = (), @@ -110,6 +127,7 @@ def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser: parser = argparse.ArgumentParser( add_help=add_help, description="GitLab API Command Line Interface", + formatter_class=VerticalHelpFormatter, allow_abbrev=False, ) parser.add_argument("--version", help="Display the version.", action="store_true") |