Skip to content

Commit 3528080

Browse files
committed
fixed optionsFirst bug
1 parent e4c9420 commit 3528080

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

src/docopt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ function parse_argv($tokens, \ArrayIterator $options, $optionsFirst=false)
987987
$parsed = array_merge($parsed, parse_shorts($tokens, $options));
988988
}
989989
elseif ($optionsFirst) {
990-
return array_merge($parsed, array_map(function($v) { return new Argument(null, $v); }, iterator_to_array($tokens)));
990+
return array_merge($parsed, array_map(function($v) { return new Argument(null, $v); }, $tokens->left()));
991991
}
992992
else {
993993
$parsed[] = new Argument(null, $tokens->move());

test/lib/PythonPortedTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -829,16 +829,26 @@ public function testIssue59()
829829

830830
public function testOptionsFirst()
831831
{
832-
$this->assertEquals($this->docopt('usage: prog [--opt] [<args>...]',
833-
'--opt this that')->args, array('--opt'=>true,
834-
'<args>'=>array('this', 'that')));
835-
$this->assertEquals($this->docopt('usage: prog [--opt] [<args>...]',
836-
'this that --opt')->args, array('--opt'=>true,
837-
'<args>'=>array('this', 'that')));
838-
$this->assertEquals($this->docopt('usage: prog [--opt] [<args>...]',
839-
'this that --opt',
840-
array('optionsFirst'=>true))->args, array('--opt'=>false,
841-
'<args>'=>array('this', 'that', '--opt')));
832+
$this->assertEquals(
833+
$this->docopt('usage: prog [--opt] [<args>...]', '--opt this that')->args,
834+
array('--opt'=>true, '<args>'=>array('this', 'that'))
835+
);
836+
837+
$this->assertEquals(
838+
$this->docopt('usage: prog [--opt] [<args>...]', 'this that --opt')->args,
839+
array('--opt'=>true, '<args>'=>array('this', 'that'))
840+
);
841+
842+
$this->assertEquals(
843+
$this->docopt('usage: prog [--opt] [<args>...]', 'this that --opt', array('optionsFirst'=>true))->args,
844+
array('--opt'=>false, '<args>'=>array('this', 'that', '--opt'))
845+
);
846+
847+
// found issue with PHP version in this situation
848+
$this->assertEquals(
849+
$this->docopt('usage: prog [--opt=<val>] [<args>...]', ' --opt=foo this that --opt', array('optionsFirst'=>true))->args,
850+
array('--opt'=>'foo', '<args>'=>array('this', 'that', '--opt'))
851+
);
842852
}
843853

844854
public function testIssue68OptionsShortcutDoesNotIncludeOptionsInUsagePattern()

0 commit comments

Comments
 (0)