Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions CommandLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static function parseArgs($argv = null)
$key = substr($arg, 2);

// --foo value
if ($i + 1 < $j && $argv[$i + 1][0] !== '-')
if ($i + 1 < $j && (strlen($argv[$i + 1]) == 0 || $argv[$i + 1][0] !== '-'))
{
$value = $argv[$i + 1];
$i++;
Expand All @@ -98,7 +98,7 @@ public static function parseArgs($argv = null)
else
{
$key = substr($arg, 2, $eqPos - 2);
$value = substr($arg, $eqPos + 1);
$value = (strlen($arg) == $eqPos + 1) ? '' : substr($arg, $eqPos + 1);
$out[$key] = $value;
}
}
Expand All @@ -110,7 +110,7 @@ public static function parseArgs($argv = null)
if (substr($arg, 2, 1) === '=')
{
$key = substr($arg, 1, 1);
$value = substr($arg, 3);
$value = (strlen($arg) == 3) ? '' : substr($arg, 3);
$out[$key] = $value;
}
// -abc
Expand All @@ -124,7 +124,7 @@ public static function parseArgs($argv = null)
$out[$key] = $value;
}
// -a value1 -abc value2
if ($i + 1 < $j && $argv[$i + 1][0] !== '-')
if ($i + 1 < $j && (strlen($argv[$i + 1]) == 0 || $argv[$i + 1][0] !== '-'))
{
$out[$key] = $argv[$i + 1];
$i++;
Expand Down
33 changes: 33 additions & 0 deletions tests/CommandLineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public function singleSwitchWithValue()
$this->assertEquals('b', $result['a']);
}

/** @test */
public function singleSwitchWithEmptyValue()
{
$result = CommandLine::parseArgs(array(self::FILE, '-a='));
$this->assertEquals(1, count($result));
$this->assertSame('', $result['a']);
}

/** @test */
public function multiSwitch()
{
Expand Down Expand Up @@ -89,6 +97,14 @@ public function singleFlagWithValue()
$this->assertEquals('b', $result['a']);
}

/** @test */
public function singleFlagWithEmptyValue()
{
$result = CommandLine::parseArgs(array(self::FILE, '--a='));
$this->assertEquals(1, count($result));
$this->assertSame('', $result['a']);
}

/** @test */
public function singleFlagOverwriteValue()
{
Expand Down Expand Up @@ -145,6 +161,14 @@ public function singleFlagWithValueWithoutEquation ()
$this->assertEquals('b', $result['a']);
}

/** @test */
public function singleFlagWithEmptyValueWithoutEquation ()
{
$result = CommandLine::parseArgs(array(self::FILE, '--a', ''));
$this->assertEquals(1, count($result));
$this->assertSame('', $result['a']);
}

/** @test */
public function multiSwitchAsOneWithValue()
{
Expand All @@ -154,6 +178,15 @@ public function multiSwitchAsOneWithValue()
$this->assertEquals('value', $result['b']);
}

/** @test */
public function multiSwitchAsOneWithEmptyValue()
{
$result = CommandLine::parseArgs(array(self::FILE, '-ab', ''));
$this->assertEquals(2, count($result));
$this->assertTrue($result['a']);
$this->assertSame('', $result['b']);
}

/** @test */
public function combination()
{
Expand Down