Skip to content

Latest commit

 

History

History
175 lines (122 loc) · 7.13 KB

README.md

File metadata and controls

175 lines (122 loc) · 7.13 KB

JBZoo / Cli

CI codecov Psalm Coverage CodeFactor PHP Strict Types
Stable Version Total Downloads Dependents Visitors GitHub License

The library greatly extends the functionality of Symfony/Console and helps make creating new console utilities in PHP quicker and easier.

  • Improved progress bar with a new template and additional information. See ExamplesProgressBar.php.
  • Convert option values to a strict variable type. See ExamplesOptionsStrictTypes.php.
  • New built-in styles and colors for text output. See ExamplesStyles.php.
  • A powerful alias $this->_($messages, $level) instead of output->wrileln(). See ExamplesOutput.php.
  • Display timing and memory usage information with --profile option.
  • Show timestamp at the beginning of each message with --timestamp option.
  • Mute any sort of errors. So exit code will be always 0 (if it's possible) with --mute-errors.
  • None-zero exit code on any StdErr message with --non-zero-on-error option.
  • For any errors messages application will use StdOut instead of StdErr --stdout-only option (It's on your own risk!).
  • Disable progress bar animation for logs with --no-progress option.

Live Demo

asciicast

Installing

composer require jbzoo/cli

Usage example

The simplest CLI application has the following file structure. See the Demo App for more details.

/path/to/app/
    my-app                      # Binrary file (See below)
    composer.json               # Composer file
    /Commands/                  # Commands directory
        Simple.php              # One of the commands (See below)
    /vendor/
        autoload.php            # Composer autoload

./demo/composer.json

See Details
{
    "name"        : "vendor/cli-application",
    "type"        : "project",
    "description" : "Example of CLI App based on JBZoo/CLI",
    "license"     : "MIT",
    "keywords"    : ["cli", "application", "example"],

    "require"     : {
        "php"       : ">=7.2",
        "jbzoo/cli" : "^1.0.0"
    },

    "require-dev" : {
        "roave/security-advisories" : "dev-latest"
    },

    "autoload"    : {
        "psr-4" : {"DemoApp\\" : ""}
    },

    "bin"         : ["my-app"]
}

Binary file: demo/my-app

See Details
#!/usr/bin/env php
<?php declare(strict_types=1);

namespace ExampleApp;

use JBZoo\Cli\CliApplication;

require_once __DIR__ . '/vendor/autoload.php';

// Set your application name and version.
$application = new CliApplication('Your Application Name', 'v1.0.0');

// Scan directory to find commands.
//  * It doesn't work recursively!
//  * They must be inherited from the class \JBZoo\Cli\CliCommand
$application->registerCommandsByPath(__DIR__ . '/Commands', __NAMESPACE__);

// Execute it.
$application->run();

The simplest CLI action: ./demo/Commands/Simple.php

See Details
<?php declare(strict_types=1);

namespace DemoApp\Commands;

use JBZoo\Cli\CliCommand;
use JBZoo\Cli\Codes;

class Simple extends CliCommand
{
    protected function configure(): void
    {
        // Action name. It will be used in command line.
        // Example: `./my-app simple`
        $this->setName('simple');

        // Defined inhereted CLI options. See ./src/CliCommand.php for details.
        parent::configure();
    }

    protected function executeAction(): int
    {
        // Your code here
        $this->_('Hello world!');

        // Exit code. 0 - success, 1 - error.
        return Codes::OK;
    }
}

Useful projects and links

License

MIT

See Also

  • CI-Report-Converter - Converting different error reports for deep compatibility with popular CI systems.
  • Composer-Diff - See what packages have changed after composer update.
  • Composer-Graph - Dependency graph visualization of composer.json based on mermaid-js.
  • Mermaid-PHP - Generate diagrams and flowcharts with the help of the mermaid script language.
  • Utils - Collection of useful PHP functions, mini-classes, and snippets for every day.
  • Image - Package provides object-oriented way to manipulate with images as simple as possible.
  • Data - Extended implementation of ArrayObject. Use files as config/array.
  • Retry - Tiny PHP library providing retry/backoff functionality with multiple backoff strategies and jitter support.
  • SimpleTypes - Converting any values and measures - money, weight, exchange rates, length, ...