Skip to content

Commit

Permalink
Maintain passthrough args as an array rather than imploding into a st…
Browse files Browse the repository at this point in the history
…ring. Attach passthrough args to the input object instead of the command object. Handle passing in passthrough args as array values for commands whose last argument is an array.
  • Loading branch information
greg-1-anderson committed Mar 25, 2016
1 parent 4984153 commit 6d45dd2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
10 changes: 10 additions & 0 deletions RoboFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,16 @@ public function tryPara($options = ['printed' => false, 'error' => false])
$para->run();
}

public function tryArgs($a, $b = 'default')
{
$this->say("The parameter a is $a and b is $b");
}

public function tryArrayArgs(array $a)
{
$this->say("The parameters passed are:\n" . var_export($a, true));
}

public function tryOptbool($opts = ['silent|s' => false])
{
if (!$opts['silent']) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"php": ">=5.5.0",
"league/container": "~2.2",
"consolidation/log": "~1",
"consolidation/annotation-command": "~0",
"consolidation/annotation-command": "dev-passthrough-args",
"symfony/finder": "~2.5|~3.0",
"symfony/console": "~2.5|~3.0",
"symfony/process": "~2.5|~3.0",
Expand Down
18 changes: 10 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Consolidation\AnnotationCommand\AnnotationCommandFactory;
use Consolidation\AnnotationCommand\PassThroughArgsInput;

class Runner
{
Expand All @@ -18,11 +19,6 @@ class Runner
const ROBOCLASS = 'RoboFile';
const ROBOFILE = 'RoboFile.php';

/**
* @var string PassThoughArgs
*/
protected $passThroughArgs = null;

/**
* @var string RoboClass
*/
Expand Down Expand Up @@ -87,10 +83,10 @@ public function execute($input = null)
{
register_shutdown_function(array($this, 'shutdown'));
set_error_handler(array($this, 'handleError'));
$input = $this->prepareInput($input ? $input : $this->shebang($_SERVER['argv']));

// If we were not provided a container, then create one
if (!Config::hasContainer()) {
$input = $this->prepareInput($input ? $input : $this->shebang($_SERVER['argv']));
// Set up our dependency injection container.
$container = new RoboContainer();
static::configureContainer($container, $input);
Expand All @@ -103,6 +99,7 @@ public function execute($input = null)
}

$container = Config::getContainer();
$output = $container->get('output');
$app = $container->get('application');

if (!$this->loadRoboFile()) {
Expand All @@ -120,11 +117,11 @@ public function execute($input = null)

// Register commands for all of the public methods in the RoboFile.
$commandFactory = new AnnotationCommandFactory();
$commandList = $commandFactory->createCommandsFromClass($roboCommandFileInstance, $this->passThroughArgs);
$commandList = $commandFactory->createCommandsFromClass($roboCommandFileInstance);
foreach ($commandList as $command) {
$app->add($command);
}
$app->run($container->get('input'), $container->get('output'));
$app->run($input, $output);
}

/**
Expand Down Expand Up @@ -267,11 +264,12 @@ protected function isShebangLine($line)
*/
protected function prepareInput($argv)
{
$passThroughArgs = [];
$pos = array_search('--', $argv);

// cutting pass-through arguments
if ($pos !== false) {
$this->passThroughArgs = implode(' ', array_slice($argv, $pos+1));
$passThroughArgs = array_slice($argv, $pos+1);
$argv = array_slice($argv, 0, $pos);
}

Expand All @@ -293,7 +291,11 @@ protected function prepareInput($argv)
}
}
}
return new ArgvInput($argv);
$input = new ArgvInput($argv);
if ($passThroughArgs) {
$input = new PassThroughArgsInput($passThroughArgs, $input);
}
return $input;
}

public function shutdown()
Expand Down

0 comments on commit 6d45dd2

Please sign in to comment.