Skip to content

Commit b2cab50

Browse files
committed
README and fixes
1 parent 5e2c529 commit b2cab50

File tree

3 files changed

+232
-4
lines changed

3 files changed

+232
-4
lines changed

README.md

Lines changed: 182 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,182 @@
1-
# php-simple-console
2-
One file console framework to help you write build scripts.
1+
# PHP Simple Console
2+
3+
Single file console framework to help you write build scripts.
4+
5+
## Installation
6+
7+
Use composer:
8+
9+
``` bash
10+
composer require asika/simple-console
11+
```
12+
13+
Or downlaod single file to use: [Download Here](https://raw.githubusercontent.com/asika32764/php-simple-console/master/src/Console.php)
14+
15+
## Getting Started
16+
17+
Use closure
18+
19+
``` php
20+
// Include single file
21+
include_once __DIR__ . '/Console.php';
22+
23+
// Or use composer
24+
include_once __DIR__ . '/vendor/autolod.php';
25+
26+
$app = new \Asika\SimpleConsole\Console;
27+
28+
// Use closure
29+
$app->execute(function (\Asika\SimpleConsole\Console $app)
30+
{
31+
// PHP 5.3
32+
$app->out('Hello');
33+
34+
// PHP 5.4 or higher use $this
35+
$this->out('Hello');
36+
37+
// Return TRUE will auto convert to 0 exitcode.
38+
return true;
39+
});
40+
```
41+
42+
Or Create your own class.
43+
44+
``` php
45+
class Build extends \Asika\SimpleConsole\Console
46+
{
47+
protected $help = <<<HELP
48+
[Usage] php build.php <version>
49+
50+
[Options]
51+
h | help Show help information
52+
v Show more debug information.
53+
HELP;
54+
55+
protected function doExecute ()
56+
{
57+
$this->out('Hello');
58+
59+
// Return TRUE will auto convert to 0 exitcode.
60+
return true;
61+
}
62+
}
63+
64+
$app = new Build;
65+
$app->execute();
66+
```
67+
68+
## Show HELP
69+
70+
Add `-h` or `--help` to show usage, you can add custom usage to `$this->help`, or override `$this->getHelp()`.
71+
72+
If you want to change `h` and `help` option, override `$this->helpOptions = array('...')`.
73+
74+
## Handle Error
75+
76+
Just throw Exception in `doExecute()`, Console will auto catch error.
77+
78+
``` php
79+
throw new \RuntimeException('...');
80+
```
81+
82+
Add `-v` to show backtrace if error.
83+
84+
## Handle Wrong Arguments
85+
86+
Wrong Argument use `\Asika\SimpleConsole\CommandArgsException`
87+
88+
``` php
89+
$arg = $this->getArgument(0);
90+
91+
if (!$arg)
92+
{
93+
throw new \Asika\SimpleConsole\CommandArgsException('Please enter a name.');
94+
}
95+
```
96+
97+
Console will auto show help information.
98+
99+
``` bash
100+
[Warning] Please enter a name.
101+
102+
[Usage] console.php <name>
103+
104+
[Options]
105+
h | help Show help info.
106+
v Show more debug information.
107+
```
108+
109+
## API
110+
111+
### `getArgument($order[, $default = null])`
112+
113+
``` php
114+
$first = $this->getArgument(0, 'default value');
115+
```
116+
117+
### `setArgument($order, $$value)`
118+
119+
``` php
120+
$this->setArgument(1, 'value');
121+
```
122+
123+
### `getOption($name: array|string[, $default = null])`
124+
125+
Get option `--foo`
126+
127+
``` php
128+
$this->getOption('foo');
129+
```
130+
131+
Get option `-f` or `--foo`, first match will return.
132+
133+
``` php
134+
$this->getOption(array('f', 'foo'));
135+
```
136+
137+
> NOTE:
138+
> `-abc` will convert to `a => 1, b => 1, c => 1`
139+
> And `-vvv` will convert to `v => 3`
140+
141+
### `setOption($name, $value)`
142+
143+
Set otpion to toption list. `$name` also support array.
144+
145+
### `out($string[, $newline: bool = false])`
146+
147+
Write to STDOUT,
148+
149+
``` bash
150+
$this->out('Hello')->out('World');
151+
```
152+
153+
### `err($string[, $newline: bool = false])`
154+
155+
Write to STDERR
156+
157+
``` bash
158+
$this->err('Hello')->err('World');
159+
```
160+
161+
### `in($string[$default = null, $bool = false)`
162+
163+
Ask a question, read from STDIN
164+
165+
``` bash
166+
$un = $this->in('Please enter username: ', 'default_name');
167+
```
168+
169+
Read as boolean:
170+
171+
- `yes, y, 1, true` will convert to `TRUE`
172+
- `no, n, 0, false` will convert to `FALSE`
173+
174+
``` bash
175+
$bool = $this->in('Are you sure? [Y/n]', [default true/false], true);
176+
```
177+
178+
### `exec($cmd)`
179+
180+
A proxy to execute a cmd by `exec()` and return value.
181+
182+
It will add a title `>> {your command}` before exec so you will know what has been executed.

src/Console.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ class Console
5050
*/
5151
protected $helpOptions = array('h', 'help');
5252

53+
/**
54+
* Property booleanMapping.
55+
*
56+
* @var array
57+
*/
58+
protected $booleanMapping = array(
59+
0 => array('n', 'no', 'false', 0, '0', true),
60+
1 => array('y', 'yes', 'true', 1, '1', false, null)
61+
);
62+
5363
/**
5464
* CliInput constructor.
5565
*
@@ -292,14 +302,48 @@ public function err($text = null, $nl = true)
292302
* in
293303
*
294304
* @param string $ask
305+
* @param mixed $default
295306
*
296307
* @return string
297308
*/
298-
public function in($ask = '')
309+
public function in($ask = '', $default = null, $bool = false)
299310
{
300311
$this->out($ask, false);
301312

302-
return fread(STDIN);
313+
$in = rtrim(fread(STDIN, 8192), "\n\r");
314+
315+
if ($bool)
316+
{
317+
$in = $in === '' ? $default : $in;
318+
319+
return (bool) $this->mapBoolean($in);
320+
}
321+
322+
return $in === '' ? (string) $default : $in;
323+
}
324+
325+
/**
326+
* mapBoolean
327+
*
328+
* @param string $in
329+
*
330+
* @return bool
331+
*/
332+
public function mapBoolean($in)
333+
{
334+
$in = strtolower((string) $in);
335+
336+
if (in_array($in, $this->booleanMapping[0], true))
337+
{
338+
return false;
339+
}
340+
341+
if (in_array($in, $this->booleanMapping[1], true))
342+
{
343+
return true;
344+
}
345+
346+
return null;
303347
}
304348

305349
/**

test/test.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ class Foo extends \Asika\SimpleConsole\Console
1616
$app->execute(function ($app)
1717
{
1818
$this->out('Hello');
19+
20+
$a = $this->in('Are you sure [Y/n]', true, true);
21+
22+
$this->out($a);
1923
});

0 commit comments

Comments
 (0)