Skip to content

Commit ede1224

Browse files
author
Gauvain Pocentek
committed
rework (and fix) the CLI parsing
1 parent c2d1f8e commit ede1224

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

bin/gitlab

+21-25
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,12 @@ def clsToWhat(cls):
6868

6969

7070
def populate_sub_parser_by_class(cls, sub_parser):
71-
sub_parser_class = sub_parser.add_subparsers(
72-
dest='action',
73-
title="positional argument",
74-
description='action with %s' % cls.__name__,
75-
help='action to do'
76-
)
7771
for action_name in ACTIONS:
7872
attr = 'can' + action_name.capitalize()
79-
try:
80-
y = cls.__dict__[attr]
81-
except AttributeError:
82-
y = gitlab.GitlabObject.__dict__[attr]
73+
y = getattr(cls, attr) or getattr(gitlab.GitlabObject, attr)
8374
if not y:
8475
continue
85-
sub_parser_action = sub_parser_class.add_parser(action_name)
76+
sub_parser_action = sub_parser.add_parser(action_name)
8677
[sub_parser_action.add_argument("--%s" % x.replace('_', '-'),
8778
required=True)
8879
for x in cls.requiredUrlAttrs]
@@ -119,7 +110,7 @@ def populate_sub_parser_by_class(cls, sub_parser):
119110

120111
if cls in extra_actions:
121112
for action_name in sorted(extra_actions[cls]):
122-
sub_parser_action = sub_parser_class.add_parser(action_name)
113+
sub_parser_action = sub_parser.add_parser(action_name)
123114
d = extra_actions[cls][action_name]
124115
[sub_parser_action.add_argument("--%s" % arg, required=True)
125116
for arg in d['requiredAttrs']]
@@ -235,32 +226,37 @@ def do_project_owned():
235226
if __name__ == "__main__":
236227
ssl_verify = True
237228
timeout = 60
229+
238230
parser = argparse.ArgumentParser(
239-
description='GitLab API Command Line Interface')
240-
parser.add_argument("-v", "--verbosity", "--fancy",
231+
description="GitLab API Command Line Interface")
232+
parser.add_argument("-v", "--verbose", "--fancy",
241233
help="increase output verbosity",
242234
action="store_true")
243-
parser.add_argument("--gitlab", metavar='gitlab',
235+
parser.add_argument("--gitlab",
244236
help=("Specifies which python-gitlab.cfg "
245237
"configuration section should be used. "
246238
"If not defined, the default selection "
247239
"will be used."),
248240
required=False)
249-
subparsers = parser.add_subparsers(
250-
dest='what',
251-
title="positional argument",
252-
description='GitLab object',
253-
help='GitLab object'
254-
)
241+
242+
subparsers = parser.add_subparsers(dest='what')
255243

256244
# populate argparse for all Gitlab Object
245+
classes = []
257246
for cls in gitlab.__dict__.values():
258247
try:
259248
if gitlab.GitlabObject in getmro(cls):
260-
sub_parser = subparsers.add_parser(clsToWhat(cls))
261-
populate_sub_parser_by_class(cls, sub_parser)
262-
except Exception:
249+
classes.append(cls)
250+
except AttributeError:
263251
pass
252+
classes.sort()
253+
254+
for cls in classes:
255+
arg_name = clsToWhat(cls)
256+
object_group = subparsers.add_parser(arg_name)
257+
258+
object_subparsers = object_group.add_subparsers(dest='action')
259+
populate_sub_parser_by_class(cls, object_subparsers)
264260

265261
arg = parser.parse_args()
266262
d = arg.__dict__
@@ -272,7 +268,7 @@ if __name__ == "__main__":
272268
gitlab_id = arg.gitlab
273269
# conflicts with "gitlab" attribute from GitlabObject class
274270
d.pop("gitlab")
275-
verbose = arg.verbosity
271+
verbose = arg.verbose
276272
action = arg.action
277273
what = arg.what
278274

0 commit comments

Comments
 (0)