author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Tue, 14 Jun 2016 07:04:34 +0100 | |
branch | jv |
changeset 19988 | 148721e96482 |
parent 18120 | e3a375d5f6a8 |
child 19989 | cac9cb31ce9e |
permissions | -rw-r--r-- |
13402 | 1 |
" |
13937 | 2 |
COPYRIGHT (c) 2006 by eXept Software AG |
13402 | 3 |
All Rights Reserved |
4 |
||
13937 | 5 |
This software is furnished under a license and may be used |
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
13402 | 11 |
" |
12 |
"{ Package: 'stx:libbasic' }" |
|
13 |
||
17467 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
13402 | 16 |
Object subclass:#CmdLineParser |
17 |
instanceVariableNames:'options argv' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'System-Support-Command line' |
|
21 |
! |
|
22 |
||
23 |
!CmdLineParser class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
13937 | 27 |
COPYRIGHT (c) 2006 by eXept Software AG |
13402 | 28 |
All Rights Reserved |
29 |
||
13937 | 30 |
This software is furnished under a license and may be used |
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
13402 | 36 |
" |
37 |
! ! |
|
38 |
||
39 |
!CmdLineParser class methodsFor:'parsing'! |
|
40 |
||
41 |
parse: argv for: object |
|
42 |
||
43 |
^self new parse: argv for: object |
|
44 |
||
45 |
"Created: / 28-01-2009 / 12:06:04 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
46 |
! ! |
|
47 |
||
48 |
!CmdLineParser methodsFor:'accessing'! |
|
49 |
||
50 |
args |
|
51 |
^ argv |
|
52 |
! |
|
53 |
||
54 |
args:aCollection |
|
55 |
||
56 |
argv := aCollection |
|
57 |
||
58 |
"Modified: / 08-06-2009 / 13:24:02 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
59 |
! |
|
60 |
||
61 |
cmdlineOptionHelp |
|
62 |
||
63 |
^CmdLineOption new |
|
64 |
short: $a; |
|
65 |
long: 'help'; |
|
66 |
description: 'Prints short summary of available options'; |
|
67 |
action:[self printHelp] |
|
68 |
||
69 |
"Created: / 08-06-2009 / 14:54:11 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
70 |
! |
|
71 |
||
72 |
options |
|
73 |
^ options |
|
74 |
! |
|
75 |
||
76 |
options:something |
|
77 |
options := something. |
|
78 |
! ! |
|
79 |
||
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
80 |
!CmdLineParser methodsFor:'building'! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
81 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
82 |
on: spec description: description do: action |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
83 |
| option | |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
84 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
85 |
option := CmdLineOption new. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
86 |
option spec: spec. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
87 |
option description: description. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
88 |
option action: action. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
89 |
options add: option. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
90 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
91 |
"Created: / 14-06-2016 / 06:43:37 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
92 |
! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
93 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
94 |
on: spec do: action |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
95 |
^ self on: spec description: nil do: action |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
96 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
97 |
"Created: / 14-06-2016 / 07:02:56 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
98 |
! ! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
99 |
|
13402 | 100 |
!CmdLineParser methodsFor:'error reporting'! |
101 |
||
102 |
error: message option: option |
|
103 |
||
104 |
<resource: #skipInDebuggerWalkback> |
|
105 |
||
106 |
^CmdLineOptionError new |
|
107 |
errorString: message; |
|
108 |
parameter: option; |
|
109 |
raise |
|
110 |
||
111 |
"Created: / 08-06-2009 / 14:22:19 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
112 |
! |
|
113 |
||
114 |
errorOptionHasNoArgument:option |
|
115 |
self error:'option has no argument' option:option |
|
116 |
||
117 |
"Created: / 08-06-2009 / 14:27:49 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
118 |
! |
|
119 |
||
120 |
errorOptionRequiresArgument:option |
|
121 |
self error:'option requires an argument' option:option |
|
122 |
! ! |
|
123 |
||
124 |
!CmdLineParser methodsFor:'initialization'! |
|
125 |
||
126 |
collectOptionsFrom: anObject |
|
127 |
||
13937 | 128 |
options := CmdLineOption optionsFor: anObject |
13402 | 129 |
|
130 |
"Created: / 08-06-2009 / 13:06:23 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
13937 | 131 |
"Modified: / 06-11-2011 / 21:40:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 132 |
! ! |
133 |
||
134 |
!CmdLineParser methodsFor:'parsing'! |
|
135 |
||
136 |
parse |
|
137 |
" |
|
16455 | 138 |
Parses argv array. |
139 |
Returns array of unparsed (i.e. non-option) arguments |
|
13402 | 140 |
" |
141 |
||
142 |
| i | |
|
143 |
i := 1. |
|
144 |
[i <= argv size] whileTrue: |
|
145 |
[|arg option | |
|
146 |
arg := argv at:i. |
|
147 |
"arg is not an option" |
|
148 |
arg first ~= $- |
|
149 |
ifTrue: |
|
150 |
[^argv copyFrom: i]. |
|
16455 | 151 |
i := self parseArg: i |
152 |
]. |
|
13402 | 153 |
^#() |
154 |
||
155 |
"Created: / 08-06-2009 / 13:26:22 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
156 |
"Modified: / 08-06-2009 / 14:38:33 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
157 |
! |
|
158 |
||
159 |
parse:aCollection |
|
160 |
" |
|
161 |
Parses argv array. Returns array of unparsed (i.e. non-option) |
|
162 |
arguments |
|
163 |
" |
|
164 |
||
165 |
^self |
|
166 |
args: aCollection; |
|
167 |
parse. |
|
168 |
||
169 |
"Created: / 28-01-2009 / 12:08:29 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
170 |
"Modified: / 08-06-2009 / 13:26:26 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
171 |
! |
|
172 |
||
173 |
parse: argv for: object |
|
174 |
||
175 |
" |
|
176 |
Parses argv array. Returns array of unparsed (i.e. non-option) |
|
177 |
arguments. Options are obtained from given object |
|
178 |
" |
|
179 |
||
180 |
^self |
|
181 |
collectOptionsFrom: object; |
|
182 |
parse: argv |
|
183 |
||
184 |
"Created: / 28-01-2009 / 11:50:27 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
185 |
"Modified: / 08-06-2009 / 13:07:59 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
186 |
! |
|
187 |
||
188 |
parse:argv options: opts |
|
189 |
||
190 |
" |
|
191 |
Parses argv array. Returns array of unparsed (i.e. non-option) |
|
192 |
arguments. Options are obtained from given object |
|
193 |
" |
|
194 |
||
195 |
options := opts. |
|
196 |
^self parse: argv |
|
197 |
||
198 |
"Created: / 29-05-2009 / 15:51:41 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
199 |
"Modified: / 08-06-2009 / 13:08:12 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
200 |
! ! |
|
201 |
||
202 |
!CmdLineParser methodsFor:'printing & storing'! |
|
203 |
||
204 |
printHelp |
|
205 |
||
206 |
^self printHelpOn: Stdout |
|
207 |
||
208 |
"Created: / 08-06-2009 / 14:55:52 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
209 |
! |
|
210 |
||
211 |
printHelpOn: stream |
|
212 |
||
213 |
stream nextPutAll:'help...'; cr. |
|
214 |
||
215 |
"Created: / 08-06-2009 / 14:56:00 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
216 |
! ! |
|
217 |
||
218 |
!CmdLineParser methodsFor:'private'! |
|
219 |
||
220 |
optionByLong:longName |
|
221 |
^ options |
|
222 |
detect:[:option | option long = longName ] |
|
223 |
ifNone:[ |
|
224 |
longName = 'help' |
|
225 |
ifTrue:[self cmdlineOptionHelp] |
|
226 |
ifFalse:[CmdLineOptionError raiseErrorString:'Unknown option: ' , longName ]] |
|
227 |
||
228 |
"Created: / 30-01-2009 / 09:15:24 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
229 |
"Modified: / 08-06-2009 / 14:57:42 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
230 |
! |
|
231 |
||
232 |
optionByShort:shortName |
|
233 |
^ options |
|
234 |
detect:[:option | option short = shortName ] |
|
235 |
ifNone: |
|
17467 | 236 |
[ shortName == $h |
13402 | 237 |
ifTrue:[self cmdlineOptionHelp] |
238 |
ifFalse:[CmdLineOptionError raiseErrorString:'Unknown option: ' , shortName ]] |
|
239 |
||
240 |
"Created: / 30-01-2009 / 09:16:51 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
241 |
"Modified: / 08-06-2009 / 14:58:14 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
242 |
! |
|
243 |
||
244 |
parseArg:index |
|
245 |
" |
|
246 |
Parses arg at index. Returns an index of |
|
247 |
next arg to be parsed." |
|
248 |
||
249 |
|arg option param| |
|
250 |
||
251 |
arg := argv at:index. |
|
252 |
arg second ~= $- |
|
253 |
ifTrue: |
|
254 |
["/ short option or bunch of those |
|
255 |
2 to:arg size do:[:subIndex | |
|
256 |
option := self optionByShort:(arg at:subIndex). |
|
257 |
option ifNotNil: |
|
258 |
[option hasParam |
|
259 |
ifFalse:[option process] |
|
260 |
ifTrue: |
|
261 |
["Do additional check, if this short option |
|
262 |
is last." |
|
263 |
((subIndex ~= arg size) or:[ (argv size) < (index + 1) ]) ifTrue:[ |
|
264 |
self errorOptionRequiresArgument:option |
|
265 |
]. |
|
266 |
param := (argv at:index + 1). |
|
267 |
option process:param. |
|
268 |
^ index + 2 |
|
269 |
]]]. |
|
270 |
^ index + 1] |
|
271 |
ifFalse: |
|
272 |
["/ long option starting with -- |
|
273 |
| equalPos | |
|
274 |
||
275 |
(equalPos := arg indexOf:$=) == 0 ifTrue:[ |
|
276 |
"/no arg specified |
|
277 |
(option := self optionByLong:(arg copyFrom:3)) |
|
278 |
ifNotNil:[ |
|
279 |
option hasParam ifTrue:[self errorOptionRequiresArgument:option]. |
|
280 |
option process]. |
|
281 |
^index + 1. |
|
282 |
] ifFalse: [ |
|
283 |
option := self optionByLong:(arg copyFrom:3 to: equalPos - 1). |
|
284 |
param := arg copyFrom: equalPos + 1. |
|
285 |
option ifNotNil: |
|
286 |
[option hasParam |
|
287 |
ifTrue: |
|
288 |
[option process: param] |
|
289 |
ifFalse: |
|
290 |
[self errorOptionHasNoArgument: option]] |
|
291 |
]. |
|
292 |
^index + 2 |
|
293 |
] |
|
294 |
||
295 |
"Created: / 08-06-2009 / 14:38:53 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
296 |
! ! |
|
297 |
||
298 |
!CmdLineParser class methodsFor:'documentation'! |
|
299 |
||
13937 | 300 |
version |
17467 | 301 |
^'$Header: /cvs/stx/stx/libbasic/CmdLineParser.st,v 1.5 2015-02-13 22:48:41 cg Exp $' |
13402 | 302 |
! |
303 |
||
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
304 |
version_HG |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
305 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
306 |
^ '$Changeset: <not expanded> $' |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
307 |
! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
308 |
|
13402 | 309 |
version_SVN |
17467 | 310 |
^ '$Id: CmdLineParser.st,v 1.5 2015-02-13 22:48:41 cg Exp $' |
13402 | 311 |
! ! |
16455 | 312 |