Skip to content

Commit

Permalink
Revert 2bd797f commit & deprecated exception fix
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k committed Apr 13, 2023
1 parent 8f79aa3 commit 44c46ce
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/ContinuousOptionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
*
* // command arguments
* $arguments = array();
*
*
* $argv = explode(' ','-v -d -c subcommand1 -a -b -c subcommand2 -c subcommand3 arg1 arg2 arg3');
*
* // parse application options first
Expand Down Expand Up @@ -139,7 +139,7 @@ public function continueParse()

protected function fillDefaultValues(OptionCollection $opts, OptionResult $result)
{
// register option result from options with default value
// register option result from options with default value
foreach ($opts as $opt) {
if ($opt->value === null && $opt->defaultValue !== null) {
$opt->setValue($opt->getDefaultValue());
Expand All @@ -153,7 +153,7 @@ public function parse(array $argv)
{
// create new Result object.
$result = new OptionResult();
list($this->argv, $extra) = $this->preprocessingArguments($argv);
$this->argv = $this->preprocessingArguments($argv);
$this->length = count($this->argv);

// from last parse index
Expand Down
17 changes: 8 additions & 9 deletions src/OptionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function setSpecs(OptionCollection $specs)
*/
protected function consumeOptionToken(Option $spec, $arg, $next, & $success = false)
{
// Check options doesn't require next token before
// Check options doesn't require next token before
// all options that require values.
if ($spec->isFlag()) {

Expand Down Expand Up @@ -70,11 +70,11 @@ protected function consumeOptionToken(Option $spec, $arg, $next, & $success = fa
$spec->setValue($next->arg);
return 1;

}
}
return 0;
}

/*
/*
* push value to multipl value option
*/
protected function pushOptionValue(Option $spec, $arg, $next)
Expand All @@ -94,15 +94,14 @@ protected function preprocessingArguments(array $argv)
{
// preprocessing arguments
$newArgv = array();
$extra = array();
$afterDash = false;
foreach ($argv as $arg) {
if ($arg === '--') {
$afterDash = true;
continue;
}
if ($afterDash) {
$extra[] = $arg;
$newArgv[] = $arg;
continue;
}

Expand All @@ -111,15 +110,15 @@ protected function preprocessingArguments(array $argv)
list($opt, $val) = $a->splitAsOption();
array_push($newArgv, $opt, $val);
} else {
$newArgv[] = $arg;
array_push($newArgv, $arg);
}
}
return array($newArgv, $extra);
return $newArgv;
}

protected function fillDefaultValues(OptionCollection $opts, OptionResult $result)
{
// register option result from options with default value
// register option result from options with default value
foreach ($opts as $opt) {
if ($opt->value === null && $opt->defaultValue !== null) {
$opt->setValue($opt->getDefaultValue());
Expand All @@ -141,7 +140,7 @@ public function parse(array $argv)
{
$result = new OptionResult();

list($argv, $extra) = $this->preprocessingArguments($argv);
$argv = $this->preprocessingArguments($argv);

$len = count($argv);

Expand Down
22 changes: 9 additions & 13 deletions tests/ContinuousOptionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function argumentProvider()
['program','-v', '-c', 'subcommand1', '--as', 99, 'arg1', 'arg2', 'arg3', '--','zz','xx','vv'],
[
'app' => ['verbose' => true],
'args' => ['arg1', 'arg2', 'arg3']
'args' => ['arg1', 'arg2', 'arg3', 'zz', 'xx', 'vv']
],
],
];
Expand Down Expand Up @@ -203,7 +203,7 @@ function testParser4()
$this->assertNotNull( $r );



$this->assertNotNull( $r->a , 'option a' );
$this->assertNotNull( $r->b , 'option b' );
$this->assertNotNull( $r->c , 'option c' );
Expand Down Expand Up @@ -250,7 +250,7 @@ function testParser5()
$arguments[] = $parser->advance();
}
}

$this->assertEquals( 'arg1', $arguments[0] );
$this->assertEquals( 'arg2', $arguments[1] );
$this->assertEquals( 'arg3', $arguments[2] );
Expand All @@ -261,11 +261,10 @@ function testParser5()
$this->assertNotNull( 3, $subcommand_options['subcommand3']->a );
}

/**
* @expectedException GetOptionKit\Exception\InvalidOptionException
*/
public function testParseInvalidOptionException()
{
$this->expectException(\GetOptionKit\Exception\InvalidOptionException::class);

$parser = new ContinuousOptionParser(new OptionCollection);
$parser->parse(array('app','--foo'));
$arguments = array();
Expand Down Expand Up @@ -307,23 +306,20 @@ public function testIncrementalValue()
$this->assertEquals(3, $result->keys["verbose"]->value);
}


/**
* @expectedException GetOptionKit\Exception\InvalidOptionException
*/
public function testUnknownOption()
{
$this->expectException(\GetOptionKit\Exception\InvalidOptionException::class);

$options = new OptionCollection;
$options->add("v|verbose");
$parser = new ContinuousOptionParser($options);
$result = $parser->parse(array('app', '-b'));
}

/**
* @expectedException LogicException
*/
public function testAdvancedOutOfBounds()
{
$this->expectException(\LogicException::class);

$options = new OptionCollection;
$options->add("v|verbose");
$parser = new ContinuousOptionParser($options);
Expand Down
16 changes: 6 additions & 10 deletions tests/OptionCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,28 @@ public function testAddOption()
$this->assertSame($o, $opts->getShortOption('v'));
}


/**
* @expectedException LogicException
*/
public function testAddInvalidOption()
{
$this->expectException(\LogicException::class);

$opts = new OptionCollection;
$opts->add(123);
}

/**
* @expectedException GetOptionKit\Exception\OptionConflictException
*/
public function testOptionConflictShort()
{
$this->expectException(\GetOptionKit\Exception\OptionConflictException::class);

$opts = new OptionCollection;
$opts->add('r|repeat');
$opts->add('t|time');
$opts->add('r|regex');
}

/**
* @expectedException GetOptionKit\Exception\OptionConflictException
*/
public function testOptionConflictLong()
{
$this->expectException(\GetOptionKit\Exception\OptionConflictException::class);

$opts = new OptionCollection;
$opts->add('r|repeat');
$opts->add('t|time');
Expand Down
38 changes: 16 additions & 22 deletions tests/OptionParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ protected function setUp(): void
$this->parser = new OptionParser($this->specs);
}

/**
* @expectedException Exception
*/
public function testInvalidOption()
{
$this->expectException(\Exception::class);

$options = new OptionCollection;
$options->addOption(new Option(0));
}
Expand Down Expand Up @@ -229,17 +228,16 @@ public function testParseIncrementalOption()

$parser = new OptionParser($opts);
$result = $parser->parse(explode(' ','app -vvv arg1 arg2'));
$this->assertInstanceOf('GetOptionKit\Option',$result['verbose']);
$this->assertInstanceOf('GetOptionKit\Option',$result['verbose']);
$this->assertNotNull($result['verbose']);
$this->assertEquals(3, $result['verbose']->value);
}


/**
* @expectedException Exception
*/
public function testIntegerTypeNonNumeric()
{
$this->expectException(\Exception::class);

$opt = new OptionCollection;
$opt->add( 'b|bar:=number' , 'option with integer type' );

Expand Down Expand Up @@ -350,13 +348,13 @@ public function optionTestProvider()
[['a','--foo','a', 'b', 'c']]
),
array( 'f|foo', 'simple boolean option', 'foo', true,
[['a','--foo'], ['a','-f']]
[['a','--foo'], ['a','-f']]
),
array( 'f|foo:=string', 'string option', 'foo', 'xxx',
[['a','--foo','xxx'], ['a','-f', 'xxx']]
[['a','--foo','xxx'], ['a','-f', 'xxx']]
),
array( 'f|foo:=string', 'string option', 'foo', 'xxx',
[['a','b', 'c', '--foo','xxx'], ['a', 'a', 'b', 'c', '-f', 'xxx']]
[['a','b', 'c', '--foo','xxx'], ['a', 'a', 'b', 'c', '-f', 'xxx']]
),
);
}
Expand All @@ -375,29 +373,26 @@ public function test($specString, $desc, $key, $expectedValue, array $argvList)
}
}

/**
* @expectedException Exception
*/
public function testParseWithoutProgramName()
{
$this->expectException(\Exception::class);

$parser = new OptionParser(new OptionCollection);
$parser->parse(array('--foo'));
}

/**
* @expectedException GetOptionKit\Exception\InvalidOptionException
*/
public function testParseInvalidOptionException()
{
$this->expectException(\GetOptionKit\Exception\InvalidOptionException::class);

$parser = new OptionParser(new OptionCollection);
$parser->parse(array('app','--foo'));
}

/**
* @expectedException GetOptionKit\Exception\RequireValueException
*/
public function testParseOptionRequireValueException()
{
$this->expectException(\GetOptionKit\Exception\RequireValueException::class);

$options = new OptionCollection;
$options->add('name:=string', 'name');

Expand Down Expand Up @@ -461,11 +456,10 @@ public function testParseAcceptsValidOption()
$this->assertArrayHasKey('f', $result);
}

/**
* @expectedException GetOptionKit\Exception\InvalidOptionValueException
*/
public function testParseThrowsExceptionOnInvalidOption()
{
$this->expectException(\GetOptionKit\Exception\InvalidOptionValueException::class);

$this->specs
->add('f:foo', 'test option')
->validator(function($value) {
Expand Down
17 changes: 7 additions & 10 deletions tests/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ public function testOptionSpec($spec)
$this->assertNotNull($opt);
}

/**
* @expectedException Exception
*/
public function testInvalidOptionSpec()
{
$this->expectException(\Exception::class);

new Option('....');
}

Expand Down Expand Up @@ -87,21 +86,19 @@ public function testValidator($cb)
$this->assertEquals('--scope', $opt->renderReadableSpec(true));
}

/**
* @expectedException Exception
*/
public function testInvalidTypeClass()
{
$this->expectException(\Exception::class);

$opt = new Option('scope');
$opt->isa('SomethingElse');
$class = $opt->getTypeClass();
}

/**
* @expectedException InvalidArgumentException
*/
public function testValidatorReturnValue()
{
$this->expectException(\InvalidArgumentException::class);

$opt = new Option('scope');
$opt->validator(function($val) {
return 123454;
Expand Down Expand Up @@ -216,7 +213,7 @@ public function testValidValues()

public function testFilter() {
$opt = new Option('scope');
$opt->filter(function($val) {
$opt->filter(function($val) {
return preg_replace('#a#', 'x', $val);
})
;
Expand Down

0 comments on commit 44c46ce

Please sign in to comment.