Fixed issue with global commands in parent contexts

This commit is contained in:
2016-11-02 22:49:19 +01:00
parent 46806e3d62
commit 455574b6a5
3 changed files with 67 additions and 11 deletions

View File

@ -176,12 +176,18 @@ class Shell
private function findCommand($command)
{
// Go over current context and walk through stack until finding command
foreach(array_merge([ $this->context ] , $this->contextStack) as $context) {
if ($context->hasCommand($command)) {
$handler = $context->getCommand($command);
break;
if ($this->context->hasCommand($command)) {
$handler = $this->context->getCommand($command);
} else {
foreach($this->contextStack as $context) {
if ($context->hasCommand($command) && $context->isCommandGlobal($command)) {
$handler = $context->getCommand($command);
break;
}
}
}
// No handler...
if (empty($handler)) {
return false;
}
@ -236,13 +242,34 @@ class Shell
break;
case 'help':
$help = $this->context->getCommandHelp();
printf("Commands in current context:\n\n");
foreach ($help as $command=>$info) {
printf(" %-20s %s\n", $command, $info);
$ghelp = [];
foreach ($this->contextStack as $context) {
$commands = $context->getCommandHelp();
foreach ($commands as $command=>$info) {
if (strpos(" ",$command)!==false) {
list ($cmd,$arg)=explode(" ",$command,2);
} else {
$cmd = $command;
}
if ($context->isCommandGlobal($cmd)) {
$ghelp[$command] = $info;
}
}
}
printf("\nGlobal commands:\n\n");
printf(" %-20s %s\n", "exit", "Leave the shell");
printf(" %-20s %s\n", "..", "Discard the current context and go to parent");
ksort($ghelp);
printf("Commands in current context:\n");
foreach ($help as $command=>$info) {
printf(" %-20s %s\n", $command, $info);
}
if (count($ghelp)) {
printf("\nImported from parent contexts:\n");
foreach ($ghelp as $command=>$info) {
printf(" %-20s %s\n", $command, $info);
}
}
printf("\nGlobal commands:\n");
printf(" %-20s %s\n", "exit", "Leave the shell");
printf(" %-20s %s\n", "..", "Discard the current context and go to parent");
break;
case 'exit':
$this->stop();