From 4d4cc7cb5a34730d736b6d83d04a3b1e82a30632 Mon Sep 17 00:00:00 2001 From: Jeroen Roos Date: Sun, 2 Jun 2024 11:01:58 +0200 Subject: [PATCH 1/2] Add options that can be specified multiple times Issue #31 --- examples/multiple.php | 48 +++++++++++++++++++++++++++++++++++++++++++ src/Options.php | 25 +++++++++++++++++----- tests/OptionsTest.php | 10 +++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) create mode 100755 examples/multiple.php diff --git a/examples/multiple.php b/examples/multiple.php new file mode 100755 index 0000000..a68e12e --- /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, multiple: 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 Simple(); +$cli->run(); diff --git a/src/Options.php b/src/Options.php index 5ee6b69..541f9e9 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, ); } @@ -158,7 +158,7 @@ public function registerCommand($command, $help) * @param string $command what command does this option apply to * @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 +167,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 +265,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 +298,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..285d117 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, multiple: 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(); From cf1a4fe7f0da7d512f7f74a42cefab155d55f4c4 Mon Sep 17 00:00:00 2001 From: Jeroen Roos Date: Sun, 2 Jun 2024 19:10:58 +0200 Subject: [PATCH 2/2] fixup! Add options that can be specified multiple times --- examples/multiple.php | 6 +++--- src/Options.php | 1 + tests/OptionsTest.php | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/multiple.php b/examples/multiple.php index a68e12e..00351af 100755 --- a/examples/multiple.php +++ b/examples/multiple.php @@ -8,7 +8,7 @@ use splitbrain\phpcli\CLI; use splitbrain\phpcli\Options; -class Simple extends CLI +class Multiple extends CLI { /** @@ -20,7 +20,7 @@ class Simple extends CLI protected function setup(Options $options) { $options->setHelp('This is a simple example, not using any subcommands'); - $options->registerOption('multiple', 'This option can be specified multiple times.', 'm', true, multiple: true); + $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); } @@ -44,5 +44,5 @@ protected function main(Options $options) } } -$cli = new Simple(); +$cli = new Multiple(); $cli->run(); diff --git a/src/Options.php b/src/Options.php index 541f9e9..e1cf963 100644 --- a/src/Options.php +++ b/src/Options.php @@ -156,6 +156,7 @@ 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 = '', bool $multiple = false) diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php index 285d117..658fce3 100644 --- a/tests/OptionsTest.php +++ b/tests/OptionsTest.php @@ -80,7 +80,7 @@ function test_complex() function test_multiple() { $options = new Options(); - $options->registerOption('multiple', 'this option can be specified multiple times', 'm', true, multiple: true); + $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();