Skip to content
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

Add support for adding option to subcommand #1

Open
wants to merge 1 commit 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
18 changes: 18 additions & 0 deletions spec/CommanderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ function it_can_add_action_to_command()
->action(function(){});
}

function it_can_add_option_to_command()
{
$program = $this->getWrappedObject();
$this->command('rmdir <dir> [otherDirs...]', 'Remove the directory')
->option('-r, --recursive', 'Remove recursively')
->action(function() use ($program) {
foreach ($program->_subCmds as $cmd) {
if ($cmd->_name == 'rmdir') {
return $cmd->recursive ? 'recursive' : 'flat';
}
}
});

$argv = ['test.php', 'rmdir', 'testDir', '-r'];

$this->parse($argv)->shouldReturn('recursive');
}

function it_will_throw_exception_if_missing_a_required_arg_for_a_command()
{
$this->command('rmdir <dir> [otherDirs...]', 'Remove the directory')
Expand Down
10 changes: 6 additions & 4 deletions src/Commander.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,21 @@ public function parse($argv)

$this->_args = $this->normalize(array_slice($argv, 1));

$this->parseOptions($this->_args);

if (count($this->_args) > 0) {
$name = $this->_args[0];

if ($name[0] !== '-') {
foreach ($this->_subCmds as $cmd) {
if ($cmd->_name == $name) {
array_shift($this->_unknownArgs);
return $this->triggerCmd($cmd);
$cmd->parseOptions($this->_args);
array_shift($cmd->_unknownArgs);
return $cmd->triggerCmd($cmd);
}
}
}
else {
$this->parseOptions($this->_args);
}
}
}

Expand Down