author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 29 Jun 2016 00:18:22 +0100 | |
branch | jv |
changeset 20076 | ff2f4d06a2fa |
parent 19989 | cac9cb31ce9e |
child 20077 | e0e720fce465 |
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 |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
17 |
instanceVariableNames:'options ignoreUnknownOptions' |
13402 | 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 |
||
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
39 |
!CmdLineParser class methodsFor:'instance creation'! |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
40 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
41 |
new |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
42 |
"return an initialized instance" |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
43 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
44 |
^ self basicNew initialize. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
45 |
! ! |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
46 |
|
13402 | 47 |
!CmdLineParser class methodsFor:'parsing'! |
48 |
||
49 |
parse: argv for: object |
|
50 |
||
51 |
^self new parse: argv for: object |
|
52 |
||
53 |
"Created: / 28-01-2009 / 12:06:04 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
54 |
! ! |
|
55 |
||
56 |
!CmdLineParser methodsFor:'accessing'! |
|
57 |
||
58 |
cmdlineOptionHelp |
|
59 |
||
60 |
^CmdLineOption new |
|
61 |
short: $a; |
|
62 |
long: 'help'; |
|
63 |
description: 'Prints short summary of available options'; |
|
64 |
action:[self printHelp] |
|
65 |
||
66 |
"Created: / 08-06-2009 / 14:54:11 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
67 |
! |
|
68 |
||
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
69 |
ignoreUnknownOptions |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
70 |
^ ignoreUnknownOptions |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
71 |
! |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
72 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
73 |
ignoreUnknownOptions:aBoolean |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
74 |
"When set to true, unknown options are silently ignored and added to |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
75 |
a list of positional arguments returned by #parse:. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
76 |
When set to false, an error is triggered when an unknown option is |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
77 |
encountered (this is the default behaviour)" |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
78 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
79 |
ignoreUnknownOptions := aBoolean. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
80 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
81 |
"Modified (comment): / 29-06-2016 / 00:03:59 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
82 |
! |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
83 |
|
13402 | 84 |
options |
85 |
^ options |
|
86 |
! |
|
87 |
||
88 |
options:something |
|
89 |
options := something. |
|
90 |
! ! |
|
91 |
||
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
92 |
!CmdLineParser methodsFor:'building'! |
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 description: description do: action |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
95 |
| option | |
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 |
option := CmdLineOption new. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
98 |
option spec: spec. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
99 |
option description: description. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
100 |
option action: action. |
19989
cac9cb31ce9e
Fixed bug introduced in 148721e96482 in CmdLineParser
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19988
diff
changeset
|
101 |
options isNil ifTrue:[options := OrderedCollection new]. |
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
102 |
options add: option. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
103 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
104 |
"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
|
105 |
! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
106 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
107 |
on: spec do: action |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
108 |
^ 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
|
109 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
110 |
"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
|
111 |
! ! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
112 |
|
13402 | 113 |
!CmdLineParser methodsFor:'error reporting'! |
114 |
||
115 |
error: message option: option |
|
116 |
||
117 |
<resource: #skipInDebuggerWalkback> |
|
118 |
||
119 |
^CmdLineOptionError new |
|
120 |
errorString: message; |
|
121 |
parameter: option; |
|
122 |
raise |
|
123 |
||
124 |
"Created: / 08-06-2009 / 14:22:19 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
125 |
! |
|
126 |
||
127 |
errorOptionHasNoArgument:option |
|
128 |
self error:'option has no argument' option:option |
|
129 |
||
130 |
"Created: / 08-06-2009 / 14:27:49 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
131 |
! |
|
132 |
||
133 |
errorOptionRequiresArgument:option |
|
134 |
self error:'option requires an argument' option:option |
|
135 |
! ! |
|
136 |
||
137 |
!CmdLineParser methodsFor:'initialization'! |
|
138 |
||
139 |
collectOptionsFrom: anObject |
|
140 |
||
13937 | 141 |
options := CmdLineOption optionsFor: anObject |
13402 | 142 |
|
143 |
"Created: / 08-06-2009 / 13:06:23 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
13937 | 144 |
"Modified: / 06-11-2011 / 21:40:11 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
145 |
! |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
146 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
147 |
initialize |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
148 |
"Invoked when a new instance is created." |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
149 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
150 |
"/ please change as required (and remove this comment) |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
151 |
"/ options := nil. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
152 |
ignoreUnknownOptions := false. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
153 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
154 |
"/ super initialize. -- commented since inherited method does nothing |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
155 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
156 |
"Modified: / 29-06-2016 / 00:04:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 157 |
! ! |
158 |
||
159 |
!CmdLineParser methodsFor:'parsing'! |
|
160 |
||
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
161 |
parse: argv |
13402 | 162 |
" |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
163 |
Parses argv array. Returns array of unparsed (i.e. non-options / unknown options) arguments |
13402 | 164 |
" |
165 |
||
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
166 |
| unparsed current | |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
167 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
168 |
unparsed := OrderedCollection new. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
169 |
current := 1. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
170 |
[current <= argv size] whileTrue: [ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
171 |
| arg next | |
13402 | 172 |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
173 |
arg := argv at:current. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
174 |
arg first == $- ifTrue:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
175 |
"/ maybe an option |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
176 |
next := self parse: argv argAt: current. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
177 |
next == 0 ifTrue:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
178 |
"/ Option has not been recognized |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
179 |
unparsed add: arg. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
180 |
next := current + 1. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
181 |
] |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
182 |
] ifFalse:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
183 |
"/ not an option |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
184 |
unparsed addAll: (argv copyFrom: current). |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
185 |
next := argv size + 1. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
186 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
187 |
current := next. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
188 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
189 |
^ unparsed |
13402 | 190 |
|
191 |
"Created: / 28-01-2009 / 12:08:29 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
192 |
"Modified: / 08-06-2009 / 13:26:26 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
193 |
"Modified: / 29-06-2016 / 00:15:57 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 194 |
! |
195 |
||
196 |
parse: argv for: object |
|
197 |
||
198 |
" |
|
199 |
Parses argv array. Returns array of unparsed (i.e. non-option) |
|
200 |
arguments. Options are obtained from given object |
|
201 |
" |
|
202 |
||
203 |
^self |
|
204 |
collectOptionsFrom: object; |
|
205 |
parse: argv |
|
206 |
||
207 |
"Created: / 28-01-2009 / 11:50:27 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
208 |
"Modified: / 08-06-2009 / 13:07:59 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
209 |
! |
|
210 |
||
211 |
parse:argv options: opts |
|
212 |
||
213 |
" |
|
214 |
Parses argv array. Returns array of unparsed (i.e. non-option) |
|
215 |
arguments. Options are obtained from given object |
|
216 |
" |
|
217 |
||
218 |
options := opts. |
|
219 |
^self parse: argv |
|
220 |
||
221 |
"Created: / 29-05-2009 / 15:51:41 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
222 |
"Modified: / 08-06-2009 / 13:08:12 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
223 |
! ! |
|
224 |
||
225 |
!CmdLineParser methodsFor:'printing & storing'! |
|
226 |
||
227 |
printHelp |
|
228 |
||
229 |
^self printHelpOn: Stdout |
|
230 |
||
231 |
"Created: / 08-06-2009 / 14:55:52 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
232 |
! |
|
233 |
||
234 |
printHelpOn: stream |
|
235 |
||
236 |
stream nextPutAll:'help...'; cr. |
|
237 |
||
238 |
"Created: / 08-06-2009 / 14:56:00 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
239 |
! ! |
|
240 |
||
241 |
!CmdLineParser methodsFor:'private'! |
|
242 |
||
243 |
optionByLong:longName |
|
244 |
^ options |
|
245 |
detect:[:option | option long = longName ] |
|
246 |
ifNone:[ |
|
247 |
longName = 'help' |
|
248 |
ifTrue:[self cmdlineOptionHelp] |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
249 |
ifFalse:[ignoreUnknownOptions ifTrue:[nil] ifFalse:[CmdLineOptionError raiseErrorString:'Unknown option: ' , longName ]]] |
13402 | 250 |
|
251 |
"Created: / 30-01-2009 / 09:15:24 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
252 |
"Modified: / 08-06-2009 / 14:57:42 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
253 |
"Modified: / 29-06-2016 / 00:05:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 254 |
! |
255 |
||
256 |
optionByShort:shortName |
|
257 |
^ options |
|
258 |
detect:[:option | option short = shortName ] |
|
259 |
ifNone: |
|
17467 | 260 |
[ shortName == $h |
13402 | 261 |
ifTrue:[self cmdlineOptionHelp] |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
262 |
ifFalse:[ignoreUnknownOptions ifTrue:[nil] ifFalse:[CmdLineOptionError raiseErrorString:'Unknown option: ' , shortName ]]] |
13402 | 263 |
|
264 |
"Created: / 30-01-2009 / 09:16:51 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
265 |
"Modified: / 08-06-2009 / 14:58:14 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
266 |
"Modified: / 29-06-2016 / 00:05:27 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 267 |
! |
268 |
||
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
269 |
parse:argv argAt: index |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
270 |
"Parses arg at index. Returns an index of next arg to be parsed. If the options |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
271 |
is not recognized (i.e., is unknown), then either an error is triggered |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
272 |
(when ignoreInknownOptions is false) or 0 is returned (when ignoreInknownOptions |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
273 |
is true)" |
13402 | 274 |
|
275 |
|arg option param| |
|
276 |
||
277 |
arg := argv at:index. |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
278 |
arg second ~= $- ifTrue:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
279 |
"/ short option or bunch of those |
13402 | 280 |
2 to:arg size do:[:subIndex | |
281 |
option := self optionByShort:(arg at:subIndex). |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
282 |
option notNil ifTrue: [ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
283 |
option hasParam ifFalse:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
284 |
option process |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
285 |
] ifTrue:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
286 |
"Do additional check, if this short option is last." |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
287 |
((subIndex ~= arg size) or:[ (argv size) < (index + 1) ]) ifTrue:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
288 |
self errorOptionRequiresArgument:option |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
289 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
290 |
param := (argv at:index + 1). |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
291 |
option process:param. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
292 |
^ index + 2 |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
293 |
] |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
294 |
] ifFalse:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
295 |
^ 0 |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
296 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
297 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
298 |
^ index + 1 |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
299 |
] ifFalse: [ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
300 |
"/ long option starting with -- |
13402 | 301 |
| equalPos | |
302 |
||
303 |
(equalPos := arg indexOf:$=) == 0 ifTrue:[ |
|
304 |
"/no arg specified |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
305 |
(option := self optionByLong:(arg copyFrom:3)) notNil ifTrue:[ |
13402 | 306 |
option hasParam ifTrue:[self errorOptionRequiresArgument:option]. |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
307 |
option process |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
308 |
] ifFalse:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
309 |
^ 0 |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
310 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
311 |
^ index + 1. |
13402 | 312 |
] ifFalse: [ |
313 |
option := self optionByLong:(arg copyFrom:3 to: equalPos - 1). |
|
314 |
param := arg copyFrom: equalPos + 1. |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
315 |
option notNil ifTrue:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
316 |
option hasParam |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
317 |
ifTrue: [option process: param] |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
318 |
ifFalse: [self errorOptionHasNoArgument: option] |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
319 |
] ifFalse:[ |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
320 |
^ 0 |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
321 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
322 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
323 |
^ index + 2 |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
324 |
] |
13402 | 325 |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
326 |
"Created: / 28-06-2016 / 23:55:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 327 |
! ! |
328 |
||
329 |
!CmdLineParser class methodsFor:'documentation'! |
|
330 |
||
13937 | 331 |
version |
17467 | 332 |
^'$Header: /cvs/stx/stx/libbasic/CmdLineParser.st,v 1.5 2015-02-13 22:48:41 cg Exp $' |
13402 | 333 |
! |
334 |
||
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
335 |
version_HG |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
336 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
337 |
^ '$Changeset: <not expanded> $' |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
338 |
! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
339 |
|
13402 | 340 |
version_SVN |
17467 | 341 |
^ '$Id: CmdLineParser.st,v 1.5 2015-02-13 22:48:41 cg Exp $' |
13402 | 342 |
! ! |
16455 | 343 |