author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 07 Sep 2016 17:00:07 +0100 | |
branch | jv |
changeset 20396 | dd4549cee94c |
parent 20077 | e0e720fce465 |
child 23107 | 40173e082cbc |
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 |
|
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
116 |
^CmdLineOptionError signal: message |
13402 | 117 |
|
118 |
"Created: / 08-06-2009 / 14:22:19 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
119 |
"Modified (format): / 07-09-2016 / 16:54:10 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 120 |
! |
121 |
||
122 |
errorOptionHasNoArgument:option |
|
123 |
self error:'option has no argument' option:option |
|
124 |
||
125 |
"Created: / 08-06-2009 / 14:27:49 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
126 |
! |
|
127 |
||
128 |
errorOptionRequiresArgument:option |
|
129 |
self error:'option requires an argument' option:option |
|
130 |
! ! |
|
131 |
||
132 |
!CmdLineParser methodsFor:'initialization'! |
|
133 |
||
134 |
collectOptionsFrom: anObject |
|
135 |
||
13937 | 136 |
options := CmdLineOption optionsFor: anObject |
13402 | 137 |
|
138 |
"Created: / 08-06-2009 / 13:06:23 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
13937 | 139 |
"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
|
140 |
! |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
141 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
142 |
initialize |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
143 |
"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
|
144 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
145 |
ignoreUnknownOptions := false. |
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 |
"Modified: / 29-06-2016 / 00:04:21 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
148 |
"Modified (comment): / 07-09-2016 / 16:23:07 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 149 |
! ! |
150 |
||
151 |
!CmdLineParser methodsFor:'parsing'! |
|
152 |
||
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
153 |
parse: argv |
13402 | 154 |
" |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
155 |
Parses argv array. Returns array of unparsed (i.e. non-options / unknown options) arguments |
13402 | 156 |
" |
157 |
||
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
158 |
| unparsed current | |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
159 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
160 |
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
|
161 |
current := 1. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
162 |
[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
|
163 |
| arg next | |
13402 | 164 |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
165 |
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
|
166 |
arg first == $- ifTrue:[ |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
167 |
" Maybe an option... " |
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
168 |
next := self parse:argv startingAt:current. |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
169 |
next == 0 ifTrue:[ |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
170 |
" Option has not been recognized " |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
171 |
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
|
172 |
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
|
173 |
] |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
174 |
] ifFalse:[ |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
175 |
" Not an option... " |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
176 |
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
|
177 |
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
|
178 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
179 |
current := next. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
180 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
181 |
^ unparsed |
13402 | 182 |
|
183 |
"Created: / 28-01-2009 / 12:08:29 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
184 |
"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
|
185 |
"Modified: / 29-06-2016 / 00:15:57 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
186 |
"Modified (format): / 07-09-2016 / 16:23:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 187 |
! |
188 |
||
189 |
parse: argv for: object |
|
190 |
||
191 |
" |
|
192 |
Parses argv array. Returns array of unparsed (i.e. non-option) |
|
193 |
arguments. Options are obtained from given object |
|
194 |
" |
|
195 |
||
196 |
^self |
|
197 |
collectOptionsFrom: object; |
|
198 |
parse: argv |
|
199 |
||
200 |
"Created: / 28-01-2009 / 11:50:27 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
201 |
"Modified: / 08-06-2009 / 13:07:59 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
202 |
! |
|
203 |
||
204 |
parse:argv options: opts |
|
205 |
||
206 |
" |
|
207 |
Parses argv array. Returns array of unparsed (i.e. non-option) |
|
208 |
arguments. Options are obtained from given object |
|
209 |
" |
|
210 |
||
211 |
options := opts. |
|
212 |
^self parse: argv |
|
213 |
||
214 |
"Created: / 29-05-2009 / 15:51:41 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
215 |
"Modified: / 08-06-2009 / 13:08:12 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
216 |
! ! |
|
217 |
||
218 |
!CmdLineParser methodsFor:'printing & storing'! |
|
219 |
||
220 |
printHelp |
|
221 |
||
222 |
^self printHelpOn: Stdout |
|
223 |
||
224 |
"Created: / 08-06-2009 / 14:55:52 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
225 |
! |
|
226 |
||
227 |
printHelpOn: stream |
|
228 |
||
229 |
stream nextPutAll:'help...'; cr. |
|
230 |
||
231 |
"Created: / 08-06-2009 / 14:56:00 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
232 |
! ! |
|
233 |
||
234 |
!CmdLineParser methodsFor:'private'! |
|
235 |
||
236 |
optionByLong:longName |
|
237 |
^ options |
|
238 |
detect:[:option | option long = longName ] |
|
239 |
ifNone:[ |
|
240 |
longName = 'help' |
|
241 |
ifTrue:[self cmdlineOptionHelp] |
|
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
242 |
ifFalse:[ignoreUnknownOptions ifTrue:[nil] ifFalse:[CmdLineOptionError signal:'Unknown option: ' , longName ]]] |
13402 | 243 |
|
244 |
"Created: / 30-01-2009 / 09:15:24 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
245 |
"Modified: / 08-06-2009 / 14:57:42 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
246 |
"Modified: / 07-09-2016 / 16:53:49 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 247 |
! |
248 |
||
249 |
optionByShort:shortName |
|
250 |
^ options |
|
251 |
detect:[:option | option short = shortName ] |
|
252 |
ifNone: |
|
17467 | 253 |
[ shortName == $h |
13402 | 254 |
ifTrue:[self cmdlineOptionHelp] |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
255 |
ifFalse:[ignoreUnknownOptions ifTrue:[nil] ifFalse:[CmdLineOptionError signal:'Unknown option: ' , shortName ]]] |
13402 | 256 |
|
257 |
"Created: / 30-01-2009 / 09:16:51 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
258 |
"Modified: / 08-06-2009 / 14:58:14 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
259 |
"Modified: / 07-09-2016 / 16:53:42 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 260 |
! |
261 |
||
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
262 |
parse:argv startingAt:index |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
263 |
"Parses arg at index. Returns an index of next arg to be parsed. If the options |
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
264 |
is not recognized (i.e., is unknown), then either an error is triggered |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
265 |
(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
|
266 |
is true)" |
13402 | 267 |
|
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
268 |
| arg option param | |
13402 | 269 |
|
270 |
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
|
271 |
arg second ~= $- ifTrue:[ |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
272 |
" Short option or bunch of those " |
13402 | 273 |
2 to:arg size do:[:subIndex | |
274 |
option := self optionByShort:(arg at:subIndex). |
|
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
275 |
option notNil ifTrue:[ |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
276 |
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
|
277 |
option process |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
278 |
] ifTrue:[ |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
279 |
" Do additional check, if this short option is last." |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
280 |
((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
|
281 |
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
|
282 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
283 |
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
|
284 |
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
|
285 |
^ index + 2 |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
286 |
] |
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
287 |
] ifFalse:[ ^ 0 ]. |
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
288 |
]. |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
289 |
^ index + 1 |
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
290 |
] ifFalse:[ |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
291 |
" Long option starting with -- " |
13402 | 292 |
| equalPos | |
293 |
||
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
294 |
equalPos := arg indexOf:$=. |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
295 |
equalPos ~~ 0 ifTrue:[ |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
296 |
option := self optionByLong:(arg copyFrom:3 to: equalPos - 1) |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
297 |
] ifFalse:[ |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
298 |
option := self optionByLong:(arg copyFrom:3). |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
299 |
]. |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
300 |
option notNil ifTrue:[ |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
301 |
^ option parseL: argv startingAt: index equalCharPosition: equalPos |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
302 |
] ifFalse:[ |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
303 |
^ 0 |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
304 |
]. |
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
305 |
]. |
13402 | 306 |
|
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19989
diff
changeset
|
307 |
"Created: / 28-06-2016 / 23:55:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
20077
e0e720fce465
CmdLineParser refactored to allow both `--long-option=value` and `--long-option value` forms
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20076
diff
changeset
|
308 |
"Modified: / 29-06-2016 / 17:01:27 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
309 |
"Modified (comment): / 07-09-2016 / 16:24:06 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 310 |
! ! |
311 |
||
312 |
!CmdLineParser class methodsFor:'documentation'! |
|
313 |
||
13937 | 314 |
version |
17467 | 315 |
^'$Header: /cvs/stx/stx/libbasic/CmdLineParser.st,v 1.5 2015-02-13 22:48:41 cg Exp $' |
13402 | 316 |
! |
317 |
||
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
318 |
version_HG |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
319 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
320 |
^ '$Changeset: <not expanded> $' |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
321 |
! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18120
diff
changeset
|
322 |
|
13402 | 323 |
version_SVN |
17467 | 324 |
^ '$Id: CmdLineParser.st,v 1.5 2015-02-13 22:48:41 cg Exp $' |
13402 | 325 |
! ! |
16455 | 326 |