CmdLineOption.st
author Jan Vrany <jan.vrany@labware.com>
Tue, 01 Jun 2021 20:19:13 +0100
branchjv
changeset 25424 51bd8a6b196f
parent 23547 c69c97cec351
permissions -rw-r--r--
Cherry-picked `Context` cherry-picked Context.st from a6b6dda4caff: * 4aaf30c174e9: #DOCUMENTATION by cg, Claus Gittinger <cg@exept.de> * c67311afcc6c: #OTHER by cg, Claus Gittinger <cg@exept.de> * 883f79e7b2a6: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 716f3fbb09e9: Don't mark contexts with `CATCHMARK`, Jan Vrany <jan.vrany@fit.cvut.cz> * cff24fa817b0: #REFACTORING by stefan, Stefan Vogel <sv@exept.de> * 521f0d837330: #UI_ENHANCEMENT by cg, Claus Gittinger <cg@exept.de> * bf1118f0fcca: #UI_ENHANCEMENT by cg, Claus Gittinger <cg@exept.de> * e587cdd22868: #BUGFIX by cg, Claus Gittinger <cg@exept.de> * fe9f9487a3ed: #DOCUMENTATION by cg, Claus Gittinger <cg@exept.de> * d5b781899274: #BUGFIX by cg, Claus Gittinger <cg@exept.de> * 8258751a7465: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 40173e082cbc: Copyright updates, Jan Vrany <jan.vrany@fit.cvut.cz> * 6db5c28207d5: #UI_ENHANCEMENT by cg, Claus Gittinger <cg@exept.de> * 871ea64fd5dc: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 4b544a108e4e: #DOCUMENTATION by cg, Claus Gittinger <cg@exept.de> * 9a8d8399e566: #FEATURE by cgexept.de, Claus Gittinger <cg@exept.de> * 170b00be0103: #BUGFIX by stefan, Stefan Vogel <sv@exept.de> * a6c73965eae8: #FEATURE by cg, Claus Gittinger <cg@exept.de> * ce2a0e462ff0: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 46a260a9ca92: #FEATURE by cg, Claus Gittinger <cg@exept.de> * 46cab49167fb: #UI_ENHANCEMENT by exept, Claus Gittinger <cg@exept.de> * 7d52dfd3997d: #DOCUMENTATION by exept, Claus Gittinger <cg@exept.de> * c52eeea62763: Fix `Context >> argAndVarNames` in cases when debug info is not available, Jan Vrany <jan.vrany@labware.com> * b5d6963fe4a9: Backed out changeset c52eeea62763, Jan Vrany <jan.vrany@labware.com> * 6fd3896f8703: #FEATURE by exept, Claus Gittinger <cg@exept.de> * b530ee616256: #REFACTORING by cg, Claus Gittinger <cg@exept.de> * ef9b481d7498: #FEATURE by cg, Claus Gittinger <cg@exept.de> * ea663b72bd51: #UI_ENHANCEMENT by cg, Claus Gittinger <cg@exept.de> * 6179572a733c: #FEATURE by exept, Claus Gittinger <cg@exept.de> * 84155b1b6622: #DOCUMENTATION by exept, Claus Gittinger <cg@exept.de> * 37d06602d856: *** empty log message ***, Claus Gittinger <cg@exept.de> * f927b9022fea: *** empty log message ***, Claus Gittinger <cg@exept.de> * 427d3be62d97: #UI_ENHANCEMENT by exept, Claus Gittinger <cg@exept.de>

"{ Encoding: utf8 }"

"
 COPYRIGHT (c) 2006 by eXept Software AG
 COPYRIGHT (c) 2009 Jan Vrany
 COPYRIGHT (c) 2016 Jan Vrany
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
"{ Package: 'stx:libbasic' }"

"{ NameSpace: Smalltalk }"

Object subclass:#CmdLineOption
	instanceVariableNames:'action description short shortSpec long longSpec'
	classVariableNames:''
	poolDictionaries:''
	category:'System-Support-Command line'
!

!CmdLineOption class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2006 by eXept Software AG
 COPYRIGHT (c) 2009 Jan Vrany
 COPYRIGHT (c) 2016 Jan Vrany
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

optionsFor: anObject

    "Returns a collection of command line options for
     given object."

    ^anObject class allSelectors asSet 
                select:[:sel|sel startsWith: 'cmdlineOption']
                thenCollect:[:sel|anObject perform: sel].
! !

!CmdLineOption methodsFor:'accessing'!

action
    ^ action

    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

action:aBlockOrMessageSend

    aBlockOrMessageSend numArgs > 1 ifTrue:
        [CmdLineOptionError raiseErrorString: 'Action must be zero-or-one arg block/message send'].    
    action := aBlockOrMessageSend.

    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 16-06-2009 / 15:46:54 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

description
    ^ description

    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

description:aString
    description := aString.

    "Created: / 28-01-2009 / 11:49:55 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

long
    ^ long
!

long:aString
    long := aString.
!

short
    ^ short
!

short:aCharacter

    (aCharacter isCharacter 
        and:[aCharacter isLetter or:[aCharacter isDigit]])
            ifTrue:[short := aCharacter]
            ifFalse:[self error: 'short option name should be alphanumeric character']

    "Modified: / 29-05-2009 / 16:05:46 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 07-09-2016 / 16:25:53 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

spec: spec
    "Build an option from option specification"

    long := short := nil.
    (spec isCollection and:[ spec isString not ]) ifTrue:[ 
        spec do:[:each | self spec0: each ]
    ] ifFalse:[ 
        self spec0: spec.
    ].

    "Created: / 14-06-2016 / 06:46:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CmdLineOption methodsFor:'parsing'!

parseL: argv startingAt: index equalCharPosition: equalPos
    "Parse a long option from argv"

    self hasParam ifTrue:[ 
        " Determine whether to parse (GNU-style ?) `--long-option=param` or
          just `--long-option param`."
        (longSpec isNil or:[longSpec includes: $=]) ifTrue:[
            equalPos == 0 ifTrue:[
                ^CmdLineOptionError signal:('Option --%',long,' requires argument').
            ] ifFalse:[
                self process: ((argv at: index) copyFrom: equalPos + 1).
            ].
            ^ index + 1.
        ] ifFalse:[
            index < argv size ifTrue:[ 
                self process: (argv at: index + 1).
                ^ index + 2.
            ] ifFalse:[ 
                 ^CmdLineOptionError signal:('Option --%',long,' requires argument')
            ].
        ]
    ] ifFalse:[ 
        self process.
        ^ index + 1
    ].

    "Created: / 29-06-2016 / 17:00:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 07-09-2016 / 16:55:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CmdLineOption methodsFor:'printing & storing'!

printOn: stream

    super printOn: stream.
    stream nextPut:$(.
    short ifNotNil:[stream nextPut: $-; nextPut: short].
    (short notNil and: [long notNil]) ifTrue:[stream nextPut:$|].
    long ifNotNil:[stream nextPut: $-;  nextPut: $-; nextPutAll: long].
    stream nextPut:$)

    "Created: / 08-06-2009 / 14:48:59 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CmdLineOption methodsFor:'private'!

spec0:aStringOrCharacter
    aStringOrCharacter isCharacter ifTrue:[ 
        self specS: '-' , aStringOrCharacter asString.  
        ^ self.
    ].
    aStringOrCharacter isString ifTrue:[ 
        aStringOrCharacter first == $- ifTrue:[ 
            aStringOrCharacter second == $- ifTrue:[ 
                self specL: aStringOrCharacter.  
                ^ self.
            ] ifFalse:[ 
                (aStringOrCharacter size == 2 and:[ aStringOrCharacter second isLetter or:[ aStringOrCharacter second isDigit ]]) ifTrue:[ 
                    self specS: aStringOrCharacter.
                    ^ self.
                ].
            ].
        ] ifFalse:[ 
            self specL: aStringOrCharacter.
            ^ self
        ].
    ].
    self error: 'Invalid option specification: ' , aStringOrCharacter asString.

    "Created: / 14-06-2016 / 06:46:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 07-09-2016 / 16:55:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

specL:aString
    | firstCharPos lastCharPos |

    long notNil ifTrue: [ self error: 'Long option already specified: ', short asString ].
    firstCharPos := 1.
    (aString first == $- and:[ aString second == $- ]) ifTrue:[ 
        firstCharPos := 3.
    ].
    lastCharPos := aString indexOf: $=.
    lastCharPos == 0 ifTrue:[ 
        lastCharPos := aString indexOf: Character space.
        lastCharPos == 0 ifTrue:[
            lastCharPos := aString size.
        ] ifFalse:[ 
            lastCharPos := lastCharPos - 1.
        ].
    ] ifFalse:[ 
        lastCharPos := lastCharPos - 1.
    ].
    (firstCharPos ~~ 1 or:[ lastCharPos ~~ aString size ])
        ifTrue:[ long := aString copyFrom: firstCharPos to: lastCharPos ]
        ifFalse:[ long := aString ].
    (long conform: [:c | c == $- or:[c isLetter or:[c isDigit]]]) ifFalse:[ 
        long := nil.
        self error: 'Invalid option specification: ' , aString asString.
        ^ self.
    ].
    longSpec := aString.
    (longSpec first == $- and:[ longSpec second == $- ]) ifFalse:[ 
        longSpec := '--' , longSpec
    ].

    "Created: / 29-06-2016 / 09:24:44 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 07-09-2016 / 16:52:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

specS:aString
    short notNil ifTrue:[ self error: 'Short option already specified: ', short asString ].
    short := aString second.
    shortSpec := aString.

    "Created: / 29-06-2016 / 09:23:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!CmdLineOption methodsFor:'processing'!

process

    action value

    "Created: / 08-06-2009 / 14:35:01 / Jan Vrany <vranyj1@fel.cvut.cz>"
!

process: value

    action value: value

    "Created: / 08-06-2009 / 14:35:08 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CmdLineOption methodsFor:'queries'!

hasParam

    ^action numArgs = 1

    "Created: / 08-06-2009 / 13:45:41 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!CmdLineOption class methodsFor:'documentation'!

version
    ^'$Header: /cvs/stx/stx/libbasic/CmdLineOption.st,v 1.3 2012-01-13 10:58:29 vrany Exp $'
!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '§Id: CmdLineOption.st 10737 2011-11-06 21:23:48Z vranyj1 §'
! !