Provide some useful utils for the php CLI application.
- Simple CLI arguments and options parser.
- Terminal console color render
- CLI code highlighter
- Build simple CLI application
- CLI ENV information helper
composer require toolkit/cli-utils
Color::printf('<info>%s</info> world', 'hello');
Color::println('hello world', 'info');
Color::println('hello world', 'error');
Color::println('hello world', 'warning');
Color::println('hello world', 'success');
echo Color::render('hello world', 'success');
examples:
use \Toolkit\Cli\Util\Terminal;
Terminal::forward(3);
Terminal::backward(2);
Terminal::clearLine();
Terminal::clearScreen();
/**
* @method static showCursor()
* @method static hideCursor()
* @method static savePosition()
* @method static restorePosition()
* @method static toTop()
* @method static toColumn(int $step)
* @method static up(int $step = 1)
* @method static down(int $step = 1)
* @method static forward(int $step = 1)
* @method static backward(int $step = 1) Moves the terminal cursor backward
* @method static toPrevNLineStart(int $step = 1)
* @method static toNextNLineStart(int $step = 1)
* @method static coordinate(int $col, int $row = 0)
* @method static clearScreen()
* @method static clearLine()
* @method static clearToScreenBegin()
* @method static clearToScreenEnd()
* @method static scrollUp(int $step = 1)
* @method static scrollDown(int $step = 1)
* @method static showSecondaryScreen()
* @method static showPrimaryScreen()
*/
This is inspire
jakub-onderka/php-console-highlighter
use Toolkit\Cli\Highlighter;
// this is an comment
$rendered = Highlighter::create()->highlight(file_get_contents(__FILE__));
\Toolkit\Cli\Cli::write($rendered);
use Toolkit\Cli\Flags;
$argv = $_SERVER['argv'];
// notice: must shift first element.
$script = \array_shift($argv);
// do parse
[$args, $shortOpts, $longOpts] = Flags::parseArgv($argv);
You can quickly build an simple CLI application:
use Toolkit\Cli\App;
// create app instance
$app = new App([
'desc' => 'this is my cli application',
]);
Use closure:
$app->addCommand('test', function ($app) {
echo "args:\n";
/** @var Toolkit\Cli\App $app */
/** @noinspection ForgottenDebugOutputInspection */
print_r($app->getArgs());
}, 'the description text for the command: test');
Use closure with a config:
$app->addByConfig(function ($app) {
echo "args:\n";
/** @var Toolkit\Cli\App $app */
/** @noinspection ForgottenDebugOutputInspection */
print_r($app->getArgs());
}, [
'name' => 'cmd2',
'desc' => 'the description text for the command: test',
]);
Use an object:
use Toolkit\Cli\App;
class MyCommand
{
public function getHelpConfig(): array
{
$help = <<<STR
Options:
--info Output some information
Example:
{{fullCmd}}
STR;
return [
'name' => 'list',
'desc' => 'list all directory name in src/',
'help' => $help,
];
}
public function __invoke(App $app)
{
echo "hello\n";
}
}
// add command
$app->addObject(new MyCommand);
// run
$app->run();
Run demo: php example/liteApp
use Toolkit\Cli\Download;
$url = 'http://no2.php.net/distributions/php-7.2.5.tar.bz2';
$down = Download::file($url, '');
// $down->setShowType('bar');
// $down->setDebug(true);
$down->start();
Progress bar
Progress text
- https://github.com/inhere/php-console Build rich console application