CmdLineOption.st
branchjv
changeset 20396 dd4549cee94c
parent 20077 e0e720fce465
child 23107 40173e082cbc
equal deleted inserted replaced
20361:f48f50e335a6 20396:dd4549cee94c
    39 optionsFor: anObject
    39 optionsFor: anObject
    40 
    40 
    41     "Returns a collection of command line options for
    41     "Returns a collection of command line options for
    42      given object."
    42      given object."
    43 
    43 
    44     ^anObject class allSelectors  
    44     ^anObject class allSelectors asSet 
    45                 select:[:sel|sel startsWith: 'cmdlineOption']
    45                 select:[:sel|sel startsWith: 'cmdlineOption']
    46                 thenCollect:[:sel|anObject perform: sel].
    46                 thenCollect:[:sel|anObject perform: sel].
    47 ! !
    47 ! !
    48 
    48 
    49 !CmdLineOption methodsFor:'accessing'!
    49 !CmdLineOption methodsFor:'accessing'!
    86 
    86 
    87 short
    87 short
    88     ^ short
    88     ^ short
    89 !
    89 !
    90 
    90 
    91 short:aCharacterOrString
    91 short:aCharacter
    92 
    92 
    93     (aCharacterOrString isCharacter 
    93     (aCharacter isCharacter 
    94         and:[aCharacterOrString isAlphaNumeric])
    94         and:[aCharacter isLetter or:[aCharacter isDigit]])
    95             ifTrue:[short := aCharacterOrString]
    95             ifTrue:[short := aCharacter]
    96             ifFalse:[self error: 'short option name should be alphanumeric character']
    96             ifFalse:[self error: 'short option name should be alphanumeric character']
    97 
    97 
    98     "Modified: / 29-05-2009 / 16:05:46 / Jan Vrany <vranyj1@fel.cvut.cz>"
    98     "Modified: / 29-05-2009 / 16:05:46 / Jan Vrany <vranyj1@fel.cvut.cz>"
       
    99     "Modified: / 07-09-2016 / 16:25:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    99 !
   100 !
   100 
   101 
   101 spec: spec
   102 spec: spec
   102     "Build an option from option specification"
   103     "Build an option from option specification"
   103 
   104 
   115 
   116 
   116 parseL: argv startingAt: index equalCharPosition: equalPos
   117 parseL: argv startingAt: index equalCharPosition: equalPos
   117     "Parse a long option from argv"
   118     "Parse a long option from argv"
   118 
   119 
   119     self hasParam ifTrue:[ 
   120     self hasParam ifTrue:[ 
   120         "/ Determine whether to parse (GNU-style ?) `--long-option=param` or
   121         " Determine whether to parse (GNU-style ?) `--long-option=param` or
   121         "/ just `--long-option param`.
   122           just `--long-option param`."
   122         (longSpec isNil or:[longSpec includes: $=]) ifTrue:[
   123         (longSpec isNil or:[longSpec includes: $=]) ifTrue:[
   123             equalPos == 0 ifTrue:[
   124             equalPos == 0 ifTrue:[
   124                 ^CmdLineOptionError new
   125                 ^CmdLineOptionError signal:('Option --%',long,' requires argument').
   125                     errorString: ('Option --%1 requires argument' bindWith: long);
       
   126                     parameter: self;
       
   127                     raise    
       
   128             ] ifFalse:[
   126             ] ifFalse:[
   129                 self process: ((argv at: index) copyFrom: equalPos + 1).
   127                 self process: ((argv at: index) copyFrom: equalPos + 1).
   130             ].
   128             ].
   131             ^ index + 1.
   129             ^ index + 1.
   132         ] ifFalse:[
   130         ] ifFalse:[
   133             index < argv size ifTrue:[ 
   131             index < argv size ifTrue:[ 
   134                 self process: (argv at: index + 1).
   132                 self process: (argv at: index + 1).
   135                 ^ index + 2.
   133                 ^ index + 2.
   136             ] ifFalse:[ 
   134             ] ifFalse:[ 
   137                  ^CmdLineOptionError new
   135                  ^CmdLineOptionError signal:('Option --%',long,' requires argument')
   138                     errorString: ('Option --%1 requires argument' bindWith: long);
       
   139                     parameter: self;
       
   140                     raise      
       
   141             ].
   136             ].
   142         ]
   137         ]
   143     ] ifFalse:[ 
   138     ] ifFalse:[ 
   144         self process.
   139         self process.
   145         ^ index + 1
   140         ^ index + 1
   146     ].
   141     ].
   147 
   142 
   148     "Created: / 29-06-2016 / 17:00:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   143     "Created: / 29-06-2016 / 17:00:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
       
   144     "Modified: / 07-09-2016 / 16:55:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   149 ! !
   145 ! !
   150 
   146 
   151 !CmdLineOption methodsFor:'printing & storing'!
   147 !CmdLineOption methodsFor:'printing & storing'!
   152 
   148 
   153 printOn: stream
   149 printOn: stream
   164 
   160 
   165 !CmdLineOption methodsFor:'private'!
   161 !CmdLineOption methodsFor:'private'!
   166 
   162 
   167 spec0:aStringOrCharacter
   163 spec0:aStringOrCharacter
   168     aStringOrCharacter isCharacter ifTrue:[ 
   164     aStringOrCharacter isCharacter ifTrue:[ 
   169         self specS: '-' , aStringOrCharacter.  
   165         self specS: '-' , aStringOrCharacter asString.  
   170         ^ self.
   166         ^ self.
   171     ].
   167     ].
   172     aStringOrCharacter isString ifTrue:[ 
   168     aStringOrCharacter isString ifTrue:[ 
   173         aStringOrCharacter first == $- ifTrue:[ 
   169         aStringOrCharacter first == $- ifTrue:[ 
   174             aStringOrCharacter second == $- ifTrue:[ 
   170             aStringOrCharacter second == $- ifTrue:[ 
   175                 self specL: aStringOrCharacter.  
   171                 self specL: aStringOrCharacter.  
   176                 ^ self.
   172                 ^ self.
   177             ] ifFalse:[ 
   173             ] ifFalse:[ 
   178                 (aStringOrCharacter size == 2 and:[ aStringOrCharacter second isAlphaNumeric ]) ifTrue:[ 
   174                 (aStringOrCharacter size == 2 and:[ aStringOrCharacter second isLetter or:[ aStringOrCharacter second isDigit ]]) ifTrue:[ 
   179                     self specS: aStringOrCharacter.
   175                     self specS: aStringOrCharacter.
   180                     ^ self.
   176                     ^ self.
   181                 ].
   177                 ].
   182             ].
   178             ].
   183         ] ifFalse:[ 
   179         ] ifFalse:[ 
   186         ].
   182         ].
   187     ].
   183     ].
   188     self error: 'Invalid option specification: ' , aStringOrCharacter asString.
   184     self error: 'Invalid option specification: ' , aStringOrCharacter asString.
   189 
   185 
   190     "Created: / 14-06-2016 / 06:46:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   186     "Created: / 14-06-2016 / 06:46:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   191     "Modified: / 29-06-2016 / 09:31:38 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   187     "Modified: / 07-09-2016 / 16:55:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   192 !
   188 !
   193 
   189 
   194 specL:aString
   190 specL:aString
   195     | firstCharPos lastCharPos |
   191     | firstCharPos lastCharPos |
   196 
   192 
   211         lastCharPos := lastCharPos - 1.
   207         lastCharPos := lastCharPos - 1.
   212     ].
   208     ].
   213     (firstCharPos ~~ 1 or:[ lastCharPos ~~ aString size ])
   209     (firstCharPos ~~ 1 or:[ lastCharPos ~~ aString size ])
   214         ifTrue:[ long := aString copyFrom: firstCharPos to: lastCharPos ]
   210         ifTrue:[ long := aString copyFrom: firstCharPos to: lastCharPos ]
   215         ifFalse:[ long := aString ].
   211         ifFalse:[ long := aString ].
   216     (long allSatisfy: [:c | c == $- or:[c isAlphaNumeric] ]) ifFalse:[ 
   212     (long conform: [:c | c == $- or:[c isLetter or:[c isDigit]]]) ifFalse:[ 
   217         long := nil.
   213         long := nil.
   218         self error: 'Invalid option specification: ' , aString asString.
   214         self error: 'Invalid option specification: ' , aString asString.
   219         ^ self.
   215         ^ self.
   220     ].
   216     ].
   221     longSpec := aString.
   217     longSpec := aString.
   222     (longSpec first == $- and:[ longSpec second == $- ]) ifFalse:[ 
   218     (longSpec first == $- and:[ longSpec second == $- ]) ifFalse:[ 
   223         longSpec := '--' , longSpec
   219         longSpec := '--' , longSpec
   224     ].
   220     ].
   225 
   221 
   226     "Created: / 29-06-2016 / 09:24:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   222     "Created: / 29-06-2016 / 09:24:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   227     "Modified: / 29-06-2016 / 18:47:31 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   223     "Modified: / 07-09-2016 / 16:52:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
   228 !
   224 !
   229 
   225 
   230 specS:aString
   226 specS:aString
   231     short notNil ifTrue:[ self error: 'Short option already specified: ', short asString ].
   227     short notNil ifTrue:[ self error: 'Short option already specified: ', short asString ].
   232     short := aString second.
   228     short := aString second.