https://github.com/czproject/phpcli
Helper class for PHP CLI apps.
https://github.com/czproject/phpcli
php php-cli
Last synced: 4 months ago
JSON representation
Helper class for PHP CLI apps.
- Host: GitHub
- URL: https://github.com/czproject/phpcli
- Owner: czproject
- License: other
- Created: 2012-10-27T16:26:49.000Z (about 13 years ago)
- Default Branch: master
- Last Pushed: 2025-06-11T07:37:44.000Z (7 months ago)
- Last Synced: 2025-08-23T04:00:45.451Z (5 months ago)
- Topics: php, php-cli
- Language: PHP
- Size: 176 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license.md
Awesome Lists containing this project
README
PHP CLI Console
===============
[](https://github.com/czproject/phpcli/actions)
[](https://packagist.org/packages/czproject/phpcli)
[](https://github.com/czproject/phpcli/releases)
[](https://github.com/czproject/phpcli/blob/master/license.md)
Installation
------------
[Download a latest package](https://github.com/czproject/phpcli/releases) or use [Composer](http://getcomposer.org/):
```
composer require czproject/phpcli
```
PhpCli requires PHP 5.6 or later, optionaly [Readline extension](http://www.php.net/manual/en/book.readline.php).
Usage
-----
``` php
use CzProject\PhpCli\ConsoleFactory;
require __DIR__ . '/vendor/autoload.php';
$console = ConsoleFactory::createConsole();
// output
$console->output('CzProject CLI Simple Console', 'green')
->nl() // new line
->output('Hey!', 'yellow')
->nl()
->output('Fred!', 'blue')
->nl()
->output('Fred is dead!', 'red')
->nl()
->output(['nooooooo...!', ' ', 'But, no problem!'], 'gray')
->nl()
->output('The end.')
->nl();
// input
$username = $console->input('Enter your name');
$console->output('Hello! ', 'blue')
->output($username, 'green')
->output(' [user]', 'yellow')
->nl() // print new line
->output('Bye!', 'blue')
->nl();
// input with default value
$username = $console->input('Enter your name', 'John');
// confirm
$agree = $console->confirm('Do you want to continue?');
// confirm with default value
$canQuit = $console->confirm('Really?', TRUE);
// select
$value = $console->select('Select color:', [
'value' => 'label',
'#ff0000' => 'Red',
'#00ff00' => 'Green',
'#0000ff' => 'Blue',
]);
// select with default value
$value = $console->select('Select color:', [
'value' => 'label',
'#ff0000' => 'Red',
'#00ff00' => 'Green',
'#0000ff' => 'Blue',
], '#ff0000');
```
## Parameters
**Arguments**
```php
$name = $console->getArgument(0)->getValue(); // string|NULL
$size = $console->getArgument(1, 'int')
->setRequired()
->addRule(function ($value) {
return $value > 0;
})
->getValue();
$price = $console->getArgument(2, 'float') // float
->setDefaultValue(100.0)
->getValue();
```
**Options**
```php
$name = $console->getOption('name')->getValue(); // string|NULL
$size = $console->getOption('size', 'int')
->setRequired()
->addRule(function ($value) {
return $value > 0;
})
->getValue();
$price = $console->getOption('price', 'float') // float
->setDefaultValue(100.0)
->getValue();
$words = $console->getOption('word')
->setRepeatable()
->getValue();
```
**Supported types**
* `string`
* `int` and `integer`
* `float`
* `bool` and `boolean`
--------------------------------------------------------------------------------
License: [New BSD License](license.md)
Author: Jan Pecha, https://www.janpecha.cz/