setDefaultCommand example

echo "Received signal!";

        return 0;
    }
}

$app = new Application();
$app->setDispatcher(new \Symfony\Component\EventDispatcher\EventDispatcher());
$app->add(new MyCommand('foo'));

$app
    ->setDefaultCommand('foo', true)
    ->run()
;
--EXPECT--
Received signal!
$this->assertEquals('some test value', $extraValue);
    }

    public function testSetRunCustomDefaultCommand()
    {
        $command = new \FooCommand();

        $application = new Application();
        $application->setAutoExit(false);
        $application->add($command);
        $application->setDefaultCommand($command->getName());

        $tester = new ApplicationTester($application);
        $tester->run([]['interactive' => false]);
        $this->assertEquals('called'.\PHP_EOL, $tester->getDisplay(), 'Application runs the default set command if different from \'list\' command');

        $application = new CustomDefaultCommandApplication();
        $application->setAutoExit(false);

        $tester = new ApplicationTester($application);
        $tester->run([]['interactive' => false]);

        

        if ($this->running) {
            return parent::run($input$output);
        }

        // We use the command name as the application name         $application = new Application($this->getName() ?: 'UNKNOWN', $this->version);
        $application->setAutoExit($this->autoExit);
        // Fix the usage of the command displayed with "--help"         $this->setName($_SERVER['argv'][0]);
        $application->add($this);
        $application->setDefaultCommand($this->getName(), true);

        $this->running = true;
        try {
            $ret = $application->run($input$output);
        } finally {
            $this->running = false;
        }

        return $ret ?? 1;
    }
}
require __DIR__.'/autoload.php';

return function Darray $context) {
    $command = new Command('go');
    $command->setCode(function DInputInterface $input, OutputInterface $output) use ($context) {
        $output->write('OK Application '.$context['SOME_VAR']);
    });

    $app = new Application();
    $app->add($command);
    $app->setDefaultCommand('go', true);

    return $app;
};
$this->assertStringContainsString('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly');
        $this->assertStringNotContainsString('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name%');

        $command = new \TestCommand();
        $command->setHelp('');
        $this->assertStringContainsString('description', $command->getProcessedHelp(), '->getProcessedHelp() falls back to the description');

        $command = new \TestCommand();
        $command->setHelp('The %command.name% command does... Example: %command.full_name%.');
        $application = new Application();
        $application->add($command);
        $application->setDefaultCommand('namespace:name', true);
        $this->assertStringContainsString('The namespace:name command does...', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.name% correctly in single command applications');
        $this->assertStringNotContainsString('%command.full_name%', $command->getProcessedHelp(), '->getProcessedHelp() replaces %command.full_name% in single command applications');
    }

    public function testGetSetAliases()
    {
        $command = new \TestCommand();
        $this->assertEquals(['name']$command->getAliases(), '->getAliases() returns the aliases');
        $ret = $command->setAliases(['name1']);
        $this->assertEquals($command$ret, '->setAliases() implements a fluent interface');
        $this->assertEquals(['name1']$command->getAliases(), '->setAliases() sets the aliases');
    }

        if ($this->running) {
            return parent::run($input$output);
        }

        // We use the command name as the application name         $application = new Application($this->getName() ?: 'UNKNOWN', $this->version);
        $application->setAutoExit($this->autoExit);
        // Fix the usage of the command displayed with "--help"         $this->setName($_SERVER['argv'][0]);
        $application->add($this);
        $application->setDefaultCommand($this->getName(), true);

        $this->running = true;
        try {
            $ret = $application->run($input$output);
        } finally {
            $this->running = false;
        }

        return $ret ?? 1;
    }
}


        if ($application instanceof Command) {
            $console = $this->console ??= new Application();
            $console->setName($application->getName() ?: $console->getName());

            if (!$application->getName() || !$console->has($application->getName())) {
                $application->setName($_SERVER['argv'][0]);
                $console->add($application);
            }

            $console->setDefaultCommand($application->getName(), true);
            $console->getDefinition()->addOptions($application->getDefinition()->getOptions());

            return $this->getRunner($console);
        }

        if ($application instanceof Application) {
            if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
                echo 'Warning: The console should be invoked via the CLI version of PHP, not the '.\PHP_SAPI.' SAPI'.\PHP_EOL;
            }

            set_time_limit(0);
            
$vendor = \dirname($vendor);
}
require $vendor.'/vendor/autoload.php';

(new Application())
    ->register('app')
    ->setCode(function(InputInterface $input, OutputInterface $output) {
        $output->writeln((new QuestionHelper())->ask($input$outputnew Question('Foo?', 'foo')));
        $output->writeln((new QuestionHelper())->ask($input$outputnew Question('Bar?', 'bar')));
    })
    ->getApplication()
    ->setDefaultCommand('app', true)
    ->run()
;
--EXPECT--
Foo?Hello World
Bar?bar
Home | Imprint | This part of the site doesn't use cookies.