Skip to content

Commit

Permalink
Add ability to set values to options
Browse files Browse the repository at this point in the history
Also make options be case sensitive
  • Loading branch information
knivey committed Jun 11, 2021
1 parent 12ba27b commit 8e5a1c1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
19 changes: 15 additions & 4 deletions src/Args.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Args implements \ArrayAccess, \Countable
*/
function __construct(public string $syntax, protected array $opts = [])
{
$this->opts = array_map('\strtolower', $this->opts);
$argv = array_filter(explode(' ', $syntax));
if (count($argv) == 0) {
return;
Expand Down Expand Up @@ -91,8 +90,14 @@ public function parse(string $msg) : Args {
$msg = explode(' ', $msg);
$msgb = [];
foreach ($msg as $w) {
if(in_array($w, $this->opts))
$this->parsedOpts[$w] = $w;
if(str_contains($w, "=")) {
list($lhs, $rhs) = explode("=", $w, 2);
} else {
$lhs = $w;
$rhs = null;
}
if(in_array($lhs, $this->opts))
$this->parsedOpts[$lhs] = $rhs;
else
$msgb[] = $w;
}
Expand Down Expand Up @@ -123,7 +128,13 @@ public function getOpts() {
}

public function getOpt($name) : bool {
return isset($this->parsedOpts[strtolower($name)]);
return array_key_exists($name, $this->parsedOpts);
}

public function getOptVal($name) {
if(!$this->getOpt($name))
return false;
return $this->parsedOpts[$name];
}

public function getArg(string $name): ?Arg {
Expand Down
48 changes: 46 additions & 2 deletions tests/ArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,15 @@ function testOptions()
$this->assertEmpty($args->getOpts());
$args->parse('moo --nes poo');
$this->assertEquals('moo poo', $args[0]);
$this->assertEquals(['--nes'=>'--nes'], $args->getOpts());
$this->assertEquals(['--nes'=>null], $args->getOpts());

$args = new Args('<foo>', ['--nes']);
$args->parse('moo boo poo');
$this->assertEquals('moo', $args[0]);
$this->assertEmpty($args->getOpts());
$args->parse('moo --nes poo');
$this->assertEquals('moo', $args[0]);
$this->assertEquals(['--nes'=>'--nes'], $args->getOpts());
$this->assertEquals(['--nes'=>null], $args->getOpts());

$args = new Args('[foo]', ['--nes']);
$args->parse('moo boo poo');
Expand All @@ -293,4 +293,48 @@ function testOptions()
$this->assertTrue($args->getOpt('--nes'));
$this->assertTrue($args->getOpt('--bar'));
}

function testOptionsCase()
{
$args = new Args('', ['--nes', '--NES']);
$args->parse('--nes');
$this->assertTrue($args->getOpt("--nes"));
$this->assertFalse($args->getOpt("--NES"));

$args = new Args('', ['--nes', '--NES']);
$args->parse('--NES');
$this->assertFalse($args->getOpt("--nes"));
$this->assertTrue($args->getOpt("--NES"));
}

function testOptionsValue()
{
$args = new Args('', ['--nes', '--jam']);
$args = $args->parse('--nes');
$this->assertTrue($args->getOpt("--nes"));
$this->assertEquals('', $args->getOptVal("--nes"));
$this->assertFalse($args->getOptVal("--jam"));

$args = $args->parse('--nes=lol');
$this->assertTrue($args->getOpt("--nes"));
$this->assertEquals("lol", $args->getOptVal("--nes"));
$this->assertFalse($args->getOpt("--jam"));
$this->assertFalse($args->getOptVal("--jam"));

$args = $args->parse('--nes=LOL');
$this->assertTrue($args->getOpt("--nes"));
$this->assertEquals("LOL", $args->getOptVal("--nes"));
$this->assertFalse($args->getOpt("--jam"));
$this->assertFalse($args->getOptVal("--jam"));

$args = $args->parse('--nes=LOL --jam=yeah');
$this->assertTrue($args->getOpt("--nes"));
$this->assertEquals("LOL", $args->getOptVal("--nes"));
$this->assertTrue($args->getOpt("--jam"));
$this->assertEquals("yeah", $args->getOptVal("--jam"));

$args = $args->parse('--nes=0');
$this->assertTrue($args->getOpt("--nes"));
$this->assertEquals("0", $args->getOptVal("--nes"));
}
}

0 comments on commit 8e5a1c1

Please sign in to comment.