1. Go to this page and download the library: Download nette/command-line library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nette / command-line example snippets
use Nette\CommandLine\Command;
use Nette\CommandLine\Parser;
$command = new Command('convert');
$command->addFlag('--verbose', 'Enable verbose mode', alias: '-v');
$command->addOption('--output', 'Output file', alias: '-o');
$command->addArgument('input', 'File to convert');
$args = (new Parser)->parse($command);
$args['--verbose']; // true, or null if not used
$args['--output']; // 'file.txt', or null if not used
$args['input']; // 'photo.png'
$args = (new Parser)->parse($command, ['--verbose', '-o', 'out.txt', 'photo.png']);
$args = (new Parser)->parse($check, ['check', 'src', '--generate-baseline']);
$cli->addCommand('check', 'Report violations', handler: $checker->check(...))
->addArgument('paths', 'Files or directories', optional: true, repeatable: true);
$args = (new Parser)->parse($cli);
if ($args['--help']) {
(new HelpRenderer)->render($args->command);
exit(0);
}
exit($args->run());
public function check(array $paths, ?string $config, ParseResult $args): int
use Nette\CommandLine\Attributes\{Command, Option};
final class ConfigCommand
{
#[Command('Print the configuration')]
public function config(
ParseResult $args,
#[Option('Print it for one file', valueName: 'path')]
?string $file = null,
#[Option('Output format')]
Format $format = Format::Console,
): int
{
...
}
}
$cli->addCommandFrom((new ConfigCommand)->config(...));
$cli->addCommandsFrom(new ConfigCommand);
public static function parseJobs(string $value): int
{
return Normalizers::int(min: 1)($value);
}
final readonly class RunOptions
{
public function __construct(
#[Argument('Files or directories', repeatable: true)]
public array $paths = [],
#[Option('Number of worker processes', valueName: 'n', normalizer: [self::class, 'parseJobs'])]
public ?int $jobs = null,
) {
}
}
#[Command('Report violations')]
public function check(RunOptions $run): int
use Nette\CommandLine\HelpRenderer;
$command = new Command('convert', 'Converts files between formats.');
$command->addFlag('--verbose', 'Enable verbose mode', alias: '-v');
$command->addOption('--output', 'Output file', alias: '-o', valueName: 'file');
$command->addArgument('input', 'Input file');
(new HelpRenderer)->render($command);
use Nette\CommandLine\UsageException;
try {
$args = (new Parser)->parse($cli);
exit($args->run());
} catch (UsageException $e) {
(new HelpRenderer)->renderError($e);
exit(2);
}
function (Command $command, bool $stdin, array $paths): int {
if ($stdin && $paths) {
throw new UsageException('Paths cannot be combined with `--stdin`.', $command);
}
...
}
$console = new Console;
$console->writeLine('Error!', 'red');
$console->writeLine('White text on blue background', 'white/blue');
$console->writeLine($console->color('gray', 'Config ') . $file); // a line of several colors is composed
$console = new Console(STDOUT, colorDepth: ColorDepth::None); // or $console->setColorDepth(ColorDepth::None);
$console->hasColors(); // whether it colors
$console->getColorDepth(); // ColorDepth::None
$console->isTerminal(); // whether someone is watching: progress bars, prompts
$console->getWidth(); // columns of the terminal, 80 when it is not one
$console->writeLine('See ' . $console->link('https://nette.org/cli'));
$console->writeLine($console->link('https://nette.org/cli', 'the manual')); // "the manual (https://…)" without colors
foreach ($files as $i => $file) {
$console->setStatus(sprintf('%d/%d %s', $i + 1, count($files), $file));
$console->writeLine(check($file)); // erases the status and writes above it
}
$console->clearStatus();
use Nette\CommandLine\Ansi;
Ansi::measure($console->color('red', 'chyba')); // 5, the escape sequences do not count
Ansi::pad($console->color('red', 'chyba'), 10); // padded to 10 columns, not to 10 bytes
Ansi::truncate($path, 30, keepEnd: true); // cuts the front, so the file name stays
Ansi::strip($text); // the text without any escape sequence
use Nette\CommandLine\Prompt;
$prompt = new Prompt($console);
if ($prompt->isInteractive()) {
$answer = $prompt->choose('Apply this fix?', ['y', 'n', 'a', 'q'], default: 'n'); // Apply this fix? [y/N/a/q]
}