1. Go to this page and download the library: Download silassare/kli 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/ */
// Per-action handler (checked first):
$act->handler(function (KliArgs $args) use ($kli): void {
// handle the action
});
// Command-level fallback handler (all actions that have no own handler):
$cmd->handler(function (KliAction $action, KliArgs $args) use ($kli): void {
// $action tells you which sub-command was invoked
});
$act->option('output', 'o')->string()->def('/tmp');
$act->option('verbose')->bool()->def(false);
$act->handler(function (KliArgs $args): void {
$path = $args->get('output'); // by name
$path = $args->get('o'); // by flag
$verbose = $args->get('verbose'); // bool
$extra = $args->getAnonymousArgs(); // anything after --
$first = $args->getAnonymousAt(0);
});
// Single position: the first bare token becomes --file
$act->option('file', 'f')->path()->offsets(0);
// Range: tokens at positions 0..2 are collected into an array
$act->option('files', 'f')->path()->multiple()->offsets(0, 2);
// Infinite range: all tokens from position 1 onward
$act->option('args')->string()->offsets(1, INF);
class RedNegativeFormatter implements KliTableCellFormatterInterface
{
public function format(mixed $value, KliTableHeader $header, array $row): string
{
return (string) $value;
}
public function getStyle(mixed $value, KliTableHeader $header, array $row): ?KliStyle
{
return $value < 0 ? (new KliStyle())->red() : null;
}
}
$table->borderStyle()->cyan(); // color all border characters
// Override specific border characters (merges into defaults):
$table->setBorderChars(['top-left' => '+', 'top' => '-']);
$kli->info('Hint: use --help'); // ℹ ... (cyan icon)
$kli->warn('Deprecated flag used'); // ⚠ ... (yellow icon)
$kli->success('All done'); // ✔ ... (green icon)
$kli->error('Something went wrong'); // ✖ ... (red bold icon)
$kli->writeLn('raw output'); // new line then string
$kli->write('inline output');
$kli->log('info', 'message'); // append to log file (if configured)
$kli->log('error', 'oops', ['key' => 'val']); // with context
$kli->bell(1); // terminal bell character
class MyTool extends Kli
{
public function getVersion(bool $full = false): string
{
$version = '2.3.1';
return $full ? basename($this->getCliEntryPoint()) . ' v' . $version : $version;
}
}
class MyApp extends Kli
{
// Custom version string
public function getVersion(bool $full = false): string { ... }
// Custom welcome banner shown in interactive mode and on --help
public function welcome(): void
{
$this->writeLn($this->style()->bold()->apply('Welcome to MyApp!'));
}
// Override input source (useful in tests)
public function readLine(string $prompt, bool $is_password = false): string { ... }
// Tell whether readLine() can prompt the user (default: interactive mode or STDIN is a TTY)
public function canPrompt(): bool { ... }
// Tell whether the last readLine() call reached the end of input
public function isEndOfInput(): bool { ... }
// Override terminate() to intercept exit() calls (useful in tests)
public function terminate(int $code = 0): never
{
throw new MyAppExitException($code);
}
}