@@ -20,7 +20,6 @@ import os
20
20
import sys
21
21
22
22
from ConfigParser import ConfigParser
23
- from optparse import OptionParser
24
23
25
24
from inspect import getmro
26
25
@@ -30,19 +29,28 @@ def die(msg):
30
29
sys .stderr .write (msg + "\n " )
31
30
sys .exit (1 )
32
31
33
- # cmd line options
34
- parser = OptionParser ()
35
- parser .add_option ("-g" , "--gitlab" , dest = "gitlab_id" ,
36
- help = "select the gitlab connection from the configuration file" ,
37
- metavar = "GITLAB" )
38
- (options , args ) = parser .parse_args ()
32
+ gitlab_id = None
33
+
34
+ args = []
35
+ d = {}
36
+ for arg in sys .argv [1 :]:
37
+ if arg .startswith ('--' ):
38
+ k , v = arg .split ('=' , 2 )
39
+ k = k [2 :].strip ()
40
+ v = v .strip ()
41
+
42
+ if k == 'gitlab' :
43
+ gitlab_id = v
44
+ else :
45
+ d [k ] = v
46
+ else :
47
+ args .append (arg )
39
48
40
49
# read the config
41
50
config = ConfigParser ()
42
51
config .read (['/etc/python-gitlab.cfg' ,
43
52
os .path .expanduser ('~/.python-gitlab.cfg' )])
44
53
45
- gitlab_id = options .gitlab_id
46
54
if gitlab_id is None :
47
55
try :
48
56
gitlab_id = config .get ('global' , 'default' )
@@ -79,16 +87,6 @@ if gitlab.GitlabObject not in getmro(cls):
79
87
die ("Unknown object: %s" % what )
80
88
81
89
if action == "create" :
82
- d = {}
83
- try :
84
- for arg in args :
85
- k , v = arg .split ("=" , 2 )
86
- k = k .strip ()
87
- v = v .strip ()
88
- d [k ] = v
89
- except :
90
- die ("Impossible to parse data: %s" % arg )
91
-
92
90
try :
93
91
o = cls (gl , d )
94
92
o .save ()
@@ -98,16 +96,6 @@ if action == "create":
98
96
sys .exit (0 )
99
97
100
98
elif action == "list" :
101
- d = {}
102
- try :
103
- for arg in args :
104
- k , v = arg .split ("=" , 2 )
105
- k = k .strip ()
106
- v = v .strip ()
107
- d [k ] = v
108
- except :
109
- die ("Impossible to parse data: %s" % arg )
110
-
111
99
try :
112
100
l = cls .list (gl , ** d )
113
101
except Exception as e :
@@ -121,19 +109,9 @@ elif action == "list":
121
109
122
110
elif action == "get" :
123
111
try :
124
- id = args .pop (0 )
112
+ id = d .pop ('id' )
125
113
except :
126
- die ("First arg must be the object id" )
127
-
128
- d = {}
129
- try :
130
- for arg in args :
131
- k , v = arg .split ("=" , 2 )
132
- k = k .strip ()
133
- v = v .strip ()
134
- d [k ] = v
135
- except :
136
- die ("Impossible to parse data: %s" % arg )
114
+ die ("Missing --id argument" )
137
115
138
116
try :
139
117
o = cls (gl , id , ** d )
@@ -146,19 +124,9 @@ elif action == "get":
146
124
147
125
elif action == "delete" :
148
126
try :
149
- id = args .pop (0 )
127
+ id = d .pop ('id' )
150
128
except :
151
- die ("First arg must be the object id" )
152
-
153
- d = {}
154
- try :
155
- for arg in args :
156
- k , v = arg .split ("=" , 2 )
157
- k = k .strip ()
158
- v = v .strip ()
159
- d [k ] = v
160
- except :
161
- die ("Impossible to parse data: %s" % arg )
129
+ die ("Missing --id argument" )
162
130
163
131
try :
164
132
o = cls (gl , id , ** d )
@@ -174,19 +142,9 @@ elif action == "delete":
174
142
175
143
elif action == "update" :
176
144
try :
177
- id = args .pop (0 )
178
- except :
179
- die ("First arg must be the object id" )
180
-
181
- d = {}
182
- try :
183
- for arg in args :
184
- k , v = arg .split ("=" , 2 )
185
- k = k .strip ()
186
- v = v .strip ()
187
- d [k ] = v
145
+ id = d .pop ('id' )
188
146
except :
189
- die ("Impossible to parse data: %s" % arg )
147
+ die ("Missing --id argument" )
190
148
191
149
try :
192
150
o = cls (gl , id , ** d )
0 commit comments