Skip to content

Add options that can be specified multiple times #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions examples/multiple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/php
<?php

namespace splitbrain\phpcli\examples;

require __DIR__ . '/../vendor/autoload.php';

use splitbrain\phpcli\CLI;
use splitbrain\phpcli\Options;

class Multiple extends CLI
{

/**
* Register options and arguments on the given $options object
*
* @param Options $options
* @return void
*/
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, '', 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();
26 changes: 21 additions & 5 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}

Expand Down Expand Up @@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/OptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down