From e13d28cf262415230b980c7c7f59e603cd3426da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20D=C3=A9cima?= Date: Mon, 30 Nov 2015 16:43:39 -0300 Subject: [PATCH] Add empty values tests and fix --- CommandLine.php | 8 ++++---- tests/CommandLineTest.php | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CommandLine.php b/CommandLine.php index 20c88a8..d9bca8b 100644 --- a/CommandLine.php +++ b/CommandLine.php @@ -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++; @@ -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; } } @@ -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 @@ -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++; diff --git a/tests/CommandLineTest.php b/tests/CommandLineTest.php index 8f79473..1978601 100644 --- a/tests/CommandLineTest.php +++ b/tests/CommandLineTest.php @@ -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() { @@ -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() { @@ -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() { @@ -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() {