Library for creating CLI commands or applications

Related tags

Command Line console
Overview

Console

Motivation: this library purpose is to provide a lighter and more robust API for console commands and/or applications to symfony/console.

It can be used either in combination with FrameworkBundle to facilitate the creation of commands or as a stand-alone package to create a CLI application app.

Key differences:

  • Implement interfaces instead of extending classes
  • Leverages an IO object instead of Input + Output + SymfonyStyle which offers:
    • The API of SymfonyStyle but still access to the Input and Output objects
    • A typed API for arguments and options

Table of Contents

Installation with Symfony

<?php declare(strict_types=1);
// config/bundles.php

return [
    // ...
    // Symfony\Bundle\FrameworkBundle\Symfony\Bundle\FrameworkBundle()
    // ...
    Fidry\Console\FidryConsoleBundle::class => ['all' => true],
];

Usage preview

To implement a command you have to implement the Fidry\Console\Command\Command interface as follows:

<?php declare(strict_types=1);

namespace Acme;

use Acme\MyService;
use Fidry\Console\Command\Command;
use Fidry\Console\Command\Configuration;
use Fidry\Console\ExitCode;
use Fidry\Console\IO;

final class CommandWithService implements Command
{
    private MyService $service;

    public function __construct(MyService $service)
    {
        $this->service = $service;
    }

    public function getConfiguration(): Configuration
    {
        return new Configuration(
            'app:foo',
            'Calls MyService',
            <<<'EOT'
            The <info>%command.name</info> command calls MyService
            EOT,
        );
    }

    public function execute(IO $io): int
    {
        $this->service->call();

        return ExitCode::SUCCESS;
    }
}

With the bundle enabled, those services are auto-configured into traditional Symfony commands.

Complete documentation

Known limitations

Some limitations are due to lack of time dedicated to those or based on the assumption they are not necessary. Those choices may be revisited depending on of the use case presented.

  • Support for hidden commands (see doc)
  • Support for command aliases
  • Support for command usage configuration
  • Some methods of Application

Contributing

The project provides a Makefile in which the most common commands have been registered such as fixing the coding style or running the test.

# Print the list of available commands
make
# or
make help
Comments
  • PHPDoc file headers

    PHPDoc file headers

    Would you welcome valid File-level DocBlocks?

    /**
     * @package           Fidry\Console
     * @author            Théo FIDRY <[email protected]>
     * @copyright         2022 Théo FIDRY
     * @license           https://github.com/theofidry/console/blob/main/LICENSE MIT
     */
    
    opened by szepeviktor 5
  • Symlinked file does not exist

    Symlinked file does not exist

    PHPScoper requires fidry/console as a dependency. Your distributed production package (we use it through Composer) currently has a symlink in it that refers to a file ../tests/console that does not exist. I'm assuming it's some kind of file from their unit tests that they do not ship in their distributed package.

    Anyway, our build script was erroring out because of this when it needs to copy the files from PHPScoper (and its dependencies - hence fidry/console) to a different folder.

    For now, I managed to fix our build script by simply deleting the symlink, but I would love it if you could fix this in the Composer package.

    bug 
    opened by arnaudbroes 2
  • Add an enum variant

    Add an enum variant

    Example of workaround code used for PHP-Scoper:

        private static function getSymbolType(IO $io): SymbolType
        {
            $symbolType = $io->getArgument(self::SYMBOL_TYPE_ARG)->asString();
    
            return SymbolType::from($symbolType);
        }
    
    opened by theofidry 0
  • Bump to PHP8.1

    Bump to PHP8.1

    (waiting on being integrated in Box)

    This allows to:

    • Bump the Symfony min constraint to 6.x
    • Bump the Symfony contracts deps to 3.x
    • Bump thecodingmachine/safe to 2.x
    opened by theofidry 0
  • IO: add more variants

    IO: add more variants

    • [x] add non-empty string variants
    • [x] document that the string values are trimmed
    • [ ] enum based value
    • [x] choice based value
    • [x] allow to create new instance with different input/output
    • [ ] strict booleans: when there is an option without value ok; otherwise fail
    • [ ] string matching pattern
    opened by theofidry 0
Releases(0.5.5)
  • 0.5.5(Dec 18, 2022)

  • 0.5.4(Dec 18, 2022)

  • 0.5.3(Nov 3, 2022)

  • 0.5.2(Nov 1, 2022)

    Features

    • Provide a factory to get an error IO (#81)
    • Add unofficial Symfony 4 support (#94)

    Misc

    • Various doc fixes and improvements (#82, 83, #84)
    • Fix CS (#86, #93)
    • Adjust Bamarni configuration (#87, #90)
    • Add sponsoring link
    • Exclude the bin directory in the artifacts (#91)
    • Remove unused constant (#92)
    • Minor Makefile improvements (#97)
    Source code(tar.gz)
    Source code(zip)
  • 0.5.1(Jun 19, 2022)

    Features

    • Remove deprecated API (#77)
    • Bump dependencies (#80)

    Misc

    • Normalize CI config (#74)
    • Do no export unnecessary directories (#78)
    • Speed up infection by running threads (#79)
    Source code(tar.gz)
    Source code(zip)
  • 0.5.0(Jun 19, 2022)

    Features

    • Make DisplayNormalizer::removeTrailingSpaces() allow multiple extra normalizers (#63)
    • Add CommandTester (#58)
    • Add support for Symfony AppTester & CommandTester (#64)
    • Add argument/option name to the error message (#66)
    • Allow to coerce a type with a custom error message (#67)
    • Add IO::createDefault() (#69)
    • Add IO::hasOption() (#70)
    • Make ApplicationRunner::runApplication() IO optional (#72)
    Source code(tar.gz)
    Source code(zip)
  • 0.4.0(Feb 19, 2022)

    Features

    • Add support for Composer 2.2 (#20)
    • Add support for Symfony 6 (#21)
    • Add AppTester which is a variant of the Symfony ApplicationTester
    • Add various Psalm support improvements (#23, #28, #44)
    • Switch to a more elaborate and type-safe system (#25, #27, #30, #33, #34, #35, #36, #41, #42, #45, #46, #47, #48, #49, #50). The old API is still available but deprecated [Soft BC Break]:
    // Before
    function (\Fidry\Console\IO $io): bool {
        return $io->getBooleanArgument('my-arg');
    }
    
    // After
    function (\Fidry\Console\Input\IO $io): bool {
        return $io->getArgument('my-arg')->asBoolean();
    }
    
    • Move IO from the Fidry\Console namespace to Fidry\Console\Input. The old class is still available for BC. (#37) [Soft BC break]
    • Rename ConsoleAssert into InputAssert (#29) and enrich InputAssert [Soft BC Break] (the class was marked as @private)

    Note: support for PHP7.4 and 8.0 is planned to be dropped for the next release.

    Migration

    From 0.3.x to 0.4.x, there is no hard BC break. In other words, unless you were relying on classes marked as @private or @internal, nothing should break.

    Nonetheless deprecations have been introduced. The following guide is about taking care of those:

    • Replace imports of Fidry\Console\IO to Fidry\Console\Input\IO
    • Replace usages of $io->get*Argument('arg-name') to $io->getArgument('arg-name')->as*()
    • Replace usages of $io->get*Option('option-name') to $io->getOption('option-name')->as*()
    Source code(tar.gz)
    Source code(zip)
  • 0.3.0(Oct 15, 2021)

    Features

    • Bump to Symfony 5.3 (#8)
    • Add support for Symfony 5.3 lazy commands (#9, #15)
    • Add PHP 8.1 support (#10)

    Bugfixes

    • Return lists instead of arrays (#14)

    BC Breaks

    • Add LazyCommand::getDescription() static method
    • Renamed IO::getXArrayArgument() methods into IO::getXListArgument()

    Misc

    Various doc improvements (#5, #6, 25d29e4, 81bdefd, #11, #13, #16).

    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(May 16, 2021)

Owner
Théo FIDRY
Web developer. twitter: https://twitter.com/tfidry ; Medium: https://medium.com/@tfidry
Théo FIDRY
Cilex a lightweight framework for creating PHP CLI scripts inspired by Silex

Cilex, a simple Command Line Interface framework Cilex is a simple command line application framework to develop simple tools based on Symfony2 compon

null 624 Dec 6, 2022
☄️ PHP CLI mode development framework, supports Swoole, WorkerMan, FPM, CLI-Server

☄️ PHP CLI mode development framework, supports Swoole, WorkerMan, FPM, CLI-Server / PHP 命令行模式开发框架,支持 Swoole、WorkerMan、FPM、CLI-Server

Mix PHP 1.8k Jan 3, 2023
[ABANDONED] PHP library for executing commands on multiple remote machines, via SSH

#Shunt Inspired by Ruby's Capistrano, Shunt is PHP library for executing commands on multiple remote machines, via SSH. Specifically, this library was

The League of Extraordinary Packages 436 Feb 20, 2022
Simple but yet powerful library for running almost all artisan commands.

:artisan gui Simple but yet powerful library for running some artisan commands. Requirements Laravel 8.* php ^7.3 Installation Just install package: c

null 324 Dec 28, 2022
PHP library for executing commands on multiple remote machines, via SSH

#Shunt Inspired by Ruby's Capistrano, Shunt is PHP library for executing commands on multiple remote machines, via SSH. Specifically, this library was

The League of Extraordinary Packages 436 Feb 20, 2022
An Elegant CLI Library for PHP

Commando An Elegant PHP CLI Library Commando is a PHP command line interface library that beautifies and simplifies writing PHP scripts intended for c

Nate Good 793 Dec 25, 2022
A developer-friendly wrapper around execution of shell commands.

ptlis/shell-command A developer-friendly wrapper around execution of shell commands. There were several goals that inspired the creation of this packa

brian ridley 18 Dec 31, 2022
Handle signals in artisan commands

Using this package you can easily handle signals like SIGINT, SIGTERM in your Laravel app.

Spatie 96 Dec 29, 2022
A package built for lumen that ports most of the make commands from laravel.

A package built for lumen that ports most of the make commands from laravel. For lumen v5.1, but will most likely work for 5.2 as well. I haven't tested. If you have requests, let me know, or do it yourself and make a pull request

Michael Bonds 22 Mar 8, 2022
Supercharge your Symfony console commands!

zenstruck/console-extra A modular set of features to reduce configuration boilerplate for your commands: /** * Creates a user in the database. * *

Kevin Bond 29 Nov 19, 2022
A simple object oriented interface to execute shell commands in PHP

php-shellcommand php-shellcommand provides a simple object oriented interface to execute shell commands. Installing Prerequisites Your php version mus

Michael Härtl 283 Dec 10, 2022
🤖 GitHub Action to run symfony console commands.

Symfony Console GitHub Action Usage You can use it as a Github Action like this: # .github/workflows/lint.yml name: "Lint" on: pull_request: push

Nucleos 3 Oct 20, 2022
Helper commands for Laravel Beyond Crud

About beyond-crud-helpers This package has many helper commands for the Laravel BEyond CRUD project by Spatie Installation composer require --dev tarr

null 4 Mar 8, 2022
A PocketMine-MP plugin which allows the users to edit no permission message of commands

CommandPermissionMessage A PocketMine-MP plugin which allows the users to edit no permission message of commands Have you ever got bored by the red me

cosmicnebula200 3 May 29, 2022
Hentai Bash - This is the core of Hentai Terminal, responsible for the basic functions and commands

Hentai Bash - This is the core of Hentai Terminal, responsible for the basic functions and commands. It is mainly used for writing and executing commands.

Hentai Group 1 Jan 26, 2022
Skeleton for creating a new Command Line Interface application with a minimum of dependencies.

Skeleton for creating a new Command Line Interface application with a minimum of dependencies.

Richard van Laak 1 Jan 17, 2022
BetterWPCLI - a small, zero-dependencies, PHP library that helps you build enterprise WordPress command-line applications.

BetterWPCLI - a small, zero-dependencies, PHP library that helps you build enterprise WordPress command-line applications.

Snicco 5 Oct 7, 2022
🖥 Build beautiful PHP CLI menus. Simple yet Powerful. Expressive DSL.

Contents Minimum Requirements Installation Upgrading Usage Quick Setup Examples API Appearance Menu Title Colour Width Padding Margin Borders Exit But

PHP School 1.9k Dec 28, 2022