Skip to content

[Console] Error calling command from default command #27838

Closed
@tobymackenzie

Description

@tobymackenzie

Symfony version(s) affected: 3.4

Description

Calling a command that has a required argument from another command works fine normally, but when called from a command being run as the default command, an error such as 'Cannot add a required argument after an optional one' will be thrown.

It may have something to do with the way the $definition is built in Application when $name is falsey, then Command::mergeApplicationDefinition() is called on Command::run(), but I'm not really sure.

Possibly related issue

How to reproduce
Create app.php

<?php
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
require(__DIR__ . '/vendor/autoload.php');

class DefaultCommand extends Command{
	static public $defaultName = 'default';
	protected function execute(InputInterface $input, OutputInterface $output){
		$output->writeln('DefaultCommand');
		$command = $this->getApplication()->find('other');
		$command->run(new ArrayInput([
			'command'=> 'other'
			,'arg'=> 'a'
		]), $output);
	}
}
class OtherCommand extends Command{
	static public $defaultName = 'other';
	protected function configure(){
		$this->addArgument('arg', InputArgument::REQUIRED);
	}
	protected function execute(InputInterface $input, OutputInterface $output){
		$output->writeln('OtherCommand ' . $input->getArgument('arg'));
	}
}

echo "----------\nRunning `" . implode(' ', $_SERVER['argv']) . "`\n";
$app = new Application();
$app->add(new DefaultCommand());
$app->add(new OtherCommand());
$app->setDefaultCommand('default');
$app->run();

Run in bash-like shell:

composer require symfony/console
php app.php default; php app.php

output:

----------
Running `app.php default`
DefaultCommand
OtherCommand a
----------
Running `app.php`
DefaultCommand


  Cannot add a required argument after an optional one.


default

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions