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