Skip to content
This repository was archived by the owner on Nov 26, 2017. It is now read-only.

Commit 61a3d8d

Browse files
author
Michael Babker
committed
Merge pull request #186 from eddieajau/Docs
Add simple example for CLI application
2 parents 324e144 + 0561030 commit 61a3d8d

File tree

1 file changed

+74
-18
lines changed

1 file changed

+74
-18
lines changed

Diff for: src/Joomla/Application/README.md

+74-18
Original file line numberDiff line numberDiff line change
@@ -173,32 +173,82 @@ You can provide customised implementations these methods by creating the followi
173173
* `mockWebSetBody`
174174
* `mockWebSetHeader`
175175

176-
## Colors for CLI Applications
176+
## Command Line Applications
177+
178+
The Joomla Framework provides an application class for making command line applications.
179+
180+
An example command line application skeleton:
181+
182+
```php
183+
<?php
184+
185+
use Joomla\Application\AbstractCliApplication;
186+
187+
// Bootstrap the autoloader (adjust path as appropriate to your situation).
188+
require_once __DIR__ . '/../vendor/autoload.php';
189+
190+
class MyCli extends AbstractCliApplication
191+
{
192+
protected function doExecute()
193+
{
194+
$this->out('It works');
195+
}
196+
}
197+
198+
$app = new MyCli;
199+
$app->execute();
200+
201+
```
202+
203+
### Colors for CLI Applications
177204

178205
It is possible to use colors on an ANSI enabled terminal.
179206

180207
```php
181-
// Green text
182-
$this->out('<info>foo</info>');
208+
class MyCli extends AbstractCliApplication
209+
{
210+
protected function doExecute()
211+
{
212+
// Green text
213+
$this->out('<info>foo</info>');
183214

184-
// Yellow text
185-
$this->out('<comment>foo</comment>');
215+
// Yellow text
216+
$this->out('<comment>foo</comment>');
186217

187-
// Black text on a cyan background
188-
$this->out('<question>foo</question>');
218+
// Black text on a cyan background
219+
$this->out('<question>foo</question>');
189220

190-
// White text on a red background
191-
$this->out('<error>foo</error>');
221+
// White text on a red background
222+
$this->out('<error>foo</error>');
223+
}
224+
}
192225
```
193226

194227
You can also create your own styles.
195228

196229
```php
197230
use Joomla\Application\Cli\Colorstyle;
198231

199-
$style = new Colorstyle('yellow', 'red', array('bold', 'blink'));
200-
$this->getOutput()->addStyle('fire', $style);
201-
$this->out('<fire>foo</fire>');
232+
class MyCli extends AbstractCliApplication
233+
{
234+
/**
235+
* Override to initialise the colour styles.
236+
*
237+
* @return void
238+
*
239+
* @since 1.0
240+
*/
241+
protected function initialise()
242+
{
243+
$style = new Colorstyle('yellow', 'red', array('bold', 'blink'));
244+
$this->getOutput()->addStyle('fire', $style);
245+
}
246+
247+
protected function doExecute()
248+
{
249+
$this->out('<fire>foo</fire>');
250+
}
251+
}
202252

203253
```
204254

@@ -209,14 +259,20 @@ And available options are: bold, underscore, blink and reverse.
209259
You can also set these colors and options inside the tagname:
210260

211261
```php
212-
// Green text
213-
$this->out('<fg=green>foo</fg=green>');
262+
class MyCli extends AbstractCliApplication
263+
{
264+
protected function doExecute()
265+
{
266+
// Green text
267+
$this->out('<fg=green>foo</fg=green>');
214268

215-
// Black text on a cyan background
216-
$this->out('<fg=black;bg=cyan>foo</fg=black;bg=cyan>');
269+
// Black text on a cyan background
270+
$this->out('<fg=black;bg=cyan>foo</fg=black;bg=cyan>');
217271

218-
// Bold text on a yellow background
219-
$this->out('<bg=yellow;options=bold>foo</bg=yellow;options=bold>');
272+
// Bold text on a yellow background
273+
$this->out('<bg=yellow;options=bold>foo</bg=yellow;options=bold>');
274+
}
275+
}
220276
```
221277

222278
## Installation via Composer

0 commit comments

Comments
 (0)