https://github.com/cakephp/console
[Read only] Console libraries from CakePHP
https://github.com/cakephp/console
Last synced: 7 months ago
JSON representation
[Read only] Console libraries from CakePHP
- Host: GitHub
- URL: https://github.com/cakephp/console
- Owner: cakephp
- License: other
- Created: 2019-10-23T03:02:34.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-05-17T03:08:10.000Z (8 months ago)
- Last Synced: 2025-06-17T12:03:31.594Z (7 months ago)
- Language: PHP
- Size: 1.16 MB
- Stars: 5
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://packagist.org/packages/cakephp/console)
[](LICENSE.txt)
# 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`:
```php
#!/usr/bin/php -q
run($argv));
````
For our `Application` class we can start with:
```php
add('hello', HelloCommand::class);
return $commands;
}
}
```
Next we'll build a very simple `HelloCommand`:
```php
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("Hello {$args->getArgument('name')}");
}
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:
* [Option Parsing](https://book.cakephp.org/4/en/console-commands/option-parsers.html)
* [Input & Output](https://book.cakephp.org/4/en/console-commands/input-output.html)