Skip to content

Commit a3d34b0

Browse files
authored
Merge pull request #1367 from platformsh/forward-dynamic-options-subcommand-2
Forward dynamic options when running a sub command (attempt #2)
2 parents d6ff397 + 027d24b commit a3d34b0

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

src/Command/CommandBase.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\Component\Console\Exception\InvalidArgumentException as ConsoleInvalidArgumentException;
3131
use Symfony\Component\Console\Input\ArgvInput;
3232
use Symfony\Component\Console\Input\ArrayInput;
33+
use Symfony\Component\Console\Input\InputDefinition;
3334
use Symfony\Component\Console\Input\InputInterface;
3435
use Symfony\Component\Console\Input\InputOption;
3536
use Symfony\Component\Console\Output\ConsoleOutputInterface;
@@ -1744,10 +1745,12 @@ protected function getSelectedEnvironment()
17441745
/**
17451746
* Run another CLI command.
17461747
*
1747-
* @param string $name
1748+
* @param string $name
17481749
* The name of the other command.
1749-
* @param array $arguments
1750+
* @param array $arguments
17501751
* Arguments for the other command.
1752+
* Unambiguous options that both commands have in common will be passed
1753+
* on automatically.
17511754
* @param OutputInterface $output
17521755
* The output for the other command. Defaults to the current output.
17531756
*
@@ -1760,12 +1763,8 @@ protected function runOtherCommand($name, array $arguments = [], OutputInterface
17601763
/** @var Command $command */
17611764
$command = $application->find($name);
17621765

1763-
// Pass on interactivity arguments to the other command.
1764-
if (isset($this->input) && $command->getDefinition()->hasOption('yes')) {
1765-
$arguments += [
1766-
'--yes' => $this->input->getOption('yes'),
1767-
'--no' => $this->input->getOption('no'),
1768-
];
1766+
if (isset($this->input)) {
1767+
$this->forwardStandardOptions($arguments, $this->input, $command->getDefinition());
17691768
}
17701769

17711770
$cmdInput = new ArrayInput(['command' => $name] + $arguments);
@@ -1801,6 +1800,41 @@ protected function runOtherCommand($name, array $arguments = [], OutputInterface
18011800
return $result;
18021801
}
18031802

1803+
/**
1804+
* Forwards standard (unambiguous) arguments that a source and target command have in common.
1805+
*
1806+
* @param array &$args
1807+
* @param InputInterface $input
1808+
* @param InputDefinition $targetDef
1809+
*/
1810+
private function forwardStandardOptions(array &$args, InputInterface $input, InputDefinition $targetDef)
1811+
{
1812+
$stdOptions = [
1813+
'no',
1814+
'no-interaction',
1815+
'yes',
1816+
1817+
'no-wait',
1818+
'wait',
1819+
1820+
'org',
1821+
'host',
1822+
'project',
1823+
'environment',
1824+
'app',
1825+
'worker',
1826+
'instance',
1827+
];
1828+
foreach ($stdOptions as $name) {
1829+
if (!\array_key_exists('--' . $name, $args) && $targetDef->hasOption($name) && $input->hasOption($name)) {
1830+
$value = $input->getOption($name);
1831+
if ($value !== null) {
1832+
$args['--' . $name] = $value;
1833+
}
1834+
}
1835+
}
1836+
}
1837+
18041838
/**
18051839
* Add aliases that should be hidden from help.
18061840
*

0 commit comments

Comments
 (0)