3 Commits

3 changed files with 81 additions and 9 deletions

View File

@ -4,6 +4,7 @@ require_once __DIR__."/../vendor/autoload.php";
use NoccyLabs\Shell\Shell;
use NoccyLabs\Shell\Command;
use NoccyLabs\Shell\Context;
class MyCommand extends Command
{
@ -13,6 +14,18 @@ class MyCommand extends Command
}
}
class MyContext extends Context
{
/**
* @args
* @help Useful test!
*/
public function test()
{
}
}
class MyShell extends Shell
{
@ -20,7 +33,8 @@ class MyShell extends Shell
protected function configure()
{
$context = $this->createContext();
$context = new MyContext();
$this->pushContext($context);
$context->addCommand("hello", function () {
echo "world\nthis\nis\na\ntest\n";
});

View File

@ -8,6 +8,8 @@ class Context
protected $commands = [];
protected $commandInfo = [];
protected $data = [];
@ -21,11 +23,42 @@ class Context
protected function configure()
{
// Override this to do setup stuff
$this->findCommands();
}
public function addCommand($command, callable $handler)
protected function findCommands()
{
$refl = new \ReflectionClass(get_called_class());
foreach ($refl->getMethods() as $method) {
$docblock = $method->getDocComment();
$lines = array_map(function ($line) {
return trim($line, "*/ \t");
}, explode("\n", $docblock));
$info = [];
foreach ($lines as $line) {
if (preg_match("/^@(command|help|args) (.+?)$/", $line, $match)) {
list($void,$key,$value) = $match;
$info[$key] = $value;
}
if (count($info)>0) {
$this->addCommand($method->getName(), [$this, $method->getName()], $info);
}
}
}
}
public function addCommand($command, callable $handler, array $info=[])
{
$this->commands[$command] = $handler;
$this->commandInfo[$command] = $info;
}
public function setCommandHelp($command, $help)
{
if (!array_key_exists($command, $this->commandInfo)) {
return;
}
$this->commandInfo[$command]['help'] = $help;
}
public function addCommands(array $commands)
@ -50,6 +83,18 @@ class Context
return $this->commands[$command];
}
public function getCommandHelp()
{
$ret = [];
foreach ($this->commands as $command=>$handler) {
$info = $this->commandInfo[$command];
$args = array_key_exists("args",$info)?$info['args']:"";
$help = array_key_exists("help",$info)?$info['help']:"";
$ret[trim("{$command} {$args}")] = $help;
}
return $ret;
}
public function getName()
{
return $this->name;

View File

@ -60,10 +60,17 @@ class Shell
public function getContextPath($separator=":")
{
// Return null if we don't have a current context
if (!$this->context)
return null;
// Assemble the contexts to walk
$stack = [ $this->context->getName() ];
foreach ($this->contextStack as $context) {
$stack[] = $context->getName();
}
// Reverse the order to make it more logical
$stack = array_reverse($stack);
return join($separator,$stack);
}
@ -192,19 +199,25 @@ class Shell
{
switch ($command) {
case '.':
printf("context<%s>:\n", $this->context->getName());
echo " ".join("\n ",explode("\n",json_encode($this->context->getData(),JSON_PRETTY_PRINT)))."\n";
printf("context<%s>: %s\n", $this->context->getName(), json_encode($this->context->getData()));
$level = 0;
foreach ($this->contextStack as $context) {
printf(" %s- context<%s>\n", str_repeat(" ",$level++), $context->getName());
}
break;
case '..':
if (count($this->contextStack)>0)
$this->popContext();
break;
case 'help':
echo
"Built in commands:\n".
" . Show current context\n".
" .. Go to parent context\n".
" exit Exit the shell\n";
$help = $this->context->getCommandHelp();
printf("Commands in current context:\n\n");
foreach ($help as $command=>$info) {
printf(" %-20s %s\n", $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");
break;
case 'exit':
$this->stop();