Skip to content

Commit 8616475

Browse files
committed
first move into 2.0.0
1 parent 5f180ff commit 8616475

6 files changed

Lines changed: 579 additions & 143 deletions

File tree

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ Take a look on [openhub.net](https://www.openhub.net/p/php_component_cli_argumen
3333
* lists (command --foobar=foo | command -f=foo)
3434
* values values (command <value>)
3535

36-
# Optimization Potential
37-
38-
* add api documentation
39-
4036
# Example
4137

4238
Simple call [run.php](https://github.com/bazzline/php_component_cli_arguments/blob/master/Example/run.php) with tons of arguments like illustrated below.
@@ -163,19 +159,23 @@ Your code has to take care if an argument is passed or not anyways. Using the av
163159

164160
# API
165161

166-
Thanks to [apigen](https://github.com/apigen/apigen), the api is available in the [document](https://github.com/bazzline/php_component_cli_arguments/blob/master/document/index.html) section or [online](http://code.bazzline.net/).
162+
[API](http://www.bazzline.net/988fc4501e48d8aed30583f58a97f0d4c14ab2d3/index.html) is available at [bazzline.net](http://www.bazzline.net).
167163

168164
# History
169165

170166
* upcomming
171167
* @todo
172-
* add *toString()'
173-
* add *countValues* etc.
174168
* add *hasFlags* to easy up validation if long or short flag is set (e.g. '-v|--verbose')
175-
* replace "setArguments" with "parse" like [nette](https://github.com/nette/command-line/blob/master/src/CommandLine/Parser.php) is doing it
176-
* remove *generate_api*
169+
* cover Collection and Parser with unit tests
177170
* updated dependency handling by beeing less restrictive
178-
* [1.1.2](https://github.com/bazzline/php_component_cli_argument/tree/1.1.1) - released at 07.11.2015
171+
* [2.0.0](https://github.com/bazzline/php_component_cli_argument/tree/2.0.0) - not yet released
172+
* add *convertToArray()'
173+
* add *convertToString()'
174+
* add *getNumberOfElements()*
175+
* create classes for flags, lists and values
176+
* remove *generate_api*
177+
* replace "setArguments" with "parseArguments" like [nette](https://github.com/nette/command-line/blob/master/src/CommandLine/Parser.php) is doing it
178+
* [1.1.2](https://github.com/bazzline/php_component_cli_argument/tree/1.1.2) - released at 07.11.2015
179179
* updated dependencies
180180
* [1.1.1](https://github.com/bazzline/php_component_cli_argument/tree/1.1.1) - released at 18.08.2015
181181
* updated dependencies

document/.todo

Lines changed: 0 additions & 1 deletion
This file was deleted.

generate_api

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 93 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ class Arguments
1111
/** @var array */
1212
private $arguments;
1313

14-
/** @var array */
14+
/** @var Collection */
1515
private $flags;
1616

17-
/** @var array */
17+
/** @var Collection */
1818
private $lists;
1919

20-
/** @var array */
20+
/** @var Parser */
21+
private $parser;
22+
23+
/** @var Collection */
2124
private $values;
2225

2326
/**
@@ -26,10 +29,12 @@ class Arguments
2629
*/
2730
public function __construct($argv = null, $removeFirstArgument = true)
2831
{
32+
$this->parser = new Parser();
33+
2934
if (is_array($argv)) {
30-
$this->setArguments($argv, $removeFirstArgument);
35+
$this->parseArguments($argv, $removeFirstArgument);
3136
} else {
32-
$this->initiate();
37+
$this->bindValuesFromGenerator();
3338
}
3439
}
3540

@@ -42,36 +47,68 @@ public function getArguments()
4247
}
4348

4449
/**
45-
* @return array
50+
* @param bool $convertCollectionToArray
51+
* @return array|Collection
4652
*/
47-
public function getFlags()
53+
public function getFlags($convertCollectionToArray = true)
4854
{
49-
return ($this->hasFlags()) ? $this->flags : array();
55+
return (
56+
$this->convertCollectionToArrayIfNeeded(
57+
$this->flags,
58+
$convertCollectionToArray
59+
)
60+
);
5061
}
5162

5263
/**
5364
* @param string $name
54-
* @return null|mixed
65+
* @param bool $convertCollectionToArray
66+
* @return null|Collection
5567
*/
56-
public function getList($name)
68+
public function getList($name, $convertCollectionToArray = true)
5769
{
58-
return ($this->hasList($name)) ? $this->lists[$name] : null;
70+
$list = $this->lists->offsetGet($name);
71+
72+
if ($list instanceof Collection) {
73+
$return = (
74+
$this->convertCollectionToArrayIfNeeded(
75+
$list,
76+
$convertCollectionToArray
77+
)
78+
);
79+
} else {
80+
$return = null;
81+
}
82+
83+
return $return;
5984
}
6085

6186
/**
62-
* @return array
87+
* @param bool $convertCollectionToArray
88+
* @return array|Collection
6389
*/
64-
public function getLists()
90+
public function getLists($convertCollectionToArray = true)
6591
{
66-
return ($this->hasLists()) ? $this->lists : array();
92+
return (
93+
$this->convertCollectionToArrayIfNeeded(
94+
$this->lists,
95+
$convertCollectionToArray
96+
)
97+
);
6798
}
6899

69100
/**
70-
* @return array
101+
* @param bool $convertCollectionToArray
102+
* @return array|Collection
71103
*/
72-
public function getValues()
104+
public function getValues($convertCollectionToArray = true)
73105
{
74-
return ($this->hasValues()) ? $this->values : array();
106+
return (
107+
$this->convertCollectionToArrayIfNeeded(
108+
$this->values,
109+
$convertCollectionToArray
110+
)
111+
);
75112
}
76113

77114
/**
@@ -88,15 +125,15 @@ public function hasArguments()
88125
*/
89126
public function hasFlag($name)
90127
{
91-
return in_array($name, $this->flags);
128+
return $this->flags->containsValue($name);
92129
}
93130

94131
/**
95132
* @return bool
96133
*/
97134
public function hasFlags()
98135
{
99-
return (!empty($this->flags));
136+
return (!$this->flags->isEmpty());
100137
}
101138

102139
/**
@@ -105,138 +142,94 @@ public function hasFlags()
105142
*/
106143
public function hasList($name)
107144
{
108-
return (isset($this->lists[$name]));
145+
return $this->lists->containsKey($name);
109146
}
110147

111148
/**
112149
* @return bool
113150
*/
114151
public function hasLists()
115152
{
116-
return (!empty($this->lists));
153+
return (!$this->lists->isEmpty());
117154
}
118155

119156
/**
120157
* @return bool
121158
*/
122159
public function hasValues()
123160
{
124-
return (!empty($this->values));
161+
return (!$this->values->isEmpty());
125162
}
126163

127164
/**
128165
* @param array $argv
129166
* @param boolean $removeFirstArgument
130167
* @return $this
131168
*/
132-
public function setArguments(array $argv, $removeFirstArgument = true)
169+
public function parseArguments(array $argv, $removeFirstArgument = true)
133170
{
134171
if ($removeFirstArgument) {
135172
array_shift($argv);
136173
}
137174

138-
$this->initiate();
139175
$this->arguments = $argv;
140-
$this->generate($this->arguments);
176+
$this->parse($this->arguments);
177+
$this->bindValuesFromGenerator();
141178

142179
return $this;
143180
}
144181

145182
/**
146-
* @param string $name
147-
* @param mixed $value
183+
* @return array
148184
*/
149-
private function addToList($name, $value)
185+
public function convertToArray()
150186
{
151-
$value = trim($value, '"'); //remove >"< if exists
152-
153-
if (isset($this->lists[$name])) {
154-
$this->lists[$name][] = $value;
155-
} else {
156-
$this->lists[$name] = array($value);
157-
}
187+
return $this->getArguments();
158188
}
159189

160-
private function generate(array $arguments)
190+
/**
191+
* @return string
192+
*/
193+
public function convertToString()
161194
{
162-
foreach ($arguments as $argument) {
163-
if ($this->startsWith($argument, '--')) {
164-
$argument = substr($argument, 2);
165-
$this->handleLongNameListOrFlag($argument);
166-
} else if ($this->startsWith($argument, '-')) {
167-
$argument = substr($argument, 1);
168-
$this->handleShortNameListOrFlag($argument);
169-
} else {
170-
$this->values[] = $argument;
171-
}
172-
173-
}
195+
return implode(' ', $this->convertToArray());
174196
}
175197

176-
private function handleLongNameListOrFlag($argument)
198+
/**
199+
* @return string
200+
*/
201+
public function __toString()
177202
{
178-
if ($this->contains($argument, '=')) {
179-
$position = strpos($argument, '=');
180-
$name = substr($argument, 0, $position);
181-
$value = substr($argument, ($position + 1));
182-
$this->addToList($name, $value);
183-
} else {
184-
$this->flags[] = $argument;
185-
}
203+
return $this->convertToString();
186204
}
187205

188-
private function handleShortNameListOrFlag($argument)
189-
{
190-
$containsEqualCharacter = ($this->contains($argument, '='));
191-
$equalCharacterIsOnSecondPosition = (strpos($argument, '=') === 1);
192-
$isShortNameList = ($containsEqualCharacter
193-
&& $equalCharacterIsOnSecondPosition);
194-
195-
if ($isShortNameList) {
196-
$name = substr($argument, 0, 1);
197-
$value = substr($argument, 2);
198-
$this->addToList($name, $value);
199-
} else if (!$containsEqualCharacter) {
200-
$length = strlen($argument);
201-
$iterator = 0;
202-
while ($iterator < $length) {
203-
$this->flags[] = $argument{$iterator};
204-
++$iterator;
205-
}
206-
}
207-
}
208-
209-
/**
210-
* @param string $string
211-
* @param string $search
212-
* @return bool
213-
*/
214-
private function contains($string, $search)
206+
private function parse(array $arguments)
215207
{
216-
if (strlen($search) === 0) {
217-
$contains = false;
218-
} else {
219-
$contains = !(strpos($string, $search) === false);
220-
}
208+
$parser = $this->parser;
221209

222-
return $contains;
210+
$parser->parse($arguments);
223211
}
224212

225-
private function initiate()
213+
private function bindValuesFromGenerator()
226214
{
227-
$this->arguments = array();
228-
$this->flags = array();
229-
$this->lists = array();
230-
$this->values = array();
215+
$parser = $this->parser;
216+
217+
$this->flags = $parser->getFlags();
218+
$this->lists = $parser->getLists();
219+
$this->values = $parser->getValues();
231220
}
232221

233222
/**
234-
* @param string $string
235-
* @param string $start
236-
* @return bool
223+
* @param Collection $collection
224+
* @param bool $isNeeded
225+
* @return array|Collection
237226
*/
238-
private function startsWith($string, $start)
227+
private function convertCollectionToArrayIfNeeded(Collection $collection, $isNeeded)
239228
{
240-
return (strncmp($string, $start, strlen($start)) === 0);
229+
return (
230+
$isNeeded
231+
? $collection->convertToArray()
232+
: $collection
233+
);
241234
}
242235
}

0 commit comments

Comments
 (0)