Nette Command-Line
Are you writing a PHP script that runs from the command line? Whether it's a little helper for one afternoon or a tool with a whole set of commands, this library does all the tedious work around it for you:
- Reads options and arguments and checks their values
- Builds the help and clear error messages
- Handles tools with several commands too
- Prints in color, but switches colors off by itself in a pipe or a log
- Has no dependencies
Installation
composer require nette/command-line
It requires PHP version 8.2 and supports PHP up to 8.6.
First Script
It's best shown with an example. Say you have a pile of images and want to convert them to another format. Let's write a script
convert.php for it, to be called like this:
php convert.php photo.jpg logo.png --format png
php convert.php photo.jpg --dry-run
The command line is made of three kinds of items. Arguments are plain values without a dash, here the file names. An
option has a name and a value: --format png says which format to convert to, and can be shortened to
-f png. A flag is just a switch with no value: --dry-run (-n for short) makes the
script only print what it would do, without touching a single file.
How much code does it take to make all of this work? Surprisingly little. You write the conversion itself as an ordinary method, and the attributes on its parameters tell how each value is given on the command line:
use Nette\CommandLine\Attributes as Cli;
use Nette\CommandLine\Command;
use Nette\CommandLine\Runner;
final class ImageConverter
{
#[Cli\Command('Converts images to another format')]
public function convert(
#[Cli\Argument('Images to convert', repeatable: true)]
array $files,
#[Cli\Option('Target format', alias: '-f', enum: ['jpg', 'png', 'webp'])]
string $format = 'webp',
#[Cli\Flag('Only show what would be done', alias: '-n')]
bool $dryRun = false,
): int
{
// converts each file to $format, with $dryRun only prints what it would do
return 0;
}
}
Notice that you never touch $argv or parse anything. The parameter $format becomes the option
--format, $dryRun the flag --dry-run, and the default value of a parameter applies when the
user leaves the option out. The texts in the attributes describe the items and show up in the help. repeatable: true
says there may be more files, so the method gets them as an array. enum lists the allowed values, so the script
refuses --format gif before the method is even called.
All that's left is to run the method. Below the class, in the same file convert.php, add:
require __DIR__ . '/vendor/autoload.php';
$converter = new ImageConverter;
$program = Command::from($converter->convert(...));
$runner = new Runner($program, title: 'Converter', version: '1.0');
exit($runner->run());
Writing $converter->convert(...) doesn't call the method, it passes it on. Command::from() reads
from it what the script accepts, and Runner takes care of the rest: it parses the command line, checks the values and
calls the method. The title and the version are what the script prints for --version. Whatever the method returns
becomes the exit code of the script. That's how the shell, CI or another script learns how things went: 0 means success, anything
else a problem.
And since people at the command line have their habits, the script copes with all the usual spellings:
--format png, --format=png and -f png mean the same, single-letter options can be stuck
together (-nf png is -n -f png), and options may stand anywhere between the files.
Help and Errors
Now try running php convert.php --help. Runner has given the script the options --help
and --version, and built the help from the descriptions in the attributes:

You didn't have to write it, and yet it is complete, aligned and in color. And since it comes from the same method that does the work, it will never claim anything other than what the script really can do.
The script is just as well prepared for mistakes. When the user makes a typo or enters nonsense, they don't get a stack trace but a clear answer:
$ php convert.php --fromat png photo.jpg
Error: Unknown option `--fromat`. Did you mean `--format`?
$ php convert.php --format gif photo.jpg
Error: Option `--format`: expects `jpg`, `png` or `webp`, `gif` given.
The help is printed right below the message, so the user doesn't have to look anything up, and the script ends with exit code 2, the usual way to report a wrong command line. Your method isn't called at all in that case, so it never runs with nonsensical values.
Without Attributes
Attributes are the most comfortable way: each option is described right next to the parameter that receives it, the method gets the values straight in its parameters, and there's no separate definition to keep in sync with the code. Underneath them, though, lies a plain API you can use directly. It comes in handy for a quick script without a class, or when you put the options together at runtime:
use Nette\CommandLine\Command;
use Nette\CommandLine\Parser;
$command = new Command('convert');
$command->addOption('--format', 'Target format', alias: '-f', enum: ['jpg', 'png', 'webp']);
$command->addArgument('files', 'Images to convert', repeatable: true);
$args = (new Parser)->parse($command);
$args->get('--format'); // 'png'
$args->get('files'); // ['photo.jpg', 'logo.png']
Keep in mind, though, that Parser only reads. It doesn't handle the help or report mistakes for you, so a wrong
command line ends with an exception. How to add that, or how to bring in Runner here as well, is shown on the page Arguments, Options and Commands, which also covers everything options
and arguments can do.
Several Commands
Scripts have a habit of growing. Today it converts images, tomorrow it should shrink them too. Such tools are usually
controlled by commands, much like composer install and composer update:
php images.php convert photo.jpg --format png
php images.php resize photo.jpg --width 800
php images.php --quiet convert photo.jpg
You don't have to rewrite anything. Each command is one method with the #[Cli\Command] attribute, just like
convert() above, and a whole class of such methods is added in one call. Options that apply to all commands, like
--quiet on the last line, are added to the program as a whole:
$tools = new ImageTools; // contains the methods convert() and resize()
$program = new Command('images', commandRequired: true);
$program->addFlag('--quiet', 'Print nothing but errors', alias: '-q');
$program->addCommandsFrom($tools);
$runner = new Runner($program);
exit($runner->run());
commandRequired: true says that the tool does nothing without a command, so images.php alone is
reported as a mistake. Each command gets its own help (php images.php convert --help), and commands can be nested, so
php images.php cache clear, for example, is the command clear inside the command cache.
More in Commands.
Terminal Output
A script doesn't only read, it also writes. That's what the Console class is for, and it follows where the output
goes. In a terminal it writes in color, and when you redirect the output to a file or the script runs in CI, it leaves the colors
out by itself, so the log stays clean. The same with progress: in a terminal it shows on a line that keeps being rewritten, and it
never gets into the log at all.
You create the console once and hand it to the class that needs it, for example in the constructor:
final class ImageConverter
{
public function __construct(
private Console $console,
) {
}
#[Cli\Command('Converts images to another format')]
public function convert(
#[Cli\Argument('Images to convert', repeatable: true)]
array $files,
): int
{
foreach ($files as $file) {
$this->console->setStatus("Converting $file"); // the progress line
// ...
$this->console->writeLine("$file converted", 'green');
}
return 0;
}
}
$console = new Console;
$converter = new ImageConverter($console);
On top of that, text is printed exactly as it is. Some libraries look for color markup in the text, so XML or user input has to be escaped first or it breaks. Nothing like that can happen here. More on the page Terminal Output.
Testing
Finally, good news for everyone who writes tests: you test the script without running it from the command line. You pass
run() the individual words of the command line as if the user had typed them, and catch the error output in memory.
The example uses Nette Tester:
$output = fopen('php://memory', 'w+');
$runner = new Runner($program, err: new Console($output));
$exitCode = $runner->run(['--format', 'gif', 'photo.jpg']);
Assert::same(2, $exitCode);
Assert::contains('expects `jpg`, `png` or `webp`', stream_get_contents($output, offset: 0));
More in Testing.
Upgrading from 1.x
Version 2.0 is a new API with no compatibility layer: in 1.x a single Parser held the definition, parsed the line
and printed the help text it had been given, while in 2.0 a Command is the definition, Parser reads a
line against it and HelpRenderer draws the help from it.
The Nette plugin for Claude Code and Codex includes a guide by which your coding agent rewrites 1.x code to 2.0 faithfully and reports every place where 2.0 behaves differently.