|
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. |
0 commit comments