src/Clapp/CommandLineArgumentDefinition.php
protected function parseDefinitions()
    {
        foreach ($this->definitions as $nameDef => $description) {
            $nameParts = explode("|", $nameDef);

            if (sizeof($nameParts) !== 2) {
                throw new \UnexpectedValueException("Unexpected argument name definition expecting \"longName|char\"");
            } //if

            $longName = $nameParts[0];
            $isMulti = false;
            $parameterType = false;

            $shortNameLength = strlen($nameParts[1]);

            if ($shortNameLength == 1) {
                $shortName = $nameParts[1];
            } else {
                $secondChar = substr($nameParts[1], 1, 1);

                switch ($secondChar) {
                    case '=':
                        $shortNameParts = explode("=", $nameParts[1]);

                        $shortName = $shortNameParts[0];
                        $parameterTypeString = $shortNameParts[1];

                        if (substr($parameterTypeString, -1) === '+') {
                            $isMulti = true;
                            $parameterTypeString = substr($parameterTypeString, 0, -1); // remove trailing +
                        } //if

                        switch ($parameterTypeString) {
                            case 'i':
                            case 'int':
                            case 'integer':
                                $parameterType = 'integer';
                                break;
                            case 's':
                            case 'str':
                            case 'string':
                                $parameterType = 'string';
                                break;
                            default:
                                throw new \UnexpectedValueException("Expecting parameter type".
                                   " to be either integer or string");
                                break;
                        } //switch

                        break;
                    case '+':
                        if ($shortNameLength > 2) {
                            throw new \UnexpectedValueException("Multiple flag charachter (+)".
                               " should be last character in definition");
                        } //if

                        $shortName = substr($nameParts[1], 0, 1);
                        $isMulti = true;

                        break;
                    default:
                        throw new \UnexpectedValueException("Expecting short name definition to be a single char");
                        break;
                } // switch

            } //if

            if (isset($this->longNames[$longName])) {
                throw new \UnexpectedValueException("Cannot redefine long name {$longName}");
            } //if

            if (isset($this->shortNames[$shortName])) {
                throw new \UnexpectedValueException("Cannot redefine short name {$shortName}");
            } //if

            $this->longNames[$longName] = array(
                'shortName' => $shortName,
                'isMultipleAllowed' => $isMulti,
                'parameterType' => $parameterType,
                'description' => $description
            );

            $this->shortNames[$shortName] = $longName;

        } //foreach

        $this->isParsed = true;
    }
protected function parseDefinitions()
    {
        foreach ($this->definitions as $nameDef => $description) {
            $nameParts = explode("|", $nameDef);

            if (sizeof($nameParts) !== 2) {
                throw new \UnexpectedValueException("Unexpected argument name definition expecting \"longName|char\"");
            } //if

            $longName = $nameParts[0];
            $isMulti = false;
            $parameterType = false;

            $shortNameLength = strlen($nameParts[1]);

            if ($shortNameLength == 1) {
                $shortName = $nameParts[1];
            } else {
                $secondChar = substr($nameParts[1], 1, 1);

                switch ($secondChar) {
                    case '=':
                        $shortNameParts = explode("=", $nameParts[1]);

                        $shortName = $shortNameParts[0];
                        $parameterTypeString = $shortNameParts[1];

                        if (substr($parameterTypeString, -1) === '+') {
                            $isMulti = true;
                            $parameterTypeString = substr($parameterTypeString, 0, -1); // remove trailing +
                        } //if

                        switch ($parameterTypeString) {
                            case 'i':
                            case 'int':
                            case 'integer':
                                $parameterType = 'integer';
                                break;
                            case 's':
                            case 'str':
                            case 'string':
                                $parameterType = 'string';
                                break;
                            default:
                                throw new \UnexpectedValueException("Expecting parameter type".
                                   " to be either integer or string");
                                break;
                        } //switch

                        break;
                    case '+':
                        if ($shortNameLength > 2) {
                            throw new \UnexpectedValueException("Multiple flag charachter (+)".
                               " should be last character in definition");
                        } //if

                        $shortName = substr($nameParts[1], 0, 1);
                        $isMulti = true;

                        break;
                    default:
                        throw new \UnexpectedValueException("Expecting short name definition to be a single char");
                        break;
                } // switch

            } //if

            if (isset($this->longNames[$longName])) {
                throw new \UnexpectedValueException("Cannot redefine long name {$longName}");
            } //if

            if (isset($this->shortNames[$shortName])) {
                throw new \UnexpectedValueException("Cannot redefine short name {$shortName}");
            } //if

            $this->longNames[$longName] = array(
                'shortName' => $shortName,
                'isMultipleAllowed' => $isMulti,
                'parameterType' => $parameterType,
                'description' => $description
            );

            $this->shortNames[$shortName] = $longName;

        } //foreach

        $this->isParsed = true;
    }
protected function parseDefinitions ( ) { foreach ( $ this -> definitions as $ nameDef => $ description ) { $ nameParts = explode ( "|" , $ nameDef ) ; if ( sizeof ( $ nameParts ) !== 2 ) { throw new \ UnexpectedValueException ( "Unexpected argument name definition expecting \"longName|char\"" ) ; } //if $ longName = $ nameParts [ 0 ] ; $ isMulti = false ; $ parameterType = false ; $ shortNameLength = strlen ( $ nameParts [ 1 ] ) ; if ( $ shortNameLength == 1 ) { $ shortName = $ nameParts [ 1 ] ; } else { $ secondChar = substr ( $ nameParts [ 1 ] , 1 , 1 ) ; switch ( $ secondChar ) { case '=' : $ shortNameParts = explode ( "=" , $ nameParts [ 1 ] ) ; $ shortName = $ shortNameParts [ 0 ] ; $ parameterTypeString = $ shortNameParts [ 1 ] ; if ( substr ( $ parameterTypeString , - 1 ) === '+' ) { $ isMulti = true ; $ parameterTypeString = substr ( $ parameterTypeString , 0 , - 1 ) ; // remove trailing + } //if switch ( $ parameterTypeString ) { case 'i' : case 'int' : case 'integer' : $ parameterType = 'integer' ; break ; case 's' : case 'str' : case 'string' : $ parameterType = 'string' ; break ; default : throw new \ UnexpectedValueException ( "Expecting parameter type" . " to be either integer or string" ) ; break ; } //switch break ; case '+' : if ( $ shortNameLength > 2 ) { throw new \ UnexpectedValueException ( "Multiple flag charachter (+)" . " should be last character in definition" ) ; } //if $ shortName = substr ( $ nameParts [ 1 ] , 0 , 1 ) ; $ isMulti = true ; break ; default : throw new \ UnexpectedValueException ( "Expecting short name definition to be a single char" ) ; break ; } // switch } //if if ( isset ( $ this -> longNames [ $ longName ] ) ) { throw new \ UnexpectedValueException ( "Cannot redefine long name {$longName}" ) ; } //if if ( isset ( $ this -> shortNames [ $ shortName ] ) ) { throw new \ UnexpectedValueException ( "Cannot redefine short name {$shortName}" ) ; } //if $ this -> longNames [ $ longName ] = array ( 'shortName' => $ shortName , 'isMultipleAllowed' => $ isMulti , 'parameterType' => $ parameterType , 'description' => $ description ) ; $ this -> shortNames [ $ shortName ] = $ longName ; } //foreach $ this -> isParsed = true ; }
parses the definitions

@author Patrick Forget <patforg at webtrendi.com>
parses the definitions