diff --git a/src/Cli/Helpers/Parameter.php b/src/Cli/Helpers/Parameter.php index ad5fbe2..499e66c 100644 --- a/src/Cli/Helpers/Parameter.php +++ b/src/Cli/Helpers/Parameter.php @@ -105,35 +105,49 @@ public function getLong() return $this->long; } - public function getValue ($rawOptions, $arguments) + public function getShortSwitch() { - // Prevent short and long options simultaneously - if (array_key_exists($this->getShort(), $rawOptions) && array_key_exists($this->getLong(), $rawOptions)) { - throw new Exception\ConflictingParameters($this, $arguments); + return '-' . $this->getShort(); + } + + public function getLongSwitch() + { + return '--' . $this->getLong(); + } + + public function getValue ($arguments) + { + // Parse arguments + $index = -1; + foreach ($arguments as $i => $value) { + if ($i !== 0 && ($value === $this->getShortSwitch() || $value === $this->getLongSwitch())) { + + // Prevent short and long options simultaneously + if ($index !== -1) { + throw new Exception\ConflictingParameters($this, $arguments); + } + + $index = $i; + } } // If it's a switch parameter (ex: -v/--verbose) return true if it was given, false otherwise if ($this->defaultValue === self::VALUE_NO_VALUE) { - return array_key_exists($this->getShort(), $rawOptions) || array_key_exists($this->getLong(), $rawOptions); + return $index !== -1; } - // If it's a value parameter (ex: -h/--host 127.0.0.1)... // Return the value if it exists - if (array_key_exists($this->getShort(), $rawOptions)) { - return $rawOptions[ $this->getShort() ]; - } - - if (array_key_exists($this->getLong(), $rawOptions)) { - return $rawOptions[ $this->getLong() ]; + if ($index !== -1 && $index < count($arguments) - 1) { + return $arguments[ $index + 1 ]; } // No value if ($this->defaultValue === self::VALUE_REQUIRED) { // required - throw in_array('-' . $this->getShort(), $arguments) || in_array('--' . $this->getLong(), $arguments) - ? new Exception\MissingParameterValue($this, $arguments) - : new Exception\MissingRequiredParameter($this, $arguments); + throw $index === -1 + ? new Exception\MissingRequiredParameter($this, $arguments) + : new Exception\MissingParameterValue($this, $arguments); } else { // default value exists return $this->defaultValue; @@ -143,31 +157,9 @@ public function getValue ($rawOptions, $arguments) public static function getFromCommandLine (array $parameters, $arguments = null) { global $argv; - $options = array(); - - $rawOptions = self::getOptions($arguments === null ? $argv : $arguments); - $options = array(); foreach ($parameters as $key => $parameter) { - $options[ $key ] = $parameter->getValue($rawOptions, $arguments === null ? $argv : $arguments); - } - - return $options; - } - - protected static function getOptions ($arguments) - { - $options = array(); - for ($i = 1; $i < count($arguments); $i++) { - if (preg_match('/^--?(.*)/', $arguments[$i], $matches)) { - if ($i < count($arguments) - 1) { - $options[ $matches[1] ] = $arguments[$i+1]; - continue; - continue; - } else { - $options[ $matches[1] ] = true; - } - } + $options[ $key ] = $parameter->getValue($arguments === null ? $argv : $arguments); } return $options; diff --git a/tests/unit/ParameterTest.php b/tests/unit/ParameterTest.php index 722fc78..7e29b7b 100644 --- a/tests/unit/ParameterTest.php +++ b/tests/unit/ParameterTest.php @@ -6,11 +6,9 @@ class ParameterUnitTestCase extends PHPUnit_Framework_TestCase { const SCRIPT = 'data/test-parameters.php'; - protected static $parameters; - - public static function setUpBeforeClass() + public function getParameters() { - self::$parameters = array( + return array( 'host' => new Parameter('h', 'host' , '127.0.0.1'), 'username' => new Parameter('u', 'username', Parameter::VALUE_REQUIRED), 'password' => new Parameter('p', 'password', Parameter::VALUE_REQUIRED), @@ -28,7 +26,7 @@ public function testShortRequiredParameter() 'verbose' => false ), Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname -p mypassword') ) ); @@ -44,7 +42,7 @@ public function testLongRequiredParameter() 'verbose' => false ), Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' --username myname --password mypassword') ) ); @@ -60,7 +58,7 @@ public function testShortOptionalParameter() 'verbose' => false ), Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname -p mypassword -h myserver.example.com') ) ); @@ -76,7 +74,7 @@ public function testLongOptionalParameter() 'verbose' => false ), Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname -p mypassword --host myserver.example.com') ) ); @@ -92,7 +90,7 @@ public function testShortSwitchParameters() 'verbose' => true ), Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname -p mypassword -v') ) ); @@ -108,7 +106,7 @@ public function testLongSwitchParameters() 'verbose' => true ), Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname -p mypassword --verbose') ) ); @@ -120,7 +118,7 @@ public function testLongSwitchParameters() public function testConflictingRequiredParameter() { Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname -p mypassword --username myname') ); } @@ -131,7 +129,7 @@ public function testConflictingRequiredParameter() public function testConflictingOptionalParameter() { Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname -p mypassword -h myserver.example.com --host myserver.example.com') ); } @@ -142,7 +140,7 @@ public function testConflictingOptionalParameter() public function testConflictingSwitchParameter() { Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname -p mypassword -v --verbose') ); } @@ -153,7 +151,7 @@ public function testConflictingSwitchParameter() public function testMissingRequiredParameter() { Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . '') ); } @@ -164,7 +162,7 @@ public function testMissingRequiredParameter() public function testMissingRequiredParameter2() { Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u myname') ); } @@ -175,7 +173,7 @@ public function testMissingRequiredParameter2() public function testMissingParameterValue() { Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' -u') ); } @@ -186,7 +184,7 @@ public function testMissingParameterValue() public function testMissingParameterValue2() { Parameter::getFromCommandLine( - self::$parameters, + $this->getParameters(), explode(' ', self::SCRIPT . ' --username') ); }