PHP code example of krzysztofzylka / console

1. Go to this page and download the library: Download krzysztofzylka/console 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/ */

    

krzysztofzylka / console example snippets


use Krzysztofzylka\Console\Prints;

Prints::line('Starting job');
Prints::info('Loading configuration');
Prints::success('Done');
Prints::warning('Running in dry-run mode');
Prints::error('Something went wrong');
Prints::section('Deployment', 'Production release');
Prints::bulletList(['Validate config', 'Run sync', 'Send notification']);
Prints::numberedList(['Build', 'Deploy', 'Verify']);
Prints::kv(['env' => 'prod', 'release' => '1.5.0']);
Prints::json(['service' => 'taskello', 'healthy' => true]);
Prints::blankLine();
Prints::write('Progress: 50%');

Prints::print('value');  // deprecated, use line()
Prints::sprint('value'); // deprecated, use write()

use Krzysztofzylka\Console\Args;

$parsed = Args::getArgs($argv);

/*
[
    'path' => 'bin/app.php',
    'args' => ['sync'],
    'params' => [
        'env' => 'prod',
        'force' => true,
        'tag' => ['v1', 'v2'],
    ],
]
*/

use Krzysztofzylka\Console\Args;

$parsed = Args::parse($argv, [
    'env' => ['type' => 'string', 'default' => 'dev', 'aliases' => ['e']],
    'force' => ['type' => 'bool', 'default' => false],
    'tag' => ['type' => 'string', 'multiple' => true],
    'retries' => ['type' => 'int', 'default' => 0],
]);

use Krzysztofzylka\Console\Form;

$name = Form::input('Name:');
$environment = Form::input('Environment:', 'prod');
$token = Form::secretInput('Token:', null, true);

use Krzysztofzylka\Console\Form;

Form::prompt('Continue?');
Form::prompt('Continue?', ' [y/n]', false, true);

use Krzysztofzylka\Console\Generator\Help;

$overview = new Help();
$overview->addHeader('Commands');
$overview->addHelp('serve', 'Serve the application');
$overview->addHelp('cache:clear', 'Clear cache');
$overview->addHelp('routes:list', 'List routes');

echo $overview->renderOverview();

$help = new Help();
$help->setTitle('Sync command');
$help->setDescription('Synchronize local data with a remote source.');
$help->setUsage('php bin/app sync <workspace> [--env=prod] [--force]');
$help->addCommand('sync', 'Synchronize local data with a remote source.');
$help->addArgument('workspace', 'Workspace identifier.', true);
$help->addOption('--env', 'Environment name.', false, 'prod', false, ['dev', 'stage', 'prod']);
$help->addOption('--force', 'Run command without confirmation.');
$help->addExample('php bin/app sync main --env=prod', 'Synchronize the main workspace in production.');

echo $help->renderCommandHelp();

use Krzysztofzylka\Console\Generator\Table;

$table = new Table();
$table->addColumn('Name', 'name');
$table->addColumn('Role', 'role', 'center');
$table->addColumn('Environment', 'environment');
$table->addColumn('Notes', 'notes', 'left', 18, true);
$table->addColumn('Requests', 'requests', 'right');
$table->addRow([
    'name' => 'Anna',
    'role' => 'Developer',
    'environment' => 'prod',
    'notes' => "Owns API rollout\nSupports on-call",
    'requests' => 1280,
]);
$table->addRow([
    'name' => 'Łukasz',
    'role' => 'QA',
    'environment' => 'stage',
    'notes' => 'Regression tests and smoke checks',
    'requests' => 315,
]);

$table->render();
$output = $table->renderToString();

use Krzysztofzylka\Console\ProgressBar;
use Krzysztofzylka\Console\Spinner;

$progress = new ProgressBar(5, 20);

for ($i = 1; $i <= 5; $i++) {
    $progress->setProgress($i, 'step ' . $i);
}

$progress->finish('completed');

$spinner = new Spinner();
$spinner->spin('Waiting...');
$spinner->finish('Done');

use Krzysztofzylka\Console\Helper\Color;

echo Color::wrap('Success', 'green');
echo Color::wrap('Warning', 'yellow');
echo Color::wrap('Important', 'bold');