Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward dynamic options when running a subcommand #1367

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 42 additions & 8 deletions src/Command/CommandBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Symfony\Component\Console\Exception\InvalidArgumentException as ConsoleInvalidArgumentException;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
Expand Down Expand Up @@ -1744,10 +1745,12 @@ protected function getSelectedEnvironment()
/**
* Run another CLI command.
*
* @param string $name
* @param string $name
* The name of the other command.
* @param array $arguments
* @param array $arguments
* Arguments for the other command.
* Unambiguous options that both commands have in common will be passed
* on automatically.
* @param OutputInterface $output
* The output for the other command. Defaults to the current output.
*
Expand All @@ -1760,12 +1763,8 @@ protected function runOtherCommand($name, array $arguments = [], OutputInterface
/** @var Command $command */
$command = $application->find($name);

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

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

/**
* Forwards standard (unambiguous) arguments that a source and target command have in common.
*
* @param array &$args
* @param InputInterface $input
* @param InputDefinition $targetDef
*/
private function forwardStandardOptions(array &$args, InputInterface $input, InputDefinition $targetDef)
{
$stdOptions = [
'no',
'no-interaction',
'yes',

'no-wait',
'wait',

'org',
'host',
'project',
'environment',
'app',
'worker',
'instance',
];
foreach ($stdOptions as $name) {
if (!\array_key_exists('--' . $name, $args) && $targetDef->hasOption($name) && $input->hasOption($name)) {
$value = $input->getOption($name);
if ($value !== null) {
$args['--' . $name] = $value;
}
}
}
}

/**
* Add aliases that should be hidden from help.
*
Expand Down
Loading