@@ -64,6 +64,10 @@ public function __construct(array $arguments = [], array $options = [])
6464 $ this ->setOptions ($ options );
6565 }
6666
67+ /***************************************************************************
68+ * some methods for the arguments
69+ ***************************************************************************/
70+
6771 /**
6872 * @param array $arguments
6973 * @return InputDefinition
@@ -91,6 +95,19 @@ public function addArguments(array $arguments): self
9195 return $ this ;
9296 }
9397
98+ /**
99+ * alias of the addArgument
100+ * @param string $name
101+ * @param int|null $mode
102+ * @param string $description
103+ * @param null $default
104+ * @return InputDefinition
105+ */
106+ public function addArg (string $ name , int $ mode = null , string $ description = '' , $ default = null ): self
107+ {
108+ return $ this ->addArgument ($ name , $ mode , $ description , $ default );
109+ }
110+
94111 /**
95112 * Adds an argument.
96113 *
@@ -101,7 +118,7 @@ public function addArguments(array $arguments): self
101118 * @return $this
102119 * @throws \LogicException
103120 */
104- public function addArgument ($ name , $ mode = null , $ description = '' , $ default = null ): self
121+ public function addArgument (string $ name , int $ mode = null , string $ description = '' , $ default = null ): self
105122 {
106123 if (null === $ mode ) {
107124 $ mode = Input::ARG_OPTIONAL ;
@@ -158,16 +175,17 @@ public function addArgument($name, $mode = null, $description = '', $default = n
158175
159176 /**
160177 * @param int|string $name
161- * @return array
162- * @throws \InvalidArgumentException
178+ * @param null $default
179+ * @return string|int|null
163180 */
164- public function getArgument ($ name): array
181+ public function getArgument ($ name, $ default = null )
165182 {
166- if (!$ this ->hasArgument ($ name )) {
167- throw new \InvalidArgumentException (sprintf ('The "%s" argument does not exist. ' , $ name ));
168- }
183+ $ arguments = \is_int ($ name ) ? \array_values ($ this ->arguments ) : $ this ->arguments ;
169184
170- $ arguments = \is_int ($ name ) ? array_values ($ this ->arguments ) : $ this ->arguments ;
185+ if (!isset ($ arguments [$ name ])) {
186+ return $ default ;
187+ // throw new \InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));
188+ }
171189
172190 return $ arguments [$ name ];
173191 }
@@ -178,7 +196,7 @@ public function getArgument($name): array
178196 */
179197 public function hasArgument ($ name ): bool
180198 {
181- $ arguments = \is_int ($ name ) ? array_values ($ this ->arguments ) : $ this ->arguments ;
199+ $ arguments = \is_int ($ name ) ? \ array_values ($ this ->arguments ) : $ this ->arguments ;
182200
183201 return isset ($ arguments [$ name ]);
184202 }
@@ -208,6 +226,10 @@ public function getArgumentRequiredCount(): int
208226 return $ this ->requiredCount ;
209227 }
210228
229+ /***************************************************************************
230+ * some methods for the options
231+ ***************************************************************************/
232+
211233 /**
212234 * Sets the options
213235 *
@@ -236,6 +258,16 @@ public function addOptions(array $options = [])
236258 }
237259 }
238260
261+ /**
262+ * alias of the addOption
263+ * {@inheritdoc}
264+ * @return InputDefinition
265+ */
266+ public function addOpt (string $ name , string $ shortcut = null , int $ mode = null , string $ description = '' , $ default = null ): self
267+ {
268+ return $ this ->addOption ($ name , $ shortcut , $ mode , $ description , $ default );
269+ }
270+
239271 /**
240272 * Adds an option.
241273 *
@@ -249,10 +281,10 @@ public function addOptions(array $options = [])
249281 * @throws \InvalidArgumentException
250282 * @throws \LogicException
251283 */
252- public function addOption (string $ name , string $ shortcut = null , $ mode = null , $ description = '' , $ default = null ): self
284+ public function addOption (string $ name , string $ shortcut = null , int $ mode = null , string $ description = '' , $ default = null ): self
253285 {
254- if (0 === strpos ($ name , '- ' )) {
255- $ name = trim ($ name , '- ' );
286+ if (0 === \ strpos ($ name , '- ' )) {
287+ $ name = \ trim ($ name , '- ' );
256288 }
257289
258290 if (empty ($ name )) {
@@ -329,7 +361,7 @@ public function addOption(string $name, string $shortcut = null, $mode = null, $
329361 * @return array
330362 * @throws \InvalidArgumentException
331363 */
332- public function getOption ($ name ): array
364+ public function getOption (string $ name ): array
333365 {
334366 if (!$ this ->hasOption ($ name )) {
335367 throw new \InvalidArgumentException (sprintf ('The "--%s" option does not exist. ' , $ name ));
@@ -342,7 +374,7 @@ public function getOption($name): array
342374 * @param string $name
343375 * @return bool
344376 */
345- public function hasOption ($ name ): bool
377+ public function hasOption (string $ name ): bool
346378 {
347379 return isset ($ this ->options [$ name ]);
348380 }
@@ -397,7 +429,7 @@ private function shortcutToName(string $shortcut)
397429 */
398430 private function mergeArgOptConfig (array $ map ): array
399431 {
400- return $ map ? array_merge (self ::$ defaultArgOptConfig , $ map ) : self ::$ defaultArgOptConfig ;
432+ return $ map ? \ array_merge (self ::$ defaultArgOptConfig , $ map ) : self ::$ defaultArgOptConfig ;
401433 }
402434
403435 /**
@@ -416,16 +448,16 @@ public function getSynopsis(bool $short = false): array
416448 $ value = '' ;
417449
418450 if ($ this ->optionIsAcceptValue ($ option ['mode ' ])) {
419- $ value = sprintf (
451+ $ value = \ sprintf (
420452 ' %s%s%s ' ,
421453 $ option ['optional ' ] ? '[ ' : '' ,
422- strtoupper ($ name ),
454+ \ strtoupper ($ name ),
423455 $ option ['optional ' ] ? '] ' : ''
424456 );
425457 }
426458
427- $ shortcut = $ option ['shortcut ' ] ? sprintf ('-%s, ' , $ option ['shortcut ' ]) : '' ;
428- $ elements [] = sprintf ('[%s--%s%s] ' , $ shortcut , $ name , $ value );
459+ $ shortcut = $ option ['shortcut ' ] ? \ sprintf ('-%s, ' , $ option ['shortcut ' ]) : ' ' ;
460+ $ elements [] = \ sprintf ('[%s--%s%s] ' , $ shortcut , $ name , $ value );
429461
430462 $ key = "{$ shortcut }-- {$ name }" ;
431463 $ opts [$ key ] = ($ option ['required ' ] ? '<red>*</red> ' : '' ) . $ option ['description ' ];
@@ -455,11 +487,12 @@ public function getSynopsis(bool $short = false): array
455487 }
456488
457489 return [
458- 'description ' => $ this ->description ,
459- 'usage ' => implode (' ' , $ elements ),
460- 'arguments ' => $ args ,
461- 'options ' => $ opts ,
462- 'example ' => $ this ->example ,
490+ $ this ->description ,
491+ 'usage: ' => \implode (' ' , $ elements ),
492+ 'options: ' => $ opts ,
493+ 'arguments: ' => $ args ,
494+ 'example: ' => $ this ->example ,
495+ 'global options: ' => '' ,
463496 ];
464497 }
465498
@@ -480,7 +513,7 @@ public function argumentIsRequired($name): bool
480513 * @param int $mode
481514 * @return bool
482515 */
483- protected function argumentIsAcceptValue ($ mode ): bool
516+ protected function argumentIsAcceptValue (int $ mode ): bool
484517 {
485518 return $ mode === Input::ARG_REQUIRED || $ mode === Input::ARG_OPTIONAL ;
486519 }
@@ -489,7 +522,7 @@ protected function argumentIsAcceptValue($mode): bool
489522 * @param int $mode
490523 * @return bool
491524 */
492- protected function optionIsAcceptValue ($ mode ): bool
525+ protected function optionIsAcceptValue (int $ mode ): bool
493526 {
494527 return $ mode === Input::OPT_REQUIRED || $ mode === Input::OPT_OPTIONAL ;
495528 }
0 commit comments