Skip to content

Commit a8072d9

Browse files
author
Gauvain Pocentek
committed
Manually parse the arguments
We can use a more common syntax (-- prefix for options) this way.
1 parent dd22ce1 commit a8072d9

File tree

2 files changed

+28
-71
lines changed

2 files changed

+28
-71
lines changed

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,21 @@ Some examples:
109109
gitlab Project list
110110

111111
# get a specific project (id 2):
112-
gitlab Project get 2
112+
gitlab Project get --id=2
113113

114114
# get a list of snippets for this project:
115-
gitlab ProjectIssue list project_id=2
116-
# (you're right, this syntax sucks)
115+
gitlab ProjectIssue list --project_id=2
117116

118117
# delete a Snippet (id 3):
119-
gitlab ProjectSnippet delete 3 project_id=2
118+
gitlab ProjectSnippet delete --id=3 --project_id=2
120119

121120
# update a Snippet:
122-
gitlab ProjectSnippet update 4 project_id=2 code="My New Code"
121+
gitlab ProjectSnippet update --id=4 --project_id=2 --code="My New Code"
123122

124123
# create a Snippet:
125-
gitlab ProjectSnippet create project_id=2
124+
gitlab ProjectSnippet create --project_id=2
126125
Impossible to create object (Missing attribute(s): title, file_name, code)
127126

128127
# oops, let's add the attributes:
129-
gitlab ProjectSnippet create project_id=2 title="the title" file_name="the name" code="the code"
128+
gitlab ProjectSnippet create --project_id=2 --title="the title" --file_name="the name" --code="the code"
130129
`````

gitlab

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import os
2020
import sys
2121

2222
from ConfigParser import ConfigParser
23-
from optparse import OptionParser
2423

2524
from inspect import getmro
2625

@@ -30,19 +29,28 @@ def die(msg):
3029
sys.stderr.write(msg + "\n")
3130
sys.exit(1)
3231

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)
3948

4049
# read the config
4150
config = ConfigParser()
4251
config.read(['/etc/python-gitlab.cfg',
4352
os.path.expanduser('~/.python-gitlab.cfg')])
4453

45-
gitlab_id = options.gitlab_id
4654
if gitlab_id is None:
4755
try:
4856
gitlab_id = config.get('global', 'default')
@@ -79,16 +87,6 @@ if gitlab.GitlabObject not in getmro(cls):
7987
die("Unknown object: %s" % what)
8088

8189
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-
9290
try:
9391
o = cls(gl, d)
9492
o.save()
@@ -98,16 +96,6 @@ if action == "create":
9896
sys.exit(0)
9997

10098
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-
11199
try:
112100
l = cls.list(gl, **d)
113101
except Exception as e:
@@ -121,19 +109,9 @@ elif action == "list":
121109

122110
elif action == "get":
123111
try:
124-
id = args.pop(0)
112+
id = d.pop('id')
125113
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")
137115

138116
try:
139117
o = cls(gl, id, **d)
@@ -146,19 +124,9 @@ elif action == "get":
146124

147125
elif action == "delete":
148126
try:
149-
id = args.pop(0)
127+
id = d.pop('id')
150128
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")
162130

163131
try:
164132
o = cls(gl, id, **d)
@@ -174,19 +142,9 @@ elif action == "delete":
174142

175143
elif action == "update":
176144
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')
188146
except:
189-
die("Impossible to parse data: %s" % arg)
147+
die("Missing --id argument")
190148

191149
try:
192150
o = cls(gl, id, **d)

0 commit comments

Comments
 (0)