Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita8072d9

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

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 delete3project_id=2
118+
gitlab ProjectSnippet delete--id=3 --project_id=2
120119

121120
# update a Snippet:
122-
gitlab ProjectSnippet update4project_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
importsys
2121

2222
fromConfigParserimportConfigParser
23-
fromoptparseimportOptionParser
2423

2524
frominspectimportgetmro
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+
forarginsys.argv[1:]:
37+
ifarg.startswith('--'):
38+
k,v=arg.split('=',2)
39+
k=k[2:].strip()
40+
v=v.strip()
41+
42+
ifk=='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
ifgitlab_idisNone:
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
ifaction=="create":
82-
d= {}
83-
try:
84-
forarginargs:
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
elifaction=="list":
101-
d= {}
102-
try:
103-
forarginargs:
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
exceptExceptionase:
@@ -121,19 +109,9 @@ elif action == "list":
121109

122110
elifaction=="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-
forarginargs:
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
elifaction=="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-
forarginargs:
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
elifaction=="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-
forarginargs:
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)

[8]ページ先頭

©2009-2025 Movatter.jp