|
| 1 | +# PHP Command Line Interface (CLI) Library |
| 2 | + |
| 3 | +- Version: v1.0.0 |
| 4 | +- Date: December 2nd 2018 |
| 5 | +- [Release notes](https://github.com/pointybeard/php-cli-lib/blob/master/CHANGELOG.md) |
| 6 | +- [GitHub repository](https://github.com/pointybeard/php-cli-lib) |
| 7 | + |
| 8 | +Collection of helpful classes to use when working on the command line (cli). |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +This library is installed via [Composer](http://getcomposer.org/). To install, use `composer require pointybeard/php-cli-lib` or add `"pointybeard/php-cli-lib": "~1.0"` to your `composer.json` file. |
| 13 | + |
| 14 | +And run composer to update your dependencies: |
| 15 | + |
| 16 | + $ curl -s http://getcomposer.org/installer | php |
| 17 | + $ php composer.phar update |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +This library is broken into 3 distinct components: Message, Prompt, and Argument. They can be used as needed, although they do rely on each other internally. |
| 22 | + |
| 23 | +### Message |
| 24 | + |
| 25 | +```php |
| 26 | +use CLILib\Message; |
| 27 | + |
| 28 | +## Display a message to STDOUT |
| 29 | +(new Message) |
| 30 | + ->message("This is my message") |
| 31 | + ->prependDate(true) |
| 32 | + ->dateFormat('G:i:s > ') |
| 33 | + ->appendNewLine(true) |
| 34 | + ->foreground("light green") |
| 35 | + ->background("red") |
| 36 | + ->display(STDOUT) |
| 37 | +; |
| 38 | +``` |
| 39 | + |
| 40 | +### Prompt |
| 41 | + |
| 42 | +```php |
| 43 | +use CLILib\Prompt; |
| 44 | +use CLILib\Message; |
| 45 | + |
| 46 | +// Most basic usage |
| 47 | +$name = Prompt::display( |
| 48 | + "Please enter your name" |
| 49 | +); |
| 50 | +## > Please enter your name: |
| 51 | +## > |
| 52 | + |
| 53 | +// Asking for a password |
| 54 | +$password = Prompt::display( |
| 55 | + "Please enter your password", Prompt::FLAG_SILENT |
| 56 | +); |
| 57 | +## > Please enter your password: |
| 58 | +## > |
| 59 | + |
| 60 | +// Providing a Message object and default value |
| 61 | +$message = (new Message) |
| 62 | + ->message("Enter database user name") |
| 63 | + ->prependDate(false) |
| 64 | + ->appendNewLine(false) |
| 65 | + ->foreground("light green") |
| 66 | + ->background("red") |
| 67 | +; |
| 68 | +$databaseUserName = Prompt::display( |
| 69 | + $message, null, "root", |
| 70 | +); |
| 71 | +## > Enter database user name [root]: |
| 72 | +## > |
| 73 | + |
| 74 | +// Using a validator to check the input |
| 75 | +$user = Prompt::display( |
| 76 | + "Enter your user name", null, null, function($input){ |
| 77 | + if(strlen(trim($input)) == 0) { |
| 78 | + (new Message |
| 79 | + ->message("Error: You must enter a user name!") |
| 80 | + ->prependDate(false) |
| 81 | + ->appendNewLine(true) |
| 82 | + ->foreground("red") |
| 83 | + ; |
| 84 | + return false; |
| 85 | + } |
| 86 | + return true; |
| 87 | + } |
| 88 | +); |
| 89 | +## > Enter your user name: |
| 90 | +## > |
| 91 | +## > Error: You must enter a user name! |
| 92 | +## > Enter your user name: |
| 93 | +## > |
| 94 | +``` |
| 95 | + |
| 96 | +### Argument & Argument/Iterator |
| 97 | + |
| 98 | +Include `CLILib\Argument` in your scripts then create an instance of `Argument\Iterator`. It will automatically look for arguments, or you can pass it your own argument string (see below). |
| 99 | + |
| 100 | +#### Syntax Supported |
| 101 | + |
| 102 | +This library supports the most common argument formats. Specifically `-x`,` --long`, `/x`. It also supports use of `=` or `:` as a delimiter. The following are examples of supported argument syntax: |
| 103 | + |
| 104 | + -x |
| 105 | + --aa |
| 106 | + --database=blah |
| 107 | + -d:blah |
| 108 | + --d blah |
| 109 | + --database-name=blah |
| 110 | + /d blah |
| 111 | + -u http://www.theproject.com |
| 112 | + -y something |
| 113 | + -p:\Users\pointybeard\Sites\shellargs\ |
| 114 | + -p:"\Users\pointybeard\Sites" |
| 115 | + -h:local:host |
| 116 | + /host=local-host |
| 117 | + |
| 118 | +#### Example |
| 119 | + |
| 120 | +```php |
| 121 | +use CLILib\Argument; |
| 122 | + |
| 123 | +// Load up the arguments from $argv. By default |
| 124 | +// it will ignore the first item, which is the |
| 125 | +// script name |
| 126 | +$args = new Argument\Iterator(); |
| 127 | + |
| 128 | +// Instead of using $argv, send in an array |
| 129 | +// of arguments. e.g. emulates "... -i --database blah" |
| 130 | +$args = new Argument\Iterator(false, [ |
| 131 | + '-i', '--database', 'blah' |
| 132 | +]); |
| 133 | + |
| 134 | +// Arguments can an entire string too [Added 1.0.1] |
| 135 | +$args = new Argument\Iterator(false, [ |
| 136 | + '-i --database blah' |
| 137 | +]); |
| 138 | + |
| 139 | +// Iterate over all the arguments |
| 140 | +foreach($args as $a){ |
| 141 | + printf("%s => %s" . PHP_EOL, $a->name(), $a->value()); |
| 142 | +} |
| 143 | + |
| 144 | +// Find a specific argument by name |
| 145 | +$args->find('i'); |
| 146 | + |
| 147 | +// Find also accepts an array of values, returning the first one that is valid |
| 148 | +$args->find(['h', 'help', 'usage']); |
| 149 | +``` |
| 150 | + |
| 151 | +## Running the Test Suite |
| 152 | + |
| 153 | +You can check that all code is passing by running the following command from the shell-args folder: |
| 154 | + |
| 155 | + ./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ArgumentsTest |
| 156 | + |
| 157 | +If you want to run code coverage (e.g. `--coverage-html tests/reports/ ...`) you'll need an older version of xdebug (for PHP 5.6). To install this, use the following commands: |
| 158 | + |
| 159 | + pecl channel-update pecl.php.net |
| 160 | + pecl install xdebug-2.5.5 |
| 161 | + |
| 162 | +You'll need enable `xdebug.so`. Try adding the following to `/etc/php/5.6/mods-available` |
| 163 | + |
| 164 | + ; configuration for php xdebug module |
| 165 | + ; priority=20 |
| 166 | + zend_extension=/usr/lib/php/20131226/xdebug.so |
| 167 | + |
| 168 | +Then enable it with `phpenmod xdebug`. The above works on Ubuntu, however, paths might be different for other distros. |
| 169 | + |
| 170 | +## Support |
| 171 | + |
| 172 | +If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/pointybeard/php-cli-lib/issues), |
| 173 | +or better yet, fork the library and submit a pull request. |
| 174 | + |
| 175 | +## Contributing |
| 176 | + |
| 177 | +We encourage you to contribute to this project. Please check out the [Contributing documentation](https://github.com/pointybeard/php-cli-lib/blob/master/CONTRIBUTING.md) for guidelines about how to get involved. |
| 178 | + |
| 179 | +## License |
| 180 | + |
| 181 | +"PHP Command Line Interface (CLI) Library" is released under the [MIT License](http://www.opensource.org/licenses/MIT). |
0 commit comments