author | Claus Gittinger <cg@exept.de> |
Thu, 03 Aug 2017 16:41:13 +0200 | |
changeset 22159 | c8f19ee3f888 |
parent 16690 | 445461b9df6c |
child 18120 | e3a375d5f6a8 |
child 23385 | f93e38f2fe07 |
permissions | -rw-r--r-- |
10888 | 1 |
" |
2 |
Copyright (c) 2005 Ian Piumarta |
|
3 |
All rights reserved. |
|
4 |
||
5 |
Permission is hereby granted, free of charge, to any person obtaining a |
|
6 |
copy of this software and associated documentation files (the 'Software'), |
|
7 |
to deal in the Software without restriction, including without limitation |
|
8 |
the rights to use, copy, modify, merge, publish, distribute, and/or sell |
|
9 |
copies of the Software, and to permit persons to whom the Software is |
|
10 |
furnished to do so, provided that the above copyright notice(s) and this |
|
11 |
permission notice appear in all copies of the Software and that both the |
|
12 |
above copyright notice(s) and this permission notice appear in supporting |
|
13 |
documentation. |
|
14 |
||
15 |
THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK. |
|
16 |
||
17 |
Last edited: 2006-02-03 11:13:33 by piumarta on margaux.local |
|
18 |
" |
|
19 |
"{ Package: 'stx:libbasic' }" |
|
20 |
||
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
21 |
Dictionary subclass:#GetOpt |
16690 | 22 |
instanceVariableNames:'defaultBlock onErrorBlock' |
10888 | 23 |
classVariableNames:'' |
24 |
poolDictionaries:'' |
|
25 |
category:'System-Support' |
|
26 |
! |
|
27 |
||
28 |
!GetOpt class methodsFor:'documentation'! |
|
29 |
||
30 |
copyright |
|
31 |
" |
|
32 |
Copyright (c) 2005 Ian Piumarta |
|
33 |
All rights reserved. |
|
34 |
||
35 |
Permission is hereby granted, free of charge, to any person obtaining a |
|
36 |
copy of this software and associated documentation files (the 'Software'), |
|
37 |
to deal in the Software without restriction, including without limitation |
|
38 |
the rights to use, copy, modify, merge, publish, distribute, and/or sell |
|
39 |
copies of the Software, and to permit persons to whom the Software is |
|
40 |
furnished to do so, provided that the above copyright notice(s) and this |
|
41 |
permission notice appear in all copies of the Software and that both the |
|
42 |
above copyright notice(s) and this permission notice appear in supporting |
|
43 |
documentation. |
|
44 |
||
45 |
THE SOFTWARE IS PROVIDED 'AS IS'. USE ENTIRELY AT YOUR OWN RISK. |
|
46 |
||
47 |
Last edited: 2006-02-03 11:13:33 by piumarta on margaux.local |
|
48 |
" |
|
49 |
! |
|
50 |
||
51 |
documentation |
|
52 |
" |
|
53 |
GetOpt -- command line parser |
|
54 |
||
55 |
Smalltalk version of Unix getopt(3)-like command line parser. |
|
56 |
Crash course: |
|
57 |
||
58 |
1) Create a GetOpt with 'GetOpt new'. |
|
59 |
2) Tell it what options to expect with 'getOpt at: optChar put: optBlock' |
|
60 |
where optChar is a character (the option, duh) and optBlock is a |
|
61 |
unary block (for options without arguments) or a binary block for |
|
62 |
options with arguments. (The first block parameter is always the |
|
63 |
option letter that was matched; the second, if present, is the |
|
64 |
argument to the option.) |
|
65 |
3) Tell it what to do with option $? if you want to intercept unrecognised |
|
66 |
options. |
|
67 |
4) Send it 'default: unaryBlock' to tell it what to do with non-option |
|
68 |
arguments. |
|
69 |
5) Send it 'parse: aCollection' to parse the arguments in aCollection. |
|
70 |
||
71 |
Note that '-x foo' and '-xfoo' are handled correctly for an option |
|
72 |
'x' that expects an argument (in both cases the argument is 'foo'). |
|
73 |
||
74 |
For anyone who didn't understand the crash course, the following: |
|
75 |
||
76 |
| files searchPath outputPath verbose | |
|
77 |
files := OrderedCollection new. |
|
78 |
searchPath := OrderedCollection new. |
|
79 |
outputPath := nil. |
|
80 |
verbose := false. |
|
81 |
GetOpt new |
|
82 |
at: $I put: [ :opt :arg | searchPath add: arg ]; |
|
83 |
at: $o put: [ :opt :arg | outputPath := arg ]; |
|
84 |
at: $v put: [ :opt | verbose := true ]; |
|
85 |
at: $? put: [ :opt | self error: 'illegal option: -' , opt asString ]; |
|
86 |
default: [ :arg | files add: arg ]; |
|
87 |
parse: Smalltalk arguments startingAt: 1. |
|
88 |
||
89 |
will parse a compiler command line for include directories ('-I dir' |
|
90 |
option, argument appended to 'searchPath'), an output filename |
|
91 |
('-o filename' option, argument left in 'outputPath'), a verbosity |
|
92 |
flag ('-v' option, setting 'verbose' to true), and zero or more input |
|
93 |
filenames (anything else, appended to 'files'). |
|
94 |
If you still don't understand then you shouldn't be here. |
|
95 |
||
96 |
[author:] |
|
97 |
Ian Piumarta |
|
98 |
||
99 |
[see also:] |
|
100 |
StandaloneStartup |
|
101 |
Smalltalk |
|
102 |
ReadEvalPrintLoop |
|
103 |
" |
|
104 |
! |
|
105 |
||
106 |
example |
|
107 |
" |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
108 |
| commandLine commandLineArguments files searchPath outputPath verbose foo level | |
10888 | 109 |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
110 |
commandLine := '-I /foo/bar -level 1 --foo -o bla.x -v file1 file2 file3'. |
10888 | 111 |
commandLineArguments := commandLine asCollectionOfWords. |
112 |
||
113 |
files := OrderedCollection new. |
|
114 |
searchPath := OrderedCollection new. |
|
115 |
outputPath := nil. |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
116 |
verbose := foo := false. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
117 |
level := nil. |
10888 | 118 |
GetOpt new |
119 |
at: $I put: [ :opt :arg | searchPath add: arg ]; |
|
120 |
at: $o put: [ :opt :arg | outputPath := arg ]; |
|
121 |
at: $v put: [ :opt | verbose := true ]; |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
122 |
at: '-foo' put: [ :opt | foo := true ]; |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
123 |
at: 'level' put: [ :opt :arg | level := arg ]; |
10888 | 124 |
at: $? put: [ :opt | self error: 'illegal option: -' , opt asString ]; |
125 |
default: [ :arg | files add: arg ]; |
|
126 |
parse: commandLineArguments startingAt: 1. |
|
127 |
||
128 |
Transcript show:'files: '; showCR:files. |
|
129 |
Transcript show:'searchPath: '; showCR:searchPath. |
|
130 |
Transcript show:'outputPath: '; showCR:outputPath. |
|
131 |
Transcript show:'verbose: '; showCR:verbose. |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
132 |
Transcript show:'foo: '; showCR:foo. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
133 |
Transcript show:'level: '; showCR:level. |
10888 | 134 |
" |
135 |
! ! |
|
136 |
||
137 |
!GetOpt class methodsFor:'instance creation'! |
|
138 |
||
139 |
new |
|
140 |
^ super new initializeDefaultBlock |
|
141 |
! ! |
|
142 |
||
143 |
!GetOpt methodsFor:'accessing'! |
|
144 |
||
145 |
default: unaryBlock |
|
146 |
defaultBlock := unaryBlock |
|
16690 | 147 |
! |
148 |
||
149 |
onError: unaryBlock |
|
150 |
onErrorBlock := unaryBlock |
|
151 |
! ! |
|
152 |
||
153 |
!GetOpt methodsFor:'error reporting'! |
|
154 |
||
155 |
error:aMessage |
|
156 |
onErrorBlock notNil ifTrue:[ |
|
157 |
onErrorBlock value:aMessage |
|
158 |
]. |
|
159 |
super error:aMessage |
|
10888 | 160 |
! ! |
161 |
||
162 |
!GetOpt methodsFor:'initialization'! |
|
163 |
||
164 |
initializeDefaultBlock |
|
165 |
defaultBlock := [:arg | ]. |
|
166 |
! ! |
|
167 |
||
168 |
!GetOpt methodsFor:'parsing'! |
|
169 |
||
170 |
parse: argumentCollection |
|
171 |
^ self parse: argumentCollection startingAt: 1 |
|
172 |
! |
|
173 |
||
174 |
parse: argumentCollection startingAt: offset |
|
175 |
| args | |
|
176 |
||
177 |
args := argumentCollection readStream skip: (offset - 1). |
|
178 |
[args atEnd] |
|
179 |
whileFalse:[ |
|
180 |
| arg | |
|
181 |
arg := args next. |
|
182 |
self parseArgument: arg with: args ] |
|
183 |
! ! |
|
184 |
||
185 |
!GetOpt methodsFor:'parsing - private'! |
|
186 |
||
187 |
parseArgument: arg with: rest |
|
14133 | 188 |
(arg size > 1 and:[arg first = $-]) |
10888 | 189 |
ifTrue: [self parseOption: arg with: rest] |
190 |
ifFalse: [defaultBlock value: arg] |
|
191 |
! |
|
192 |
||
193 |
parseOption: option with: rest |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
194 |
| block longOption | |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
195 |
|
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
196 |
"/ cg: changed to support non-single-character args (--foo) |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
197 |
block := self at: option second ifAbsent:nil. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
198 |
block isNil ifTrue:[ |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
199 |
option size > 2 ifTrue:[ |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
200 |
longOption := option copyFrom:2. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
201 |
block := self at: longOption ifAbsent:nil. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
202 |
block notNil ifTrue:[ |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
203 |
"/ a long option; never take rest of option as argument |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
204 |
block arity = 1 |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
205 |
ifTrue: [ ^ block value: longOption ] |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
206 |
ifFalse: [ |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
207 |
rest atEnd |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
208 |
ifTrue: [self error: 'argument missing to option ' , longOption]. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
209 |
^ block value: longOption value: rest next |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
210 |
] |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
211 |
] |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
212 |
]. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
213 |
block isNil ifTrue:[ |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
214 |
block := self at: $? ifAbsent: nil. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
215 |
block isNil ifTrue:[ |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
216 |
^ defaultBlock value: option |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
217 |
] |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
218 |
] |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
219 |
]. |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
220 |
^ block arity = 1 |
10888 | 221 |
ifTrue: [self applyOption: option to: block] |
222 |
ifFalse: [self applyOption: option to: block with: rest] |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
223 |
|
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
224 |
"Modified: / 19-09-2011 / 10:07:57 / cg" |
10888 | 225 |
! ! |
226 |
||
227 |
!GetOpt methodsFor:'private'! |
|
228 |
||
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
229 |
applyOption: anOption to: unaryBlock |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
230 |
^anOption size == 2 |
10888 | 231 |
ifTrue: [unaryBlock value: anOption second] |
232 |
ifFalse: [self error: 'option ' , anOption , ' should not have an argument'] |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
233 |
|
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
234 |
"Modified: / 19-09-2011 / 10:03:31 / cg" |
10888 | 235 |
! |
236 |
||
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
237 |
applyOption: anOption to: binaryBlock with: rest |
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
238 |
^anOption size == 2 |
10888 | 239 |
ifTrue: [rest atEnd |
240 |
ifTrue: [self error: 'argument missing to option ' , anOption] |
|
241 |
ifFalse: [binaryBlock value: anOption second value: rest next]] |
|
242 |
ifFalse: [binaryBlock value: anOption second value: (anOption copyFrom: 3)] |
|
13712
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
243 |
|
9b34eae96ce6
cg: support long options, such as \"--foo\", \"-foo\", \"--bar x\" or
Claus Gittinger <cg@exept.de>
parents:
10888
diff
changeset
|
244 |
"Modified: / 19-09-2011 / 10:06:05 / cg" |
10888 | 245 |
! ! |
246 |
||
247 |
!GetOpt class methodsFor:'documentation'! |
|
248 |
||
249 |
version |
|
16690 | 250 |
^ '$Header: /cvs/stx/stx/libbasic/GetOpt.st,v 1.4 2014-07-03 18:46:44 cg Exp $' |
10888 | 251 |
! ! |
16690 | 252 |