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 )
0 commit comments