CmdLineOption.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Wed, 04 Oct 2017 21:32:06 +0100
branchjv
changeset 23088 aa14988f9d73
parent 20396 dd4549cee94c
child 23107 40173e082cbc
permissions -rw-r--r--
Merge of feature-94-revamp-thinlocks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
     1
"
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
     2
 COPYRIGHT (c) 2006 by eXept Software AG
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
     3
              All Rights Reserved
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
     4
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
     5
 This software is furnished under a license and may be used
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
     6
 only in accordance with the terms of that license and with the
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
     8
 be provided or otherwise made available to, or used by, any
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
     9
 other person.  No title to or ownership of the software is
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    10
 hereby transferred.
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    11
"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    12
"{ Package: 'stx:libbasic' }"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    13
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
    14
"{ NameSpace: Smalltalk }"
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
    15
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    16
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
    17
	instanceVariableNames:'action description short shortSpec long longSpec'
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    18
	classVariableNames:''
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    19
	poolDictionaries:''
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    20
	category:'System-Support-Command line'
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    21
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    22
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    23
!CmdLineOption class methodsFor:'documentation'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    24
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    25
copyright
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    26
"
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    27
 COPYRIGHT (c) 2006 by eXept Software AG
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    28
              All Rights Reserved
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    29
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    30
 This software is furnished under a license and may be used
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    31
 only in accordance with the terms of that license and with the
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    32
 inclusion of the above copyright notice.   This software may not
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    33
 be provided or otherwise made available to, or used by, any
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    34
 other person.  No title to or ownership of the software is
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    35
 hereby transferred.
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    36
"
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    37
!
13482
769530195abb copyright
Claus Gittinger <cg@exept.de>
parents: 13402
diff changeset
    38
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    39
optionsFor: anObject
13482
769530195abb copyright
Claus Gittinger <cg@exept.de>
parents: 13402
diff changeset
    40
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    41
    "Returns a collection of command line options for
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    42
     given object."
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    43
20396
dd4549cee94c Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 20077
diff changeset
    44
    ^anObject class allSelectors asSet 
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    45
                select:[:sel|sel startsWith: 'cmdlineOption']
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    46
                thenCollect:[:sel|anObject perform: sel].
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    47
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    48
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    49
!CmdLineOption methodsFor:'accessing'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    50
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    51
action
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    52
    ^ action
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    53
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    54
    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    55
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    56
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    57
action:aBlockOrMessageSend
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    58
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    59
    aBlockOrMessageSend numArgs > 1 ifTrue:
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    60
        [CmdLineOptionError raiseErrorString: 'Action must be zero-or-one arg block/message send'].    
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    61
    action := aBlockOrMessageSend.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    62
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    63
    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    64
    "Modified: / 16-06-2009 / 15:46:54 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    65
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    66
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    67
description
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    68
    ^ description
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    69
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    70
    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    71
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    72
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    73
description:aString
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    74
    description := aString.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    75
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    76
    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    77
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    78
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    79
long
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    80
    ^ long
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    81
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    82
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    83
long:aString
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    84
    long := aString.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    85
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    86
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    87
short
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    88
    ^ short
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    89
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    90
20396
dd4549cee94c Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 20077
diff changeset
    91
short:aCharacter
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    92
20396
dd4549cee94c Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 20077
diff changeset
    93
    (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
    94
        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
    95
            ifTrue:[short := aCharacter]
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    96
            ifFalse:[self error: 'short option name should be alphanumeric character']
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    97
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    98
    "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
    99
    "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
   100
!
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   101
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   102
spec: spec
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   103
    "Build an option from option specification"
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
    long := short := nil.
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   106
    (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
   107
        spec do:[:each | self spec0: each ]
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   108
    ] ifFalse:[ 
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   109
        self spec0: spec.
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   110
    ].
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   111
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   112
    "Created: / 14-06-2016 / 06:46:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   113
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   114
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
   115
!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
   116
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
   117
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
   118
    "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
   119
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
    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
   121
        " 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
   122
          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
   123
        (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
   124
            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
   125
                ^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
   126
            ] 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
   127
                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
   128
            ].
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
   129
            ^ 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
   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
            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
   132
                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
   133
                ^ 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
   134
            ] 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
   135
                 ^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
   136
            ].
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
        ]
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:[ 
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
   139
        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
   140
        ^ 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
   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
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
    "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
   144
    "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
   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
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   147
!CmdLineOption methodsFor:'printing & storing'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   148
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   149
printOn: stream
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   150
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   151
    super printOn: stream.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   152
    stream nextPut:$(.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   153
    short ifNotNil:[stream nextPut: $-; nextPut: short].
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   154
    (short notNil and: [long notNil]) ifTrue:[stream nextPut:$|].
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   155
    long ifNotNil:[stream nextPut: $-;  nextPut: $-; nextPutAll: long].
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   156
    stream nextPut:$)
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   157
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   158
    "Created: / 08-06-2009 / 14:48:59 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   159
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   160
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   161
!CmdLineOption methodsFor:'private'!
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   162
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   163
spec0:aStringOrCharacter
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   164
    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
   165
        self specS: '-' , aStringOrCharacter asString.  
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   166
        ^ self.
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   167
    ].
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   168
    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
   169
        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
   170
            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
   171
                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
   172
                ^ self.
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   173
            ] 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
   174
                (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
   175
                    self specS: aStringOrCharacter.
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   176
                    ^ self.
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   177
                ].
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   178
            ].
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
        ] 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
   180
            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
   181
            ^ self
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   182
        ].
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   183
    ].
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   184
    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
   185
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   186
    "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
   187
    "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
   188
!
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
   189
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
   190
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
   191
    | 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
   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
    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
   194
    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
   195
    (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
   196
        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
   197
    ].
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
    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
   199
    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
   200
        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
   201
        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
   202
            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
   203
        ] 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
   204
            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
   205
        ].
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
    ] 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
   207
        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
   208
    ].
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
    (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
   210
        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
   211
        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
   212
    (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
   213
        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
   214
        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
   215
        ^ 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
   216
    ].
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
    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
   218
    (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
   219
        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
   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
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
    "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
   223
    "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
   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
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
   227
    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
   228
    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
   229
    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
   230
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
    "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
   232
! !
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   233
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   234
!CmdLineOption methodsFor:'processing'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   235
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   236
process
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   237
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   238
    action value
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   239
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   240
    "Created: / 08-06-2009 / 14:35:01 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   241
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   242
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   243
process: value
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   244
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   245
    action value: value
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   246
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   247
    "Created: / 08-06-2009 / 14:35:08 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   248
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   249
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   250
!CmdLineOption methodsFor:'queries'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   251
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   252
hasParam
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   253
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   254
    ^action numArgs = 1
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   255
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   256
    "Created: / 08-06-2009 / 13:45:41 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   257
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   258
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   259
!CmdLineOption class methodsFor:'documentation'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   260
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
   261
version
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
   262
    ^'$Header: /cvs/stx/stx/libbasic/CmdLineOption.st,v 1.3 2012-01-13 10:58:29 vrany Exp $'
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   263
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   264
20076
ff2f4d06a2fa CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 19988
diff changeset
   265
version_HG
ff2f4d06a2fa CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 19988
diff changeset
   266
ff2f4d06a2fa CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 19988
diff changeset
   267
    ^ '$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
   268
!
ff2f4d06a2fa CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 19988
diff changeset
   269
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   270
version_SVN
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
   271
    ^ '§Id: CmdLineOption.st 10737 2011-11-06 21:23:48Z vranyj1 §'
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   272
! !
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   273