Browse files

Better params

  • Loading branch information...
1 parent f2062da commit 2aac3b08c52ee9b7487afdb291e6756e0770ce30 @alganet alganet committed Feb 15, 2012
Showing with 51 additions and 5 deletions.
  1. +1 −1 bin/respect.php
  2. +12 −1 examples/README.md
  3. +38 −3 library/Respect/Cli/Runner.php
View
2 bin/respect.php
@@ -15,4 +15,4 @@
require $fileName;
});
-new Respect\Cli\Runner;
+new Respect\Cli\Runner;
View
13 examples/README.md
@@ -2,5 +2,16 @@ From the project root, run like this:
`php bin/respect.php --../examples/config.ini now format Y-m-d`
-You should see the current date formatted.
+or
+
+`php bin/respect.php --DateTime format Y-m-d`
+
+Params already work:
+
+`php bin/respect.php --DateTime --time=yesterday format Y-m-d`
+
+This doesn't work yet:
+
+`php bin/respect.php --Pdo --dsn='sqlite::memory:' query --statement="SELECT * FROM sqlite_master" fetchAll`
+
View
41 library/Respect/Cli/Runner.php
@@ -3,13 +3,15 @@
namespace Respect\Cli;
use Respect\Config\Container;
+use Respect\Config\Instantiator;
class Runner
{
public $arguments;
public $container;
public $autoDispatched = true;
public $command;
+ public $params = array();
function __construct(array $arguments = array())
{
global $argv;
@@ -20,18 +22,42 @@ function run()
foreach ($this->arguments as $i => $arg)
if ($this->matchConfigFile($arg))
$this->container = new Container(realpath($this->cleanupArgument($arg)));
+ elseif ($this->matchClass($arg))
+ $this->command = new Instantiator($this->cleanupArgument($arg));
+ elseif ($this->matchParam($arg))
+ $this->setCommandParam($this->cleanupArgument($arg));
elseif ($this->matchInstance($arg))
$this->command = $this->container->{$arg};
elseif ($this->matchCommand($arg))
- return call_user_func_array(
- array($this->command, $arg),
+ return $this->command = call_user_func_array(
+ array($this->getCommandForRun(), $arg),
array_slice($this->arguments, $i+1)
);
}
+ function getCommandForRun()
+ {
+ return $this->command instanceof Instantiator
+ ? call_user_func($this->command)
+ : $this->command;
+ }
+ function setCommandParam($paramAsString)
+ {
+ list($name, $value) = explode('=', $paramAsString);
+ $this->command->setParam($name, $value);
+ }
+ function matchParam($argument)
+ {
+ return $this->matchConfig($argument) && false !== stripos($argument, '=');
+ }
function cleanupArgument($dirtyArgument)
{
return ltrim($dirtyArgument, '- ');
}
+ function matchClass($argument)
+ {
+ return $this->matchConfig($argument)
+ && class_exists($this->cleanupArgument($argument));
+ }
function matchInstance($argument)
{
return $this->matchCommand($argument)
@@ -50,9 +76,18 @@ function matchConfigFile($argument)
{
return $this->matchConfig($argument) && false !== strripos($argument, '.ini');
}
+ function formatOutput($commandReturn)
+ {
+ if (is_array($commandReturn))
+ return print_r($commandReturn, 1);
+ elseif (is_scalar($commandReturn))
+ return $commandReturn;
+ elseif (!method_exists($commandReturn, '__toString'))
+ return get_class($commandReturn);
+ }
function __destruct()
{
if ($this->autoDispatched)
- print $this->run().PHP_EOL;
+ print $this->formatOutput($this->run()).PHP_EOL;
}
}

0 comments on commit 2aac3b0

Please sign in to comment.