CmdLineOption.st
author Jan Vrany <jan.vrany@labware.com>
Thu, 25 Mar 2021 20:30:03 +0000
branchjv
changeset 25411 248600ba8fd9
parent 23547 c69c97cec351
permissions -rw-r--r--
Fix unlikely but possible race in `WeakValueDictionary` It may happen that value in `valueArray` could have been already collected by the GC but #clearDeadSlots have not yet been called. When this happened, `#at:ifAbsentPut:` returned tombstone rather than updating the dictionary with value from block. This commit fixes this by checking whether `valueArray` contain the tombstone and if so, clearing up the dead slots and restarting the operation. HTH.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23547
c69c97cec351 Issue #239: Fix all Smalltak/X source files to be in unicode (UTF8 without BOM) and prefixed by "{ Encoding: utf8 }" when any unicode character is present
Patrik Svestka <patrik.svestka@gmail.com>
parents: 23107
diff changeset
     1
"{ Encoding: utf8 }"
c69c97cec351 Issue #239: Fix all Smalltak/X source files to be in unicode (UTF8 without BOM) and prefixed by "{ Encoding: utf8 }" when any unicode character is present
Patrik Svestka <patrik.svestka@gmail.com>
parents: 23107
diff changeset
     2
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
     3
"
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
     4
 COPYRIGHT (c) 2006 by eXept Software AG
23107
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 20396
diff changeset
     5
 COPYRIGHT (c) 2009 Jan Vrany
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 20396
diff changeset
     6
 COPYRIGHT (c) 2016 Jan Vrany
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
     7
              All Rights Reserved
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
     8
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
     9
 This software is furnished under a license and may be used
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    10
 only in accordance with the terms of that license and with the
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    11
 inclusion of the above copyright notice.   This software may not
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    12
 be provided or otherwise made available to, or used by, any
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    13
 other person.  No title to or ownership of the software is
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    14
 hereby transferred.
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    15
"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    16
"{ Package: 'stx:libbasic' }"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    17
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
    18
"{ NameSpace: Smalltalk }"
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
    19
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    20
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
    21
	instanceVariableNames:'action description short shortSpec long longSpec'
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    22
	classVariableNames:''
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    23
	poolDictionaries:''
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    24
	category:'System-Support-Command line'
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    25
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    26
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    27
!CmdLineOption class methodsFor:'documentation'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    28
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    29
copyright
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    30
"
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    31
 COPYRIGHT (c) 2006 by eXept Software AG
23107
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 20396
diff changeset
    32
 COPYRIGHT (c) 2009 Jan Vrany
40173e082cbc Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 20396
diff changeset
    33
 COPYRIGHT (c) 2016 Jan Vrany
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    34
              All Rights Reserved
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    35
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    36
 This software is furnished under a license and may be used
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    37
 only in accordance with the terms of that license and with the
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    38
 inclusion of the above copyright notice.   This software may not
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    39
 be provided or otherwise made available to, or used by, any
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    40
 other person.  No title to or ownership of the software is
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    41
 hereby transferred.
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    42
"
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    43
!
13482
769530195abb copyright
Claus Gittinger <cg@exept.de>
parents: 13402
diff changeset
    44
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    45
optionsFor: anObject
13482
769530195abb copyright
Claus Gittinger <cg@exept.de>
parents: 13402
diff changeset
    46
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    47
    "Returns a collection of command line options for
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    48
     given object."
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    49
20396
dd4549cee94c Command line parser refactored to be "more portable" to other dialects.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 20077
diff changeset
    50
    ^anObject class allSelectors asSet 
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    51
                select:[:sel|sel startsWith: 'cmdlineOption']
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
    52
                thenCollect:[:sel|anObject perform: sel].
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    53
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    54
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    55
!CmdLineOption methodsFor:'accessing'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    56
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    57
action
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    58
    ^ action
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    59
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    60
    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    61
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    62
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    63
action:aBlockOrMessageSend
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    64
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    65
    aBlockOrMessageSend numArgs > 1 ifTrue:
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    66
        [CmdLineOptionError raiseErrorString: 'Action must be zero-or-one arg block/message send'].    
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    67
    action := aBlockOrMessageSend.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    68
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    69
    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    70
    "Modified: / 16-06-2009 / 15:46:54 / 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
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    74
    ^ description
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
description:aString
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    80
    description := aString.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    81
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    82
    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    83
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    84
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    85
long
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    86
    ^ long
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    87
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    88
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    89
long:aString
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    90
    long := aString.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    91
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    92
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    93
short
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    94
    ^ short
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    95
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    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
short:aCharacter
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
    98
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
    (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
   100
        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
   101
            ifTrue:[short := aCharacter]
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   102
            ifFalse:[self error: 'short option name should be alphanumeric character']
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   103
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   104
    "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
   105
    "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
   106
!
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   107
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   108
spec: spec
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   109
    "Build an option from option specification"
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
    long := short := nil.
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   112
    (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
   113
        spec do:[:each | self spec0: each ]
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   114
    ] ifFalse:[ 
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   115
        self spec0: spec.
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   116
    ].
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   117
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   118
    "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
   119
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   120
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
   121
!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
   122
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
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
   124
    "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
   125
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
    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
   127
        " 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
   128
          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
   129
        (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
   130
            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
   131
                ^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
   132
            ] 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
   133
                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
   134
            ].
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 + 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
   136
        ] 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
   137
            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
   138
                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
   139
                ^ 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
   140
            ] 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
   141
                 ^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
   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
        ]
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
    ] 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
   145
        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
   146
        ^ 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
   147
    ].
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
   148
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
    "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
   150
    "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
   151
! !
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
   152
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   153
!CmdLineOption methodsFor:'printing & storing'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   154
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   155
printOn: stream
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   156
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   157
    super printOn: stream.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   158
    stream nextPut:$(.
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   159
    short ifNotNil:[stream nextPut: $-; nextPut: short].
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   160
    (short notNil and: [long notNil]) ifTrue:[stream nextPut:$|].
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   161
    long ifNotNil:[stream nextPut: $-;  nextPut: $-; nextPutAll: long].
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   162
    stream nextPut:$)
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   163
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   164
    "Created: / 08-06-2009 / 14:48:59 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   165
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   166
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   167
!CmdLineOption methodsFor:'private'!
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   168
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   169
spec0:aStringOrCharacter
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   170
    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
   171
        self specS: '-' , aStringOrCharacter asString.  
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   172
        ^ self.
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   173
    ].
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   174
    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
   175
        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
   176
            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
   177
                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
   178
                ^ self.
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   179
            ] 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
   180
                (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
   181
                    self specS: aStringOrCharacter.
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   182
                    ^ self.
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
            ].
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
   185
        ] 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
   186
            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
   187
            ^ self
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   188
        ].
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
    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
   191
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   192
    "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
   193
    "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
   194
!
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
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
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
   197
    | 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
   198
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
    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
   200
    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
   201
    (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
   202
        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
   203
    ].
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: $=.
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 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
   207
        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
   208
            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
   209
        ] 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
   210
            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
   211
        ].
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
    ] 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
   213
        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
   214
    ].
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
    (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
   216
        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
   217
        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
   218
    (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
   219
        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
   220
        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
   221
        ^ 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
   222
    ].
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 := 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
   224
    (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
   225
        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
   226
    ].
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
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
    "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
   229
    "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
   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
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
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
   233
    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
   234
    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
   235
    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
   236
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
   237
    "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
   238
! !
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   239
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   240
!CmdLineOption methodsFor:'processing'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   241
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   242
process
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   243
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   244
    action value
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   245
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   246
    "Created: / 08-06-2009 / 14:35:01 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   247
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   248
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   249
process: value
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   250
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   251
    action value: value
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   252
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   253
    "Created: / 08-06-2009 / 14:35:08 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   254
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   255
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   256
!CmdLineOption methodsFor:'queries'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   257
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   258
hasParam
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   259
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   260
    ^action numArgs = 1
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   261
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   262
    "Created: / 08-06-2009 / 13:45:41 / Jan Vrany <vranyj1@fel.cvut.cz>"
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   263
! !
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   264
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   265
!CmdLineOption class methodsFor:'documentation'!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   266
13938
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
   267
version
c7c6a5463841 Updated from SVN
vrany
parents: 13482
diff changeset
   268
    ^'$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
   269
!
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   270
20076
ff2f4d06a2fa CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 19988
diff changeset
   271
version_HG
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
    ^ '$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
   274
!
ff2f4d06a2fa CmdLineParser refactored and added an option to ignore unknown options
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 19988
diff changeset
   275
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   276
version_SVN
23547
c69c97cec351 Issue #239: Fix all Smalltak/X source files to be in unicode (UTF8 without BOM) and prefixed by "{ Encoding: utf8 }" when any unicode character is present
Patrik Svestka <patrik.svestka@gmail.com>
parents: 23107
diff changeset
   277
    ^ '§Id: CmdLineOption.st 10737 2011-11-06 21:23:48Z vranyj1 §'
13402
2d18a79f3fcc Added command line parsing stuff
vrany
parents:
diff changeset
   278
! !
19988
148721e96482 Added convenience methods to build CmdLineOptions.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 18011
diff changeset
   279