Skip to content

Commit

Permalink
Rewritten arguments parsing to match specs
Browse files Browse the repository at this point in the history
  • Loading branch information
amercier committed Jul 31, 2013
1 parent ac9405b commit 5c1cacd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 55 deletions.
68 changes: 30 additions & 38 deletions src/Cli/Helpers/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
32 changes: 15 additions & 17 deletions tests/unit/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -28,7 +26,7 @@ public function testShortRequiredParameter()
'verbose' => false
),
Parameter::getFromCommandLine(
self::$parameters,
$this->getParameters(),
explode(' ', self::SCRIPT . ' -u myname -p mypassword')
)
);
Expand All @@ -44,7 +42,7 @@ public function testLongRequiredParameter()
'verbose' => false
),
Parameter::getFromCommandLine(
self::$parameters,
$this->getParameters(),
explode(' ', self::SCRIPT . ' --username myname --password mypassword')
)
);
Expand All @@ -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')
)
);
Expand All @@ -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')
)
);
Expand All @@ -92,7 +90,7 @@ public function testShortSwitchParameters()
'verbose' => true
),
Parameter::getFromCommandLine(
self::$parameters,
$this->getParameters(),
explode(' ', self::SCRIPT . ' -u myname -p mypassword -v')
)
);
Expand All @@ -108,7 +106,7 @@ public function testLongSwitchParameters()
'verbose' => true
),
Parameter::getFromCommandLine(
self::$parameters,
$this->getParameters(),
explode(' ', self::SCRIPT . ' -u myname -p mypassword --verbose')
)
);
Expand All @@ -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')
);
}
Expand All @@ -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')
);
}
Expand All @@ -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')
);
}
Expand All @@ -153,7 +151,7 @@ public function testConflictingSwitchParameter()
public function testMissingRequiredParameter()
{
Parameter::getFromCommandLine(
self::$parameters,
$this->getParameters(),
explode(' ', self::SCRIPT . '')
);
}
Expand All @@ -164,7 +162,7 @@ public function testMissingRequiredParameter()
public function testMissingRequiredParameter2()
{
Parameter::getFromCommandLine(
self::$parameters,
$this->getParameters(),
explode(' ', self::SCRIPT . ' -u myname')
);
}
Expand All @@ -175,7 +173,7 @@ public function testMissingRequiredParameter2()
public function testMissingParameterValue()
{
Parameter::getFromCommandLine(
self::$parameters,
$this->getParameters(),
explode(' ', self::SCRIPT . ' -u')
);
}
Expand All @@ -186,7 +184,7 @@ public function testMissingParameterValue()
public function testMissingParameterValue2()
{
Parameter::getFromCommandLine(
self::$parameters,
$this->getParameters(),
explode(' ', self::SCRIPT . ' --username')
);
}
Expand Down

0 comments on commit 5c1cacd

Please sign in to comment.