Skip to content

Commit

Permalink
Merge pull request #139 from getopt-php/issue-136
Browse files Browse the repository at this point in the history
accept any value for options with required argument
  • Loading branch information
tflori committed Jan 22, 2019
2 parents 6d22d6f + 93c41d6 commit 9df490d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,18 @@ public function process(GetOpt $getopt, callable $setOption, callable $setComman
}

if ($this->isLongOption($arg)) {
$setOption($this->longName($arg), function () use ($arg) {
return $this->value($arg);
$setOption($this->longName($arg), function (Option $option = null) use ($arg) {
return $this->value($arg, null, $option);
});
continue;
}

// the only left is short options
foreach ($this->shortNames($arg) as $name) {
$requestedValue = false;
$setOption($name, function () use ($arg, $name, &$requestedValue) {
$setOption($name, function (Option $option = null) use ($arg, $name, &$requestedValue) {
$requestedValue = true;
return $this->value($arg, $name);
return $this->value($arg, $name, $option);
});

if ($requestedValue) {
Expand Down Expand Up @@ -163,16 +163,20 @@ protected function shortNames($arg)
*
* @param string $arg
* @param string $name
* @param Option $option
* @return string
*/
protected function value($arg, $name = null)
protected function value($arg, $name = null, Option $option = null)
{
$p = strpos($arg, $this->isLongOption($arg) ? '=' : $name);
if ($this->isLongOption($arg) && $p || !$this->isLongOption($arg) && $p < strlen($arg)-1) {
return substr($arg, $p+1);
}

if (!empty($this->arguments) && $this->isValue($this->arguments[0])) {
if (!empty($this->arguments) && (
$option && in_array($option->getMode(), [GetOpt::REQUIRED_ARGUMENT, GetOpt::MULTIPLE_ARGUMENT]) ||
$this->isValue($this->arguments[0])
)) {
return array_shift($this->arguments);
}

Expand Down
2 changes: 1 addition & 1 deletion src/GetOpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function process($arguments = null)
}
}

$option->setValue($option->getMode() !== GetOpt::NO_ARGUMENT ? $getValue() : null);
$option->setValue($option->getMode() !== GetOpt::NO_ARGUMENT ? $getValue($option) : null);
};

$setCommand = function (Command $command) {
Expand Down
5 changes: 3 additions & 2 deletions test/ArgumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,15 @@ public function parseLongOptionWithValueStartingWithHyphen()
}

/** @test */
public function parseNoValueStartingWithHyphenRequired()
public function parseValueStartingWithHypenRequired()
{
$this->setExpectedException('GetOpt\ArgumentException\Missing');
$this->getopt->addOptions([
new Option('a', null, GetOpt::REQUIRED_ARGUMENT),
new Option('b', null)
]);
$this->getopt->process('-a -b');

self::assertEquals('-b', $this->getopt->getOption('a'));
}

/** @test */
Expand Down

0 comments on commit 9df490d

Please sign in to comment.