From 6d45dd2c4b8405698a5866cde3ee480978352c85 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Thu, 24 Mar 2016 22:52:03 -0700 Subject: [PATCH] Maintain passthrough args as an array rather than imploding into a string. 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. --- RoboFile.php | 10 ++++++++++ composer.json | 2 +- composer.lock | 18 ++++++++++-------- src/Runner.php | 22 ++++++++++++---------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/RoboFile.php b/RoboFile.php index 114b18e47..0d1cb0037 100644 --- a/RoboFile.php +++ b/RoboFile.php @@ -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']) { diff --git a/composer.json b/composer.json index 859109d42..220cc8869 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 4dbb40231..1f245ee21 100644 --- a/composer.lock +++ b/composer.lock @@ -4,21 +4,21 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "1d784788b0e55b955de66bfbd6359bc2", - "content-hash": "e6c4d0d08ae6292ba5d9e145e8828757", + "hash": "9c642f6bc24998a7d9c711cc406f8c56", + "content-hash": "dcadd99be7cbf9e90cf046d28bd0e578", "packages": [ { "name": "consolidation/annotation-command", - "version": "0.1.0", + "version": "dev-passthrough-args", "source": { "type": "git", "url": "https://github.com/consolidation-org/annotation-command.git", - "reference": "94c52fe313a12eb8743f1698bcb2e258a2852ca6" + "reference": "7c5868903f499d72051bae014147354ff8686dd0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/consolidation-org/annotation-command/zipball/94c52fe313a12eb8743f1698bcb2e258a2852ca6", - "reference": "94c52fe313a12eb8743f1698bcb2e258a2852ca6", + "url": "https://api.github.com/repos/consolidation-org/annotation-command/zipball/7c5868903f499d72051bae014147354ff8686dd0", + "reference": "7c5868903f499d72051bae014147354ff8686dd0", "shasum": "" }, "require": { @@ -53,7 +53,7 @@ } ], "description": "Initialize Symfony Console commands from annotated command class methods.", - "time": "2016-03-23 23:43:05" + "time": "2016-03-25 05:41:00" }, { "name": "consolidation/log", @@ -2714,7 +2714,9 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "consolidation/annotation-command": 20 + }, "prefer-stable": false, "prefer-lowest": false, "platform": { diff --git a/src/Runner.php b/src/Runner.php index cffcdd24f..f00ada6ed 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -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 { @@ -18,11 +19,6 @@ class Runner const ROBOCLASS = 'RoboFile'; const ROBOFILE = 'RoboFile.php'; - /** - * @var string PassThoughArgs - */ - protected $passThroughArgs = null; - /** * @var string RoboClass */ @@ -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); @@ -103,6 +99,7 @@ public function execute($input = null) } $container = Config::getContainer(); + $output = $container->get('output'); $app = $container->get('application'); if (!$this->loadRoboFile()) { @@ -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); } /** @@ -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); } @@ -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()