Installation Add the package via Composer:
composer require 21torr/cli
Ensure Symfony/Console is already in your composer.json (this package wraps it).
Basic Usage
Import the Cli facade in your command class:
use App\Services\Cli;
class MyCommand extends Command
{
protected static $defaultName = 'app:my-command';
public function handle()
{
$cli = app(Cli::class);
$cli->info('Hello, Laravel!');
}
}
First Use Case Replace vanilla Symfony output with styled messages:
$cli->success('Operation completed!');
$cli->warning('This might fail.');
$cli->error('Critical error!');
src/Helpers/ in the package source for helper methods.tests/ for usage examples and edge cases.Styling Output
Replace getHelperSet()->get('formatter')->format() with:
$cli->success('Success message');
$cli->error('Error message');
$cli->warning('Warning message');
$cli->info('Informational message');
$cli->question('Question? [y/N]');
Progress Bars Use the built-in progress bar helper:
$progress = $cli->progressStart(100);
for ($i = 0; $i <= 100; $i++) {
$progress->advance();
}
$progress->finish();
Tables Render tabular data with auto-formatting:
$cli->table(['Name', 'Email'], [
['John Doe', 'john@example.com'],
['Jane Smith', 'jane@example.com'],
]);
Interactive Prompts Collect user input with validation:
$name = $cli->ask('What is your name?');
$confirm = $cli->confirm('Are you sure?', false);
$password = $cli->secret('Enter password:');
Dependency Injection
Bind the Cli service in AppServiceProvider for global access:
$this->app->bind(Cli::class, function ($app) {
return new Cli($app['symfony.console.style']);
});
Command Bootstrapping
Extend Command and inject Cli via constructor:
class MyCommand extends Command
{
protected $cli;
public function __construct(Cli $cli)
{
$this->cli = $cli;
parent::__construct();
}
}
Custom Styles
Override default styles in config/cli.php:
'styles' => [
'success' => ['fg' => 'green', 'options' => ['bold']],
'error' => ['fg' => 'red', 'options' => ['bold', 'blink']],
],
Logging Integration Pipe CLI output to Laravel logs:
$cli->info('Logging this...');
// Logs to `storage/logs/laravel.log` with `[info]` prefix.
Style Conflicts
xterm-256color or screen-256color terminals. Fallback to basic styles if needed:
$cli->setFallbackStyles(true);
Progress Bar Freezes
progressStart() in a separate process or add sleep(0) in loops:
for ($i = 0; $i < 100; $i++) {
usleep(1000); // Simulate work
$progress->advance();
}
Input Buffering
secret() may echo input in some terminals (e.g., Windows CMD).symfony/console’s native secret() as a fallback:
$password = $this->askSecret('Password:');
Configuration Overrides
config/cli.php may be ignored if cached.php artisan config:clear
Disable Styles Temporarily disable styles to debug layout issues:
$cli->disableStyles();
Log Raw Output Inspect raw output before styling:
$cli->debug('Raw debug message');
Check for Deprecations Monitor the changelog for breaking changes in minor updates.
Custom Helpers
Extend the Cli class to add domain-specific methods:
class ExtendedCli extends Cli
{
public function logDatabaseStats(array $stats)
{
$this->section('Database Stats');
$this->table(['Metric', 'Value'], $stats);
}
}
Theme Support Dynamically switch themes based on environment:
$cli->setTheme(config('app.env') === 'production' ? 'dark' : 'light');
Event Listeners
Hook into CLI events (e.g., Cli\Events\CommandStarting):
event(new Cli\Events\CommandStarting($commandName, $input));
Localization Override text labels (e.g., "Are you sure?") via language files:
$cli->setTranslation('confirm', 'Bist du sicher?');
How can I help you explore Laravel packages today?