@@ -63,69 +63,6 @@ def _cls_to_what(cls):
63
63
return camel_re .sub (r'\1-\2' , cls .__name__ ).lower ()
64
64
65
65
66
- def _populate_sub_parser_by_class (cls , sub_parser ):
67
- for action_name in ['list' , 'get' , 'create' , 'update' , 'delete' ]:
68
- attr = 'can' + action_name .capitalize ()
69
- if not getattr (cls , attr ):
70
- continue
71
- sub_parser_action = sub_parser .add_parser (action_name )
72
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
73
- required = True )
74
- for x in cls .requiredUrlAttrs ]
75
- sub_parser_action .add_argument ("--sudo" , required = False )
76
-
77
- if action_name == "list" :
78
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
79
- required = True )
80
- for x in cls .requiredListAttrs ]
81
- sub_parser_action .add_argument ("--page" , required = False )
82
- sub_parser_action .add_argument ("--per-page" , required = False )
83
-
84
- elif action_name in ["get" , "delete" ]:
85
- if cls not in [gitlab .CurrentUser ]:
86
- if cls .getRequiresId :
87
- id_attr = cls .idAttr .replace ('_' , '-' )
88
- sub_parser_action .add_argument ("--%s" % id_attr ,
89
- required = True )
90
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
91
- required = True )
92
- for x in cls .requiredGetAttrs if x != cls .idAttr ]
93
-
94
- elif action_name == "create" :
95
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
96
- required = True )
97
- for x in cls .requiredCreateAttrs ]
98
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
99
- required = False )
100
- for x in cls .optionalCreateAttrs ]
101
-
102
- elif action_name == "update" :
103
- id_attr = cls .idAttr .replace ('_' , '-' )
104
- sub_parser_action .add_argument ("--%s" % id_attr ,
105
- required = True )
106
-
107
- attrs = (cls .requiredUpdateAttrs
108
- if (cls .requiredUpdateAttrs or cls .optionalUpdateAttrs )
109
- else cls .requiredCreateAttrs )
110
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
111
- required = True )
112
- for x in attrs if x != cls .idAttr ]
113
-
114
- attrs = (cls .optionalUpdateAttrs
115
- if (cls .requiredUpdateAttrs or cls .optionalUpdateAttrs )
116
- else cls .optionalCreateAttrs )
117
- [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
118
- required = False )
119
- for x in attrs ]
120
-
121
- if cls in EXTRA_ACTIONS :
122
- for action_name in sorted (EXTRA_ACTIONS [cls ]):
123
- sub_parser_action = sub_parser .add_parser (action_name )
124
- d = EXTRA_ACTIONS [cls ][action_name ]
125
- [sub_parser_action .add_argument ("--%s" % arg , required = True )
126
- for arg in d .get ('required' , [])]
127
-
128
-
129
66
def do_auth (gitlab_id , config_files ):
130
67
try :
131
68
gl = gitlab .Gitlab .from_config (gitlab_id , config_files )
@@ -286,11 +223,70 @@ def do_project_milestone_issues(self, cls, gl, what, args):
286
223
_die ("Impossible to get milestone issues (%s)" % str (e ))
287
224
288
225
289
- def main ():
290
- if "--version" in sys .argv :
291
- print (gitlab .__version__ )
292
- exit (0 )
226
+ def _populate_sub_parser_by_class (cls , sub_parser ):
227
+ for action_name in ['list' , 'get' , 'create' , 'update' , 'delete' ]:
228
+ attr = 'can' + action_name .capitalize ()
229
+ if not getattr (cls , attr ):
230
+ continue
231
+ sub_parser_action = sub_parser .add_parser (action_name )
232
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
233
+ required = True )
234
+ for x in cls .requiredUrlAttrs ]
235
+ sub_parser_action .add_argument ("--sudo" , required = False )
236
+
237
+ if action_name == "list" :
238
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
239
+ required = True )
240
+ for x in cls .requiredListAttrs ]
241
+ sub_parser_action .add_argument ("--page" , required = False )
242
+ sub_parser_action .add_argument ("--per-page" , required = False )
293
243
244
+ elif action_name in ["get" , "delete" ]:
245
+ if cls not in [gitlab .CurrentUser ]:
246
+ if cls .getRequiresId :
247
+ id_attr = cls .idAttr .replace ('_' , '-' )
248
+ sub_parser_action .add_argument ("--%s" % id_attr ,
249
+ required = True )
250
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
251
+ required = True )
252
+ for x in cls .requiredGetAttrs if x != cls .idAttr ]
253
+
254
+ elif action_name == "create" :
255
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
256
+ required = True )
257
+ for x in cls .requiredCreateAttrs ]
258
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
259
+ required = False )
260
+ for x in cls .optionalCreateAttrs ]
261
+
262
+ elif action_name == "update" :
263
+ id_attr = cls .idAttr .replace ('_' , '-' )
264
+ sub_parser_action .add_argument ("--%s" % id_attr ,
265
+ required = True )
266
+
267
+ attrs = (cls .requiredUpdateAttrs
268
+ if (cls .requiredUpdateAttrs or cls .optionalUpdateAttrs )
269
+ else cls .requiredCreateAttrs )
270
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
271
+ required = True )
272
+ for x in attrs if x != cls .idAttr ]
273
+
274
+ attrs = (cls .optionalUpdateAttrs
275
+ if (cls .requiredUpdateAttrs or cls .optionalUpdateAttrs )
276
+ else cls .optionalCreateAttrs )
277
+ [sub_parser_action .add_argument ("--%s" % x .replace ('_' , '-' ),
278
+ required = False )
279
+ for x in attrs ]
280
+
281
+ if cls in EXTRA_ACTIONS :
282
+ for action_name in sorted (EXTRA_ACTIONS [cls ]):
283
+ sub_parser_action = sub_parser .add_parser (action_name )
284
+ d = EXTRA_ACTIONS [cls ][action_name ]
285
+ [sub_parser_action .add_argument ("--%s" % arg , required = True )
286
+ for arg in d .get ('required' , [])]
287
+
288
+
289
+ def _build_parser (args = sys .argv [1 :]):
294
290
parser = argparse .ArgumentParser (
295
291
description = "GitLab API Command Line Interface" )
296
292
parser .add_argument ("--version" , help = "Display the version." ,
@@ -330,7 +326,20 @@ def main():
330
326
_populate_sub_parser_by_class (cls , object_subparsers )
331
327
object_subparsers .required = True
332
328
333
- arg = parser .parse_args ()
329
+ return parser
330
+
331
+
332
+ def _parse_args (args = sys .argv [1 :]):
333
+ parser = _build_parser ()
334
+ return parser .parse_args (args )
335
+
336
+
337
+ def main ():
338
+ if "--version" in sys .argv :
339
+ print (gitlab .__version__ )
340
+ exit (0 )
341
+
342
+ arg = _parse_args ()
334
343
args = arg .__dict__
335
344
336
345
config_files = arg .config_file
0 commit comments