Skip to content

Commit 2037352

Browse files
chore(cli): add ability to not add _id_attr as an argument
In some cases we don't want to have `_id_attr` as an argument. Add ability to have it not be added as an argument.
1 parent 61d8679 commit 2037352

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

gitlab/cli.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CustomAction:
3636
required: Tuple[str, ...]
3737
optional: Tuple[str, ...]
3838
in_object: bool
39+
requires_id: bool # if the `_id_attr` value should be a required argument
3940

4041

4142
# custom_actions = {
@@ -86,6 +87,7 @@ def register_custom_action(
8687
required: Tuple[str, ...] = (),
8788
optional: Tuple[str, ...] = (),
8889
custom_action: Optional[str] = None,
90+
requires_id: bool = True, # if the `_id_attr` value should be a required argument
8991
) -> Callable[[__F], __F]:
9092
def wrap(f: __F) -> __F:
9193
@functools.wraps(f)
@@ -109,7 +111,10 @@ def wrapped_f(*args: Any, **kwargs: Any) -> Any:
109111

110112
action = custom_action or f.__name__.replace("_", "-")
111113
custom_actions[final_name][action] = CustomAction(
112-
required=required, optional=optional, in_object=in_obj
114+
required=required,
115+
optional=optional,
116+
in_object=in_obj,
117+
requires_id=requires_id,
113118
)
114119

115120
return cast(__F, wrapped_f)

gitlab/v4/cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,13 @@ def _populate_sub_parser_by_class(
315315
)
316316
sub_parser_action.add_argument("--sudo", required=False)
317317

318+
custom_action = cli.custom_actions[name][action_name]
318319
# We need to get the object somehow
319320
if not issubclass(cls, gitlab.mixins.GetWithoutIdMixin):
320-
if cls._id_attr is not None:
321+
if cls._id_attr is not None and custom_action.requires_id:
321322
id_attr = cls._id_attr.replace("_", "-")
322323
sub_parser_action.add_argument(f"--{id_attr}", required=True)
323324

324-
custom_action = cli.custom_actions[name][action_name]
325325
for x in custom_action.required:
326326
if x != cls._id_attr:
327327
sub_parser_action.add_argument(

0 commit comments

Comments
 (0)