src/CLIParser.php
private function parseShortOptionGroup($key)
    {
        $shortOptions = $this->shortOptions;

        // We parse every single character, remove it from the current argument
        // and wd DO NOT update index counter, unless we are parsing the last option
        $chars = str_split($key);

        foreach ($chars as $char) {
            // Test if is a valid option
            $cpos = strpos($shortOptions, $char);

            if (false === $cpos) {
                $this->optind++;
                return null;
            }

            // Is Valid option: set return value
            $option = $char;

            // Check if option accept a parameter
            if (isset($shortOptions[$cpos+1]) && $shortOptions[$cpos+1] === ':') {
                // Ok, current option accept a parameter, start parsing
                
                // Check n.1: if an option accepts a parameter, to be valid must be the last
                // in an option-group, for example:
                // YES: tar -cvzf filename.tar.gz
                // NO: tar -cvfz filename.tar.gz
                
                if (strpos($key, $char) < (strlen($key)-1)) {
                    // Current option is not the last, so the parameter is invalid
                    // return option with FALSE as argument
                    $this->optarg = false;

                    // Remove current option from $this->argv[$this->optind]
                    // so it's not counted on next loop
                    $this->argv[$this->optind] = str_replace($char, '', $this->argv[$this->optind]);
                    return $option;
                    
                }

                // Our option is the last, so check for a valid parameter
                
                if ($this->optind < $this->argc -1) {
                    // Get next argument from our copy of $argv
                    $optarg = (string) $this->argv[$this->optind + 1];

                    // If the argument is not an option (should not begin with - or --)
                    if (!($this->isLongOption($optarg)) && !($this->isShortOption($optarg))) {
                        // Option value is ok, set it and update index by 2 steps
                        $this->optarg = $optarg;
                        $this->optind += 2;
                        return $option;
                    }

                }

                // Option value missing, set it to FALSE and update index by 1 step only
                $this->optarg = false;
                $this->optind++;
                return $option;

            }

            // Current option do not accept parameters
            $this->optarg = true;
            
            // Remove current option from $this->argv[$this->optind]
            // so it's not counted on next loop
            $this->argv[$this->optind] = str_replace($char, '', $this->argv[$this->optind]);
            
            // Return the option value
            // Option index is updated (by 1) only if this is the last option
            if (strpos($key, $char) == (strlen($key)-1)) {
                $this->optind++;
            }
            return $option;
        }

        // If we reach here the chance is we didn't find any allowed option
        // so we return null
        if (strpos($key, $char) == (strlen($key)-1)) {
            $this->optind++;
        }
        return null;
    }
private function parseShortOptionGroup($key)
    {
        $shortOptions = $this->shortOptions;

        // We parse every single character, remove it from the current argument
        // and wd DO NOT update index counter, unless we are parsing the last option
        $chars = str_split($key);

        foreach ($chars as $char) {
            // Test if is a valid option
            $cpos = strpos($shortOptions, $char);

            if (false === $cpos) {
                $this->optind++;
                return null;
            }

            // Is Valid option: set return value
            $option = $char;

            // Check if option accept a parameter
            if (isset($shortOptions[$cpos+1]) && $shortOptions[$cpos+1] === ':') {
                // Ok, current option accept a parameter, start parsing
                
                // Check n.1: if an option accepts a parameter, to be valid must be the last
                // in an option-group, for example:
                // YES: tar -cvzf filename.tar.gz
                // NO: tar -cvfz filename.tar.gz
                
                if (strpos($key, $char) < (strlen($key)-1)) {
                    // Current option is not the last, so the parameter is invalid
                    // return option with FALSE as argument
                    $this->optarg = false;

                    // Remove current option from $this->argv[$this->optind]
                    // so it's not counted on next loop
                    $this->argv[$this->optind] = str_replace($char, '', $this->argv[$this->optind]);
                    return $option;
                    
                }

                // Our option is the last, so check for a valid parameter
                
                if ($this->optind < $this->argc -1) {
                    // Get next argument from our copy of $argv
                    $optarg = (string) $this->argv[$this->optind + 1];

                    // If the argument is not an option (should not begin with - or --)
                    if (!($this->isLongOption($optarg)) && !($this->isShortOption($optarg))) {
                        // Option value is ok, set it and update index by 2 steps
                        $this->optarg = $optarg;
                        $this->optind += 2;
                        return $option;
                    }

                }

                // Option value missing, set it to FALSE and update index by 1 step only
                $this->optarg = false;
                $this->optind++;
                return $option;

            }

            // Current option do not accept parameters
            $this->optarg = true;
            
            // Remove current option from $this->argv[$this->optind]
            // so it's not counted on next loop
            $this->argv[$this->optind] = str_replace($char, '', $this->argv[$this->optind]);
            
            // Return the option value
            // Option index is updated (by 1) only if this is the last option
            if (strpos($key, $char) == (strlen($key)-1)) {
                $this->optind++;
            }
            return $option;
        }

        // If we reach here the chance is we didn't find any allowed option
        // so we return null
        if (strpos($key, $char) == (strlen($key)-1)) {
            $this->optind++;
        }
        return null;
    }
private function parseShortOptionGroup ( $ key ) { $ shortOptions = $ this -> shortOptions ; // We parse every single character, remove it from the current argument // and wd DO NOT update index counter, unless we are parsing the last option $ chars = str_split ( $ key ) ; foreach ( $ chars as $ char ) { // Test if is a valid option $ cpos = strpos ( $ shortOptions , $ char ) ; if ( false === $ cpos ) { $ this -> optind ++ ; return null ; } // Is Valid option: set return value $ option = $ char ; // Check if option accept a parameter if ( isset ( $ shortOptions [ $ cpos + 1 ] ) && $ shortOptions [ $ cpos + 1 ] === ':' ) { // Ok, current option accept a parameter, start parsing // Check n.1: if an option accepts a parameter, to be valid must be the last // in an option-group, for example: // YES: tar -cvzf filename.tar.gz // NO: tar -cvfz filename.tar.gz if ( strpos ( $ key , $ char ) < ( strlen ( $ key ) - 1 ) ) { // Current option is not the last, so the parameter is invalid // return option with FALSE as argument $ this -> optarg = false ; // Remove current option from $this->argv[$this->optind] // so it's not counted on next loop $ this -> argv [ $ this -> optind ] = str_replace ( $ char , '' , $ this -> argv [ $ this -> optind ] ) ; return $ option ; } // Our option is the last, so check for a valid parameter if ( $ this -> optind < $ this -> argc - 1 ) { // Get next argument from our copy of $argv $ optarg = ( string ) $ this -> argv [ $ this -> optind + 1 ] ; // If the argument is not an option (should not begin with - or --) if ( ! ( $ this -> isLongOption ( $ optarg ) ) && ! ( $ this -> isShortOption ( $ optarg ) ) ) { // Option value is ok, set it and update index by 2 steps $ this -> optarg = $ optarg ; $ this -> optind += 2 ; return $ option ; } } // Option value missing, set it to FALSE and update index by 1 step only $ this -> optarg = false ; $ this -> optind ++ ; return $ option ; } // Current option do not accept parameters $ this -> optarg = true ; // Remove current option from $this->argv[$this->optind] // so it's not counted on next loop $ this -> argv [ $ this -> optind ] = str_replace ( $ char , '' , $ this -> argv [ $ this -> optind ] ) ; // Return the option value // Option index is updated (by 1) only if this is the last option if ( strpos ( $ key , $ char ) == ( strlen ( $ key ) - 1 ) ) { $ this -> optind ++ ; } return $ option ; } // If we reach here the chance is we didn't find any allowed option // so we return null if ( strpos ( $ key , $ char ) == ( strlen ( $ key ) - 1 ) ) { $ this -> optind ++ ; } return null ; }
Parse and extract the keys and values of a short options group

Only the last option in the group can have a value

@param string $key The options group string
@return string | NULL
Parse and extract the keys and values of a short options group