diff --git a/examples/multiple.php b/examples/multiple.php new file mode 100755 index 0000000..00351af --- /dev/null +++ b/examples/multiple.php @@ -0,0 +1,48 @@ +#!/usr/bin/php +setHelp('This is a simple example, not using any subcommands'); + $options->registerOption('multiple', 'This option can be specified multiple times.', 'm', true, '', true); + $options->registerArgument('argument', 'Arguments can be required or optional. This one is optional', false); + } + + /** + * Your main program + * + * Arguments and options have been parsed when this is run + * + * @param Options $options + * @return void + */ + protected function main(Options $options) + { + if ($options->getOpt('multiple')) { + $this->info("multiple was given as " . implode(", ", (array) $options->getOpt('multiple'))); + } + + $this->info("Number of arguments: " . count($options->getArgs())); + + $this->success("main finished"); + } +} + +$cli = new Multiple(); +$cli->run(); diff --git a/src/Options.php b/src/Options.php index 5ee6b69..e1cf963 100644 --- a/src/Options.php +++ b/src/Options.php @@ -121,7 +121,7 @@ public function registerArgument($arg, $help, $required = true, $command = '') $this->setup[$command]['args'][] = array( 'name' => $arg, 'help' => $help, - 'required' => $required + 'required' => $required, ); } @@ -156,9 +156,10 @@ public function registerCommand($command, $help) * @param string|null $short one character option (specified with -) * @param bool|string $needsarg does this option require an argument? give it a name here * @param string $command what command does this option apply to + * @param bool $multiple this option can be specified multiple times * @throws Exception */ - public function registerOption($long, $help, $short = null, $needsarg = false, $command = '') + public function registerOption($long, $help, $short = null, $needsarg = false, $command = '', bool $multiple = false) { if (!isset($this->setup[$command])) { throw new Exception("Command $command not registered"); @@ -167,7 +168,8 @@ public function registerOption($long, $help, $short = null, $needsarg = false, $ $this->setup[$command]['opts'][$long] = array( 'needsarg' => $needsarg, 'help' => $help, - 'short' => $short + 'short' => $short, + 'multiple' => $multiple ); if ($short) { @@ -264,7 +266,14 @@ public function parseOptions() throw new Exception("Option $opt requires an argument", Exception::E_OPT_ARG_REQUIRED); } - $this->options[$opt] = $val; + if ($this->setup[$this->command]['opts'][$opt]['multiple']) { + if(!isset($this->options[$opt])) { + $this->options[$opt] = array(); + } + $this->options[$opt][] = $val; + } else { + $this->options[$opt] = $val; + } } else { $this->options[$opt] = true; } @@ -290,7 +299,14 @@ public function parseOptions() throw new Exception("Option $arg requires an argument", Exception::E_OPT_ARG_REQUIRED); } - $this->options[$opt] = $val; + if ($this->setup[$this->command]['opts'][$opt]['multiple']) { + if(!isset($this->options[$opt])) { + $this->options[$opt] = array(); + } + $this->options[$opt][] = $val; + } else { + $this->options[$opt] = $val; + } } else { $this->options[$opt] = true; } diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php index b8f2782..658fce3 100644 --- a/tests/OptionsTest.php +++ b/tests/OptionsTest.php @@ -77,6 +77,16 @@ function test_complex() $this->assertEquals(array('foo'), $options->args); } + function test_multiple() + { + $options = new Options(); + $options->registerOption('multiple', 'this option can be specified multiple times', 'm', true, '', true); + + $options->args = array('-m', 'first', '--multiple', 'second', '-m', 'third', '--multiple', 'fourth'); + $options->parseOptions(); + $this->assertEquals(array("first", "second", "third", "fourth"), $options->getOpt('multiple')); + } + function test_commandhelp() { $options = new Options();