cakephp / console
Build beautiful console applications with CakePHP
Requires
- php: >=8.1
- cakephp/core: ^5.1
- cakephp/event: ^5.1
- cakephp/log: ^5.1
- cakephp/utility: ^5.1
Suggests
- cakephp/datasource: To use the Command base classes
- cakephp/orm: To use the Command base classes
- dev-master
- 5.x-dev
- 5.1.1
- 5.1.0
- 5.1.0-RC2
- 5.1.0-RC1
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.0
- 5.0.0-RC2
- 5.0.0-RC1
- 5.0.0-beta2
- 5.0.0-beta1
- 4.x-dev
- 4.5.7
- 4.5.6
- 4.5.5
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.5.0-RC1
- 4.4.17
- 4.4.16
- 4.4.15
- 4.4.14
- 4.4.13
- 4.4.12
- 4.4.11
- 4.4.10
- 4.4.9
- 4.4.8
- 4.4.7
- 4.4.6
- 4.4.5
- 4.4.4
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.4.0-RC2
- 4.4.0-RC1
- 4.3.x-dev
- 4.3.11
- 4.3.10
- 4.3.9
- 4.3.8
- 4.3.7
- 4.3.6
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.3.0-RC4
- 4.3.0-RC3
- 4.3.0-RC2
- 4.3.0-RC1
- 4.2.x-dev
- 4.2.12
- 4.2.11
- 4.2.10
- 4.2.9
- 4.2.8
- 4.2.7
- 4.2.6
- 4.2.5
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.2.0-RC1
- 4.2.0-beta1
- 4.1.7
- 4.1.6
- 4.1.5
- 4.1.4
- 4.1.3
- 4.1.2
- 4.1.1
- 4.1.0
- 4.1.0-RC2
- 4.1.0-RC1
- 4.1.0-beta1
- 4.0.x-dev
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 4.0.0-RC2
- 4.0.0-RC1
- dev-5.next
- dev-4.next
This package is auto-updated.
Last update: 2024-11-04 03:00:24 UTC
README
CakePHP Console Library
This library provides a framework for building command line applications from a set of commands. It provides abstractions for defining option and argument parsers, and dispatching commands.
installation
You can install it from Composer. In your project:
composer require cakephp/console
Getting Started
To start, define an entry point script and Application class which defines
bootstrap logic, and binds your commands. Lets put our entrypoint script in
bin/tool.php
:
#!/usr/bin/php -q <?php // Check platform requirements require dirname(__DIR__) . '/vendor/autoload.php'; use App\Application; use Cake\Console\CommandRunner; // Build the runner with an application and root executable name. $runner = new CommandRunner(new Application(), 'tool'); exit($runner->run($argv));
For our Application
class we can start with:
<?php namespace App; use App\Command\HelloCommand; use Cake\Core\ConsoleApplicationInterface; use Cake\Console\CommandCollection; class Application implements ConsoleApplicationInterface { /** * Load all the application configuration and bootstrap logic. * * @return void */ public function bootstrap(): void { // Load configuration here. This is the first // method Cake\Console\CommandRunner will call on your application. } /** * Define the console commands for an application. * * @param \Cake\Console\CommandCollection $commands The CommandCollection to add commands into. * @return \Cake\Console\CommandCollection The updated collection. */ public function console(CommandCollection $commands): CommandCollection { $commands->add('hello', HelloCommand::class); return $commands; } }
Next we'll build a very simple HelloCommand
:
<?php namespace App\Command; use Cake\Console\Arguments; use Cake\Console\BaseCommand; use Cake\Console\ConsoleIo; use Cake\Console\ConsoleOptionParser; class HelloCommand extends BaseCommand { protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser { $parser ->addArgument('name', [ 'required' => true, 'help' => 'The name to say hello to', ]) ->addOption('color', [ 'choices' => ['none', 'green'], 'default' => 'none', 'help' => 'The color to use.' ]); return $parser; } public function execute(Arguments $args, ConsoleIo $io): ?int { $color = $args->getOption('color'); if ($color === 'none') { $io->out("Hello {$args->getArgument('name')}"); } elseif ($color == 'green') { $io->out("<success>Hello {$args->getArgument('name')}</success>"); } return static::CODE_SUCCESS; } }
Next we can run our command with php bin/tool.php hello Syd
. To learn more
about the various features we've used in this example read the docs: