author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Wed, 13 Jun 2018 14:39:23 +0000 | |
branch | jv |
changeset 23108 | 77cd6e1625e1 |
parent 23107 | 40173e082cbc |
child 23547 | c69c97cec351 |
permissions | -rw-r--r-- |
13402 | 1 |
" |
13938 | 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 |
||
13938 | 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 |
||
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
16 |
"{ NameSpace: Smalltalk }" |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
17 |
|
13402 | 18 |
Object subclass:#CmdLineOption |
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
|
19 |
instanceVariableNames:'action description short shortSpec long longSpec' |
13402 | 20 |
classVariableNames:'' |
21 |
poolDictionaries:'' |
|
22 |
category:'System-Support-Command line' |
|
23 |
! |
|
24 |
||
25 |
!CmdLineOption class methodsFor:'documentation'! |
|
26 |
||
27 |
copyright |
|
28 |
" |
|
13938 | 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 |
||
13938 | 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. |
|
40 |
" |
|
41 |
! |
|
13482 | 42 |
|
13938 | 43 |
optionsFor: anObject |
13482 | 44 |
|
13938 | 45 |
"Returns a collection of command line options for |
46 |
given object." |
|
47 |
||
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
48 |
^anObject class allSelectors asSet |
13938 | 49 |
select:[:sel|sel startsWith: 'cmdlineOption'] |
50 |
thenCollect:[:sel|anObject perform: sel]. |
|
13402 | 51 |
! ! |
52 |
||
53 |
!CmdLineOption methodsFor:'accessing'! |
|
54 |
||
55 |
action |
|
56 |
^ action |
|
57 |
||
58 |
"Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
59 |
! |
|
60 |
||
61 |
action:aBlockOrMessageSend |
|
62 |
||
63 |
aBlockOrMessageSend numArgs > 1 ifTrue: |
|
64 |
[CmdLineOptionError raiseErrorString: 'Action must be zero-or-one arg block/message send']. |
|
65 |
action := aBlockOrMessageSend. |
|
66 |
||
67 |
"Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
68 |
"Modified: / 16-06-2009 / 15:46:54 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
69 |
! |
|
70 |
||
71 |
description |
|
72 |
^ description |
|
73 |
||
74 |
"Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
75 |
! |
|
76 |
||
77 |
description:aString |
|
78 |
description := aString. |
|
79 |
||
80 |
"Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
81 |
! |
|
82 |
||
83 |
long |
|
84 |
^ long |
|
85 |
! |
|
86 |
||
87 |
long:aString |
|
88 |
long := aString. |
|
89 |
! |
|
90 |
||
91 |
short |
|
92 |
^ short |
|
93 |
! |
|
94 |
||
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
95 |
short:aCharacter |
13402 | 96 |
|
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
97 |
(aCharacter isCharacter |
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
98 |
and:[aCharacter isLetter or:[aCharacter isDigit]]) |
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
99 |
ifTrue:[short := aCharacter] |
13402 | 100 |
ifFalse:[self error: 'short option name should be alphanumeric character'] |
101 |
||
102 |
"Modified: / 29-05-2009 / 16:05:46 / 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
|
103 |
"Modified: / 07-09-2016 / 16:25:53 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
104 |
! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
105 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
106 |
spec: spec |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
107 |
"Build an option from option specification" |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
108 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
109 |
long := short := nil. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
110 |
(spec isCollection and:[ spec isString not ]) ifTrue:[ |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
111 |
spec do:[:each | self spec0: each ] |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
112 |
] ifFalse:[ |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
113 |
self spec0: spec. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
114 |
]. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
115 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
116 |
"Created: / 14-06-2016 / 06:46:02 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
13402 | 117 |
! ! |
118 |
||
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
|
119 |
!CmdLineOption methodsFor:'parsing'! |
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
|
120 |
|
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
|
121 |
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
|
122 |
"Parse a long option from argv" |
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
|
123 |
|
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
|
124 |
self hasParam 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
|
125 |
" Determine whether to parse (GNU-style ?) `--long-option=param` or |
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
126 |
just `--long-option param`." |
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
|
127 |
(longSpec isNil or:[longSpec includes: $=]) 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
|
128 |
equalPos == 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
|
129 |
^CmdLineOptionError signal:('Option --%',long,' requires argument'). |
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
|
130 |
] 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
|
131 |
self process: ((argv at: index) copyFrom: 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
|
132 |
]. |
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
|
133 |
^ index + 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
|
134 |
] 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
|
135 |
index < argv size 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
|
136 |
self process: (argv at: index + 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
|
137 |
^ index + 2. |
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
|
138 |
] 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
|
139 |
^CmdLineOptionError signal:('Option --%',long,' requires argument') |
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
|
140 |
]. |
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
|
141 |
] |
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
|
142 |
] 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
|
143 |
self process. |
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
|
144 |
^ index + 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
|
145 |
]. |
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
|
146 |
|
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
|
147 |
"Created: / 29-06-2016 / 17:00:39 / 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: / 07-09-2016 / 16:55:02 / 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
|
149 |
! ! |
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
|
150 |
|
13402 | 151 |
!CmdLineOption methodsFor:'printing & storing'! |
152 |
||
153 |
printOn: stream |
|
154 |
||
155 |
super printOn: stream. |
|
156 |
stream nextPut:$(. |
|
157 |
short ifNotNil:[stream nextPut: $-; nextPut: short]. |
|
158 |
(short notNil and: [long notNil]) ifTrue:[stream nextPut:$|]. |
|
159 |
long ifNotNil:[stream nextPut: $-; nextPut: $-; nextPutAll: long]. |
|
160 |
stream nextPut:$) |
|
161 |
||
162 |
"Created: / 08-06-2009 / 14:48:59 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
163 |
! ! |
|
164 |
||
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
165 |
!CmdLineOption methodsFor:'private'! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
166 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
167 |
spec0:aStringOrCharacter |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
168 |
aStringOrCharacter isCharacter 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
|
169 |
self specS: '-' , aStringOrCharacter asString. |
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
170 |
^ self. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
171 |
]. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
172 |
aStringOrCharacter isString ifTrue:[ |
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
|
173 |
aStringOrCharacter first == $- 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
|
174 |
aStringOrCharacter second == $- 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
|
175 |
self specL: aStringOrCharacter. |
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
|
176 |
^ self. |
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
177 |
] 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
|
178 |
(aStringOrCharacter size == 2 and:[ aStringOrCharacter second isLetter or:[ aStringOrCharacter second isDigit ]]) ifTrue:[ |
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
|
179 |
self specS: aStringOrCharacter. |
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
180 |
^ self. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
181 |
]. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
182 |
]. |
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
|
183 |
] 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
|
184 |
self specL: aStringOrCharacter. |
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
|
185 |
^ self |
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
186 |
]. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
187 |
]. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
188 |
self error: 'Invalid option specification: ' , aStringOrCharacter asString. |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
189 |
|
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
190 |
"Created: / 14-06-2016 / 06:46:02 / 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
|
191 |
"Modified: / 07-09-2016 / 16:55:25 / 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
|
192 |
! |
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
|
193 |
|
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
|
194 |
specL:aString |
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
|
195 |
| firstCharPos lastCharPos | |
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
|
196 |
|
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
|
197 |
long notNil ifTrue: [ self error: 'Long option already specified: ', short asString ]. |
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
|
198 |
firstCharPos := 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
|
199 |
(aString first == $- and:[ aString second == $- ]) 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
|
200 |
firstCharPos := 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
|
201 |
]. |
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
|
202 |
lastCharPos := aString 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
|
203 |
lastCharPos == 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
|
204 |
lastCharPos := aString indexOf: Character space. |
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
|
205 |
lastCharPos == 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
|
206 |
lastCharPos := aString size. |
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
|
207 |
] 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
|
208 |
lastCharPos := lastCharPos - 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
|
209 |
]. |
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
|
210 |
] 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
|
211 |
lastCharPos := lastCharPos - 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
|
212 |
]. |
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
|
213 |
(firstCharPos ~~ 1 or:[ lastCharPos ~~ aString size ]) |
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
|
214 |
ifTrue:[ long := aString copyFrom: firstCharPos to: lastCharPos ] |
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
|
215 |
ifFalse:[ long := aString ]. |
20396
dd4549cee94c
Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
20077
diff
changeset
|
216 |
(long conform: [:c | c == $- or:[c isLetter or:[c isDigit]]]) ifFalse:[ |
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
|
217 |
long := nil. |
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
|
218 |
self error: 'Invalid option specification: ' , aString asString. |
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
|
219 |
^ self. |
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
|
220 |
]. |
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
|
221 |
longSpec := aString. |
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
|
222 |
(longSpec first == $- and:[ longSpec second == $- ]) 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
|
223 |
longSpec := '--' , longSpec |
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
|
224 |
]. |
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
|
225 |
|
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
|
226 |
"Created: / 29-06-2016 / 09:24:44 / 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
|
227 |
"Modified: / 07-09-2016 / 16:52:28 / 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
|
228 |
! |
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
|
229 |
|
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
|
230 |
specS:aString |
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
|
231 |
short notNil ifTrue:[ self error: 'Short option already specified: ', short asString ]. |
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
|
232 |
short := aString second. |
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
|
233 |
shortSpec := aString. |
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
|
234 |
|
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
|
235 |
"Created: / 29-06-2016 / 09:23:16 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
236 |
! ! |
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
237 |
|
13402 | 238 |
!CmdLineOption methodsFor:'processing'! |
239 |
||
240 |
process |
|
241 |
||
242 |
action value |
|
243 |
||
244 |
"Created: / 08-06-2009 / 14:35:01 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
245 |
! |
|
246 |
||
247 |
process: value |
|
248 |
||
249 |
action value: value |
|
250 |
||
251 |
"Created: / 08-06-2009 / 14:35:08 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
252 |
! ! |
|
253 |
||
254 |
!CmdLineOption methodsFor:'queries'! |
|
255 |
||
256 |
hasParam |
|
257 |
||
258 |
^action numArgs = 1 |
|
259 |
||
260 |
"Created: / 08-06-2009 / 13:45:41 / Jan Vrany <vranyj1@fel.cvut.cz>" |
|
261 |
! ! |
|
262 |
||
263 |
!CmdLineOption class methodsFor:'documentation'! |
|
264 |
||
13938 | 265 |
version |
266 |
^'$Header: /cvs/stx/stx/libbasic/CmdLineOption.st,v 1.3 2012-01-13 10:58:29 vrany Exp $' |
|
13402 | 267 |
! |
268 |
||
20076
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19988
diff
changeset
|
269 |
version_HG |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19988
diff
changeset
|
270 |
|
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19988
diff
changeset
|
271 |
^ '$Changeset: <not expanded> $' |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19988
diff
changeset
|
272 |
! |
ff2f4d06a2fa
CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
19988
diff
changeset
|
273 |
|
13402 | 274 |
version_SVN |
13938 | 275 |
^ '§Id: CmdLineOption.st 10737 2011-11-06 21:23:48Z vranyj1 §' |
13402 | 276 |
! ! |
19988
148721e96482
Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
18011
diff
changeset
|
277 |