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 |
|
|
80 |
!CmdLineParser methodsFor:'error reporting'!
|
|
81 |
|
|
82 |
error: message option: option
|
|
83 |
|
|
84 |
<resource: #skipInDebuggerWalkback>
|
|
85 |
|
|
86 |
^CmdLineOptionError new
|
21997
|
87 |
messageText: message;
|
13402
|
88 |
parameter: option;
|
|
89 |
raise
|
|
90 |
|
|
91 |
"Created: / 08-06-2009 / 14:22:19 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
21997
|
92 |
"Modified: / 12-07-2017 / 10:24:34 / mawalch"
|
13402
|
93 |
!
|
|
94 |
|
|
95 |
errorOptionHasNoArgument:option
|
|
96 |
self error:'option has no argument' option:option
|
|
97 |
|
|
98 |
"Created: / 08-06-2009 / 14:27:49 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
99 |
!
|
|
100 |
|
|
101 |
errorOptionRequiresArgument:option
|
|
102 |
self error:'option requires an argument' option:option
|
|
103 |
! !
|
|
104 |
|
|
105 |
!CmdLineParser methodsFor:'initialization'!
|
|
106 |
|
|
107 |
collectOptionsFrom: anObject
|
|
108 |
|
13937
|
109 |
options := CmdLineOption optionsFor: anObject
|
13402
|
110 |
|
|
111 |
"Created: / 08-06-2009 / 13:06:23 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
13937
|
112 |
"Modified: / 06-11-2011 / 21:40:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
|
13402
|
113 |
! !
|
|
114 |
|
|
115 |
!CmdLineParser methodsFor:'parsing'!
|
|
116 |
|
|
117 |
parse
|
|
118 |
"
|
16455
|
119 |
Parses argv array.
|
|
120 |
Returns array of unparsed (i.e. non-option) arguments
|
13402
|
121 |
"
|
|
122 |
|
|
123 |
| i |
|
|
124 |
i := 1.
|
|
125 |
[i <= argv size] whileTrue:
|
|
126 |
[|arg option |
|
|
127 |
arg := argv at:i.
|
|
128 |
"arg is not an option"
|
|
129 |
arg first ~= $-
|
|
130 |
ifTrue:
|
|
131 |
[^argv copyFrom: i].
|
16455
|
132 |
i := self parseArg: i
|
|
133 |
].
|
13402
|
134 |
^#()
|
|
135 |
|
|
136 |
"Created: / 08-06-2009 / 13:26:22 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
137 |
"Modified: / 08-06-2009 / 14:38:33 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
138 |
!
|
|
139 |
|
|
140 |
parse:aCollection
|
|
141 |
"
|
|
142 |
Parses argv array. Returns array of unparsed (i.e. non-option)
|
|
143 |
arguments
|
|
144 |
"
|
|
145 |
|
|
146 |
^self
|
|
147 |
args: aCollection;
|
|
148 |
parse.
|
|
149 |
|
|
150 |
"Created: / 28-01-2009 / 12:08:29 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
151 |
"Modified: / 08-06-2009 / 13:26:26 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
152 |
!
|
|
153 |
|
|
154 |
parse: argv for: object
|
|
155 |
|
|
156 |
"
|
|
157 |
Parses argv array. Returns array of unparsed (i.e. non-option)
|
|
158 |
arguments. Options are obtained from given object
|
|
159 |
"
|
|
160 |
|
|
161 |
^self
|
|
162 |
collectOptionsFrom: object;
|
|
163 |
parse: argv
|
|
164 |
|
|
165 |
"Created: / 28-01-2009 / 11:50:27 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
166 |
"Modified: / 08-06-2009 / 13:07:59 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
167 |
!
|
|
168 |
|
|
169 |
parse:argv options: opts
|
|
170 |
|
|
171 |
"
|
|
172 |
Parses argv array. Returns array of unparsed (i.e. non-option)
|
|
173 |
arguments. Options are obtained from given object
|
|
174 |
"
|
|
175 |
|
|
176 |
options := opts.
|
|
177 |
^self parse: argv
|
|
178 |
|
|
179 |
"Created: / 29-05-2009 / 15:51:41 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
180 |
"Modified: / 08-06-2009 / 13:08:12 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
181 |
! !
|
|
182 |
|
|
183 |
!CmdLineParser methodsFor:'printing & storing'!
|
|
184 |
|
|
185 |
printHelp
|
|
186 |
|
|
187 |
^self printHelpOn: Stdout
|
|
188 |
|
|
189 |
"Created: / 08-06-2009 / 14:55:52 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
190 |
!
|
|
191 |
|
|
192 |
printHelpOn: stream
|
|
193 |
|
|
194 |
stream nextPutAll:'help...'; cr.
|
|
195 |
|
|
196 |
"Created: / 08-06-2009 / 14:56:00 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
197 |
! !
|
|
198 |
|
|
199 |
!CmdLineParser methodsFor:'private'!
|
|
200 |
|
|
201 |
optionByLong:longName
|
|
202 |
^ options
|
|
203 |
detect:[:option | option long = longName ]
|
|
204 |
ifNone:[
|
|
205 |
longName = 'help'
|
|
206 |
ifTrue:[self cmdlineOptionHelp]
|
|
207 |
ifFalse:[CmdLineOptionError raiseErrorString:'Unknown option: ' , longName ]]
|
|
208 |
|
|
209 |
"Created: / 30-01-2009 / 09:15:24 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
210 |
"Modified: / 08-06-2009 / 14:57:42 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
211 |
!
|
|
212 |
|
|
213 |
optionByShort:shortName
|
|
214 |
^ options
|
|
215 |
detect:[:option | option short = shortName ]
|
|
216 |
ifNone:
|
17467
|
217 |
[ shortName == $h
|
13402
|
218 |
ifTrue:[self cmdlineOptionHelp]
|
|
219 |
ifFalse:[CmdLineOptionError raiseErrorString:'Unknown option: ' , shortName ]]
|
|
220 |
|
|
221 |
"Created: / 30-01-2009 / 09:16:51 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
222 |
"Modified: / 08-06-2009 / 14:58:14 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
223 |
!
|
|
224 |
|
|
225 |
parseArg:index
|
|
226 |
"
|
|
227 |
Parses arg at index. Returns an index of
|
|
228 |
next arg to be parsed."
|
|
229 |
|
|
230 |
|arg option param|
|
|
231 |
|
|
232 |
arg := argv at:index.
|
|
233 |
arg second ~= $-
|
|
234 |
ifTrue:
|
|
235 |
["/ short option or bunch of those
|
|
236 |
2 to:arg size do:[:subIndex |
|
|
237 |
option := self optionByShort:(arg at:subIndex).
|
|
238 |
option ifNotNil:
|
|
239 |
[option hasParam
|
|
240 |
ifFalse:[option process]
|
|
241 |
ifTrue:
|
|
242 |
["Do additional check, if this short option
|
|
243 |
is last."
|
|
244 |
((subIndex ~= arg size) or:[ (argv size) < (index + 1) ]) ifTrue:[
|
|
245 |
self errorOptionRequiresArgument:option
|
|
246 |
].
|
|
247 |
param := (argv at:index + 1).
|
|
248 |
option process:param.
|
|
249 |
^ index + 2
|
|
250 |
]]].
|
|
251 |
^ index + 1]
|
|
252 |
ifFalse:
|
|
253 |
["/ long option starting with --
|
|
254 |
| equalPos |
|
|
255 |
|
|
256 |
(equalPos := arg indexOf:$=) == 0 ifTrue:[
|
|
257 |
"/no arg specified
|
|
258 |
(option := self optionByLong:(arg copyFrom:3))
|
|
259 |
ifNotNil:[
|
|
260 |
option hasParam ifTrue:[self errorOptionRequiresArgument:option].
|
|
261 |
option process].
|
|
262 |
^index + 1.
|
|
263 |
] ifFalse: [
|
|
264 |
option := self optionByLong:(arg copyFrom:3 to: equalPos - 1).
|
|
265 |
param := arg copyFrom: equalPos + 1.
|
|
266 |
option ifNotNil:
|
|
267 |
[option hasParam
|
|
268 |
ifTrue:
|
|
269 |
[option process: param]
|
|
270 |
ifFalse:
|
|
271 |
[self errorOptionHasNoArgument: option]]
|
|
272 |
].
|
|
273 |
^index + 2
|
|
274 |
]
|
|
275 |
|
|
276 |
"Created: / 08-06-2009 / 14:38:53 / Jan Vrany <vranyj1@fel.cvut.cz>"
|
|
277 |
! !
|
|
278 |
|
|
279 |
!CmdLineParser class methodsFor:'documentation'!
|
|
280 |
|
13937
|
281 |
version
|
21997
|
282 |
^'$Header$'
|
13402
|
283 |
!
|
|
284 |
|
21998
|
285 |
version_CVS
|
|
286 |
^'$Header$'
|
|
287 |
!
|
|
288 |
|
13402
|
289 |
version_SVN
|
21997
|
290 |
^ '$Id$'
|
13402
|
291 |
! !
|
16455
|
292 |
|