diff --git a/spec/CommanderSpec.php b/spec/CommanderSpec.php index 07b6c35..34da7ed 100644 --- a/spec/CommanderSpec.php +++ b/spec/CommanderSpec.php @@ -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 [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 [otherDirs...]', 'Remove the directory') diff --git a/src/Commander.php b/src/Commander.php index 2737bce..b43b7fe 100644 --- a/src/Commander.php +++ b/src/Commander.php @@ -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); + } } }