An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

[![Total Downloads](https://img.shields.io/packagist/dt/cakephp/http.svg?style=flat-square)](https://packagist.org/packages/cakephp/console)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](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)