fidry / console
Library to create CLI applications
Fund package maintenance!
theofidry
Installs: 717 286
Dependents: 3
Suggesters: 0
Security: 0
Stars: 19
Watchers: 5
Forks: 4
Open Issues: 11
Requires
- php: ^8.2
- psr/log: ^3.0
- symfony/console: ^6.4 || ^7.0
- symfony/deprecation-contracts: ^3.4
- symfony/event-dispatcher-contracts: ^2.5 || ^3.0
- symfony/service-contracts: ^2.5 || ^3.0
- thecodingmachine/safe: ^2.0
- webmozart/assert: ^1.11
Requires (Dev)
- bamarni/composer-bin-plugin: ^1.8.2
- composer/semver: ^3.3.2
- ergebnis/composer-normalize: ^2.33
- fidry/makefile: ^0.2.1 || ^1.0.0
- infection/infection: ^0.28
- phpunit/phpunit: ^10.2
- symfony/dependency-injection: ^6.4
- symfony/flex: ^2.4.0
- symfony/framework-bundle: ^6.4
- symfony/http-kernel: ^6.4
- symfony/yaml: ^6.4 || ^7.0
Conflicts
- symfony/dependency-injection: <6.4.0
- symfony/framework-bundle: <6.4.0
- symfony/http-kernel: <6.4.0
- dev-main / 1.0.x-dev
- 0.6.10
- 0.6.9
- 0.6.8
- 0.6.7
- 0.6.6
- 0.6.5
- 0.6.4
- 0.6.3
- 0.6.2
- 0.6.1
- 0.6.0
- 0.5.5
- 0.5.4
- 0.5.3
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-feat/command-tester
- dev-feat/sf72
- dev-chore/bump-72
- dev-chore/fix-dep
- dev-build/psalm
- dev-test/ci
- dev-dependabot/composer/dependencies-49a1f01d90
- dev-theofidry-patch-1
- dev-extend-command-notes
- dev-draft
- dev-refactor/register-commands
This package is auto-updated.
Last update: 2024-12-07 10:37:13 UTC
README
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:
- 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 (the input is validated when coercing it to a stricter type)
- Implement explicit interfaces instead of extending god classes
Table of Contents
- Installation with Symfony
- Usage preview
- Complete documentation
- Known Limitations
- Inspirations
- Contributing
Installation with Symfony
$ composer require theofidry/console
The Symfony Flex plugin should add the following:
<?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, Command\Configuration, ExitCode, IO }; use Symfony\Component\Console\Input\InputArgument; 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, [ new InputArgument( 'username', InputArgument::REQUIRED, 'Name of the user', ), new InputArgument( 'age', InputArgument::OPTIONAL, 'Age of the user', ), ], ); } public function execute(IO $io): int { $this->service->call( $io->getTypedArgument('username')->asStringNonEmptyList(), $io->getTypedArgument('age')->asNullablePositiveInteger(), ); return ExitCode::SUCCESS; } }
With the bundle enabled, those services are auto-configured into traditional Symfony commands.
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 obscure methods of
Application
Inspirations
- zenstruck/console-extra
- zenstruck/console-test
- webignition/symfony-console-typed-input
- webmozart-console
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