Skip to content

Commit debf451

Browse files
committed
added support for single character
1 parent 12535eb commit debf451

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ Your code has to take care if an argument is passed or not anyways. Using the av
167167
* @todo
168168
* add *hasFlags* to easy up validation if long or short flag is set (e.g. '-v|--verbose')
169169
* cover Parser with unit tests
170+
* [1.3.0](https://github.com/bazzline/php_component_cli_argument/tree/1.3.0) - released at 01.12.2015
171+
* added support for single argument like "a" or "-"
170172
* [1.2.0](https://github.com/bazzline/php_component_cli_argument/tree/1.2.0) - released at 28.11.2015
171173
* added *convertToArray()'
172174
* added *convertToString()'

source/Net/Bazzline/Component/Cli/Arguments/Parser.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ private function initiate()
136136
*/
137137
private function addToFittingCollection($argument)
138138
{
139-
if ($this->startsWith($argument, '--')) {
139+
if ($this->hasLengthOf($argument, 1)) {
140+
$this->values[] = $argument;
141+
} else if ($this->startsWith($argument, '--')) {
140142
$argument = substr($argument, 2);
141143
$this->handleLongNameListOrFlag($argument);
142144
} else if ($this->startsWith($argument, '-')) {
@@ -156,4 +158,17 @@ private function startsWith($string, $start)
156158
{
157159
return (strncmp($string, $start, strlen($start)) === 0);
158160
}
159-
}
161+
162+
/**
163+
* @param string $string
164+
* @param int $expectedLength
165+
* @return bool
166+
*/
167+
private function hasLengthOf($string, $expectedLength)
168+
{
169+
$length = strlen($string);
170+
$hasLengthOf = ($length == $expectedLength);
171+
172+
return $hasLengthOf;
173+
}
174+
}

test/Test/Net/Bazzline/Component/Cli/Arguments/ArgumentsTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ public function testWithArgumentsProvider()
4343
'lists' => array(),
4444
'values' => array()
4545
),
46+
'one value with one character' => array(
47+
'argv' => array(
48+
__FILE__,
49+
'-'
50+
),
51+
'arguments' => array(
52+
'-'
53+
),
54+
'flags' => array(),
55+
'lists' => array(),
56+
'values' => array(
57+
'-'
58+
)
59+
),
4660
'one value' => array(
4761
'argv' => array(
4862
__FILE__,
@@ -262,4 +276,4 @@ private function createArguments($argv = null, $removeFirstArgument = true)
262276
{
263277
return new Arguments($argv, $removeFirstArgument);
264278
}
265-
}
279+
}

0 commit comments

Comments
 (0)