Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

nette-command-line

БесплатноБез исполняемых скриптовНе проверен

Invoke before writing or reviewing a PHP command-line script or tool that uses nette/command-line 2.0. Covers Command with addFlag(), addOption()…

netteот nette★ 47 звёзд на GitHub

Об этом скилле

Nette Command-Line

composer require nette/command-line

Version 2.0, PHP 8.2 to 8.5, no dependencies. Four classes, each doing one thing: Command describes what the script accepts, Parser reads a command line against it, HelpRenderer draws the help from it, Console writes to the terminal.

Code written for 1.x does not run on 2.0, and some of it fails silently (new Parser($help) drops its arguments). To upgrade, follow the upgrade guide step by step.

Defining

use Nette\CommandLine\Command;
use Nette\CommandLine\Console;
use Nette\CommandLine\HelpRenderer;
use Nette\CommandLine\Normalizers;
use Nette\CommandLine\ParseException;
use Nette\CommandLine\Parser;

$command = new Command('convert', 'Converts files between formats.');
$command->addFlag('--help', 'Print this help', alias: '-h', standalone: true);
$command->addFlag('--verbose', 'Verbose output', alias: '-v');
$command->addOption('--output', 'Output file', alias: '-o', valueName: 'file');
$command->addOption('--jobs', 'Parallel jobs', normalizer: Normalizers::int(min: 1), default: 4);
$command->addArgument('input', 'File to convert');

$args = (new Parser)->parse($command);   // $_SERVER['argv'], or pass the tokens without the script name
  • The second positional argument of add*() is the description; everything else is named. addOption('--output', '-o') makes -o the description, not the alias.
  • There are no setters: a parameter is checked when added and cannot be changed. Definition mistakes (a name without a dash, a duplicate, a required argument with a default, an argument after a repeatable one) throw InvalidArgumentException at once.
  • addFlag() takes neither enum nor normalizer; those belong to options and arguments.

Reading the ParseResult

parse() returns a read-only ParseResult read like an array, keyed by the names as defined, dashes included: $args['--output'], $args['input']. A name that was never defined throws InvalidArgumentException, so a typo does not pass as a missing value; isset() is false for both an unknown name and null. Anything needing a real array (count(), array_*(), json_encode(), a parameter typed array) needs $args->toArray().

Definition Absent Given bare Given with a value
flag null true refused
negatable: true, default: true true true, --no-x gives false refused
option null or default throws, value required the string
valueOptional: true null or default true only as --x=value
argument throws unless optional: true – the string
  • An optional value is attached with =. --format xml is the option without a value followed by the argument xml – or, in a program with commands, Unknown command 'xml'. With enum: [...] the next token is taken when it is one of the allowed values.
  • default is what the parameter is worth when absent and is returned as given, never normalized nor checked against enum.
  • enum: Format::class (a backed enum) parses straight into the case.
  • repeatable: true collects an array, [] when absent. Without it the last value wins.
  • wasProvided('--x') tells a value equal to the default from the default itself; isEmpty() means nothing followed the script name.
  • A normalizer (Normalizers::int(), float(), realPath(), or any closure) runs only on values from the line. Its exception reaches you as a ParseException naming the parameter.

Standalone Flags and Commands

A standalone: true flag answers on its own: the parser returns it as true, the other flags as given and every other parameter at its default, checking or converting nothing and without requiring a missing argument or command. An unknown option is still refused.

$cli = new Command('tool');
$cli->addFlag('--help', alias: '-h', standalone: true);
$check = $cli->addCommand('check', 'Report violations');
$check->addArgument('paths', optional: true, repeatable: true);

$result = (new Parser)->parse($cli);
if ($result['--help']) {
	(new HelpRenderer)->render($result->command);   // the help of the command the user asked about
	exit(0);
}

exit(match ($result->command) {
	$check => $app->check($result['paths']),
	$cli => $app->usage(),
});

$result->command is the Command object selected by the first positional word, the program itself when there is none (new Command(..., commandRequired: true) refuses that). A command accepts its own options and those of the commands above it. match without a default arm throws when a command is left unhandled.

Errors and Help

try {
	$result = (new Parser)->parse($cli);
} catch (ParseException $e) {
	$stderr = new Console(STDERR);
	$stderr->writeLine("Error: {$e->getMessage()}", 'red');
	(new HelpRenderer($stderr))->render($e->command);
	exit(2);
}

$e->reason is a ParseFailure case (UnknownOption, `Mi

Полный SKILL.md на GitHub

Установить nette-command-line в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

references/upgrading.md

FAQ

Что делает скилл nette-command-line?

Invoke before writing or reviewing a PHP command-line script or tool that uses nette/command-line 2.0. Covers Command with addFlag(), addOption(), addArgument() and addCommand(), the Parser and its ParseResult, standalone --help and --version, negatable flags, optional values, defaults, enum, repeatable parameters, Normalizers, commands and subcommands dispatched with match, HelpRenderer, ParseException and ParseFailure, and the Console (colors and ColorDepth, NO_COLOR and FORCE_COLOR, links, setStatus() progress lines) with Ansi for measuring text. Also trigger for bin/ scripts, CLI argument parsing, "print the help", colored terminal output or progress bars in PHP. Also trigger when upgrading from 1.x, for code using new Parser($help), addFromHelp(), addSwitch(), parseOnly() or Parser::normalizeRealPath(), and for errors such as "Call to undefined method Parser::addFromHelp()" or "Too few arguments to function Parser::parse()".

Как установить скилл nette-command-line?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл nette-command-line запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить nette-command-line с