0Pricing
PHP Academy · Lesson

Console Component: CLI Commands

Build interactive CLI tools using Symfony Console Command classes.

Console Component: CLI Commands is a free PHP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is the Symfony Console Component?

Symfony Console lets you build structured, testable CLI applications in PHP with options, arguments, progress bars, and colourised output.

Installing

Install the component.

$ composer require symfony/console

Creating a Command

Extend Command and override configure() and execute().

<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GreetCommand extends Command
{
    protected static string $defaultName = "app:greet";

    protected function configure(): void
    {
        $this->setDescription("Greet a user")
             ->addArgument("name", \InputArgument::REQUIRED, "Name to greet")
             ->addOption("uppercase", "u", \InputOption::VALUE_NONE, "Uppercase output");
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $name = $input->getArgument("name");
        if ($input->getOption("uppercase")) $name = strtoupper($name);
        $output->writeln("Hello, $name!");
        return Command::SUCCESS;
    }
}

Application Setup

Register commands in a Application and run it.

<?php
use Symfony\Component\Console\Application;

$app = new Application("MyApp", "1.0.0");
$app->add(new GreetCommand());
$app->run();

Arguments and Options

Arguments are positional (required or optional). Options are named flags (--name=value or -n value).

<?php
$this->addArgument("file",  InputArgument::REQUIRED)
     ->addArgument("output", InputArgument::OPTIONAL, "Output file", "out.txt")
     ->addOption("verbose", "v",  InputOption::VALUE_NONE, "Verbose output")
     ->addOption("format",  "f",  InputOption::VALUE_REQUIRED, "Output format", "json");

Output Helpers

Format output with helper methods.

<?php
$output->writeln("<info>Success!</info>");
$output->writeln("<error>Error occurred</error>");
$output->writeln("<comment>Processing...</comment>");

ProgressBar

Display a progress bar for long-running tasks.

<?php
use Symfony\Component\Console\Helper\ProgressBar;

$bar = new ProgressBar($output, count($items));
$bar->start();
foreach ($items as $item) {
    processItem($item);
    $bar->advance();
}
$bar->finish();

Table Helper

Display tabular data in the terminal.

<?php
use Symfony\Component\Console\Helper\Table;

$table = new Table($output);
$table->setHeaders(["ID", "Name", "Email"])
      ->setRows($userData)
      ->render();

Question Helper

Prompt the user for input interactively.

<?php
use Symfony\Component\Console\Question\Question;
$helper   = $this->getHelper("question");
$question = new Question("Enter database password: ");
$question->setHidden(true);
$password = $helper->ask($input, $output, $question);

Return Codes

Return Command::SUCCESS (0), Command::FAILURE (1), or Command::INVALID (2) from execute(). CI systems use these codes.

In Laravel

Laravel Artisan commands extend Illuminate\Console\Command and use $signature and $description properties instead of configure().

Summary

Create commands by extending Command. Define arguments and options in configure(). Execute logic in execute(). Use helpers for progress bars, tables, and user prompts.

Quick Check

What integer should a successful command return from execute()?

Frequently asked questions

Is the “Console Component: CLI Commands” lesson free?

Yes — the full text of “Console Component: CLI Commands” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Console Component: CLI Commands”?

Build interactive CLI tools using Symfony Console Command classes. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Console Component: CLI Commands” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. HttpFoundation: Request and Response
  2. DependencyInjection Container
  3. EventDispatcher: Decoupled Events
  4. Console Component: CLI Commands
← Back to PHP Academy