| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 18aa1fc commit 9acd2d2
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,6 @@ | |||
| 5 | 5 | import pathlib | |
| 6 | 6 | import re | |
| 7 | 7 | import sys | |
| 8 | - import textwrap | ||
| 9 | 8 | from types import ModuleType | |
| 10 | 9 | from typing import ( | |
| 11 | 10 | Any, | |
@@ -37,11 +36,12 @@ class CustomAction: | |||
| 37 | 36 | optional: Tuple[str, ...] | |
| 38 | 37 | in_object: bool | |
| 39 | 38 | requires_id: bool # if the `_id_attr` value should be a required argument | |
| 39 | + help: Optional[str] # help text for the custom action | ||
| 40 | 40 | ||
| 41 | 41 | ||
| 42 | 42 | # custom_actions = { | |
| 43 | 43 | # cls: { | |
| 44 | - # action: (mandatory_args, optional_args, in_obj), | ||
| 44 | + # action: CustomAction, | ||
| 45 | 45 | # }, | |
| 46 | 46 | # } | |
| 47 | 47 | custom_actions: Dict[str, Dict[str, CustomAction]] = {} | |
@@ -54,40 +54,14 @@ class CustomAction: | |||
| 54 | 54 | __F = TypeVar("__F", bound=Callable[..., Any]) | |
| 55 | 55 | ||
| 56 | 56 | ||
| 57 | - class VerticalHelpFormatter(argparse.HelpFormatter): | ||
| 58 | - def format_help(self) -> str: | ||
| 59 | - result = super().format_help() | ||
| 60 | - output = "" | ||
| 61 | - indent = self._indent_increment * " " | ||
| 62 | - for line in result.splitlines(keepends=True): | ||
| 63 | - # All of our resources are on one line and wrapped inside braces. | ||
| 64 | - # For example: {application,resource1,resource2} | ||
| 65 | - # except if there are fewer resources - then the line and help text | ||
| 66 | - # are collapsed on the same line. | ||
| 67 | - # For example: {list} Action to execute on the GitLab resource. | ||
| 68 | - # We then put each resource on its own line to make it easier to read. | ||
| 69 | - if line.strip().startswith("{"): | ||
| 70 | - choice_string, help_string = line.split("}", 1) | ||
| 71 | - choice_list = choice_string.strip(" {").split(",") | ||
| 72 | - help_string = help_string.strip() | ||
| 73 | - | ||
| 74 | - if help_string: | ||
| 75 | - help_indent = len(max(choice_list, key=len)) * " " | ||
| 76 | - choice_list.append(f"{help_indent} {help_string}") | ||
| 77 | - | ||
| 78 | - choices = "\n".join(choice_list) | ||
| 79 | - line = f"{textwrap.indent(choices, indent)}\n" | ||
| 80 | - output += line | ||
| 81 | - return output | ||
| 82 | - | ||
| 83 | - | ||
| 84 | 57 | def register_custom_action( | |
| 85 | 58 | *, | |
| 86 | 59 | cls_names: Union[str, Tuple[str, ...]], | |
| 87 | 60 | required: Tuple[str, ...] = (), | |
| 88 | 61 | optional: Tuple[str, ...] = (), | |
| 89 | 62 | custom_action: Optional[str] = None, | |
| 90 | 63 | requires_id: bool = True, # if the `_id_attr` value should be a required argument | |
| 64 | + help: Optional[str] = None, # help text for the action | ||
| 91 | 65 | ) -> Callable[[__F], __F]: | |
| 92 | 66 | def wrap(f: __F) -> __F: | |
| 93 | 67 | @functools.wraps(f) | |
@@ -115,6 +89,7 @@ def wrapped_f(*args: Any, **kwargs: Any) -> Any: | |||
| 115 | 89 | optional=optional, | |
| 116 | 90 | in_object=in_obj, | |
| 117 | 91 | requires_id=requires_id, | |
| 92 | + help=help, | ||
| 118 | 93 | ) | |
| 119 | 94 | ||
| 120 | 95 | return cast(__F, wrapped_f) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -300,11 +300,14 @@ def _populate_sub_parser_by_class( | |||
| 300 | 300 | if cls.__name__ in cli.custom_actions: | |
| 301 | 301 | name = cls.__name__ | |
| 302 | 302 | for action_name in cli.custom_actions[name]: | |
| 303 | + custom_action = cli.custom_actions[name][action_name] | ||
| 303 | 304 | # NOTE(jlvillal): If we put a function for the `default` value of | |
| 304 | 305 | # the `get` it will always get called, which will break things. | |
| 305 | 306 | action_parser = action_parsers.get(action_name) | |
| 306 | 307 | if action_parser is None: | |
| 307 | - sub_parser_action = sub_parser.add_parser(action_name) | ||
| 308 | + sub_parser_action = sub_parser.add_parser( | ||
| 309 | + action_name, help=custom_action.help | ||
| 310 | + ) | ||
| 308 | 311 | else: | |
| 309 | 312 | sub_parser_action = action_parser | |
| 310 | 313 | # Get the attributes for URL/path construction | |
@@ -315,7 +318,6 @@ def _populate_sub_parser_by_class( | |||
| 315 | 318 | ) | |
| 316 | 319 | sub_parser_action.add_argument("--sudo", required=False) | |
| 317 | 320 | ||
| 318 | - custom_action = cli.custom_actions[name][action_name] | ||
| 319 | 321 | # We need to get the object somehow | |
| 320 | 322 | if not issubclass(cls, gitlab.mixins.GetWithoutIdMixin): | |
| 321 | 323 | if cls._id_attr is not None and custom_action.requires_id: | |
@@ -386,7 +388,6 @@ def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: | |||
| 386 | 388 | mgr_cls = getattr(gitlab.v4.objects, mgr_cls_name) | |
| 387 | 389 | object_group = subparsers.add_parser( | |
| 388 | 390 | arg_name, | |
| 389 | - formatter_class=cli.VerticalHelpFormatter, | ||
| 390 | 391 | help=f"API endpoint: {mgr_cls._path}", | |
| 391 | 392 | ) | |
| 392 | 393 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,13 +36,15 @@ def test_config_error_with_help_prints_help(script_runner): | |||
| 36 | 36 | ||
| 37 | 37 | def test_resource_help_prints_actions_vertically(script_runner): | |
| 38 | 38 | ret = script_runner.run(["gitlab", "project", "--help"]) | |
| 39 | - assert """action:\n list\n get""" in ret.stdout | ||
| 39 | + assert " list List the GitLab resources\n" in ret.stdout | ||
| 40 | + assert " get Get a GitLab resource\n" in ret.stdout | ||
| 40 | 41 | assert ret.returncode == 0 | |
| 41 | 42 | ||
| 42 | 43 | ||
| 43 | 44 | def test_resource_help_prints_actions_vertically_only_one_action(script_runner): | |
| 44 | 45 | ret = script_runner.run(["gitlab", "event", "--help"]) | |
| 45 | - assert """action:\n list\n""" in ret.stdout | ||
| 46 | + assert " {list} Action to execute on the GitLab resource.\n" | ||
| 47 | + assert " list List the GitLab resources\n" in ret.stdout | ||
| 46 | 48 | assert ret.returncode == 0 | |
| 47 | 49 | ||
| 48 | 50 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments