Skip to content

Commit 61441f5

Browse files
author
Alannah Kearney
committed
Initial commit
0 parents  commit 61441f5

File tree

14 files changed

+906
-0
lines changed

14 files changed

+906
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
vendor/
3+
tests/reports
4+
.php_cs.cache
5+
composer.lock

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
This project adheres to [Semantic Versioning](http://semver.org/).
4+
5+
## 1.0.0 - 2018-12-02
6+
#### Added
7+
- Initial release

CONTRIBUTING.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Contributing to this project
2+
3+
We encourage contribution to this project and only ask you follow some simple rules to make everyone's job a little easier.
4+
5+
## Found a bug?
6+
7+
Please lodge an issue at the GitHub issue tracker for this project -- [https://github.com/pointybeard/php-cli-lib/issues](https://github.com/pointybeard/php-cli-lib/issues)
8+
9+
Include details on the behaviour you are seeing, and steps needed to reproduce the problem.
10+
11+
## Want to contribute code?
12+
13+
* Fork the project
14+
* Make your feature addition or bug fix
15+
* Ensure your code is nicely formatted
16+
* Commit just the modifications, do not alter CHANGELOG.md. If relevant, link to GitHub issue (see [https://help.github.com/articles/closing-issues-via-commit-messages/](https://help.github.com/articles/closing-issues-via-commit-messages/))
17+
* Send the pull request
18+
19+
We will review the code and either merge it in, or leave some feedback.

LICENCE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
All source code included in the "PHP Command Line Interface (CLI) Library"
2+
archive is, unless otherwise specified, released under the MIT licence as
3+
follows:
4+
5+
----- begin license block -----
6+
7+
Copyright 2018 Alannah Kearney
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
27+
----- end license block -----

README.md

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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).

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "pointybeard/php-cli-lib",
3+
"description": "Collection of helpful classes to use when working on the command line (cli).",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Alannah Kearney",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"CLILib\\": "src/CLILib"
15+
}
16+
},
17+
"minimum-stability": "dev",
18+
"require": {
19+
"php": "^5.6"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^5"
23+
}
24+
}

phpunit.xml.dist

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.3/phpunit.xsd"
5+
backupGlobals="true"
6+
backupStaticAttributes="false"
7+
bootstrap="vendor/autoload.php"
8+
cacheTokens="false"
9+
colors="true"
10+
convertErrorsToExceptions="true"
11+
convertNoticesToExceptions="true"
12+
convertWarningsToExceptions="true"
13+
processIsolation="false"
14+
stopOnError="false"
15+
stopOnFailure="false"
16+
stopOnIncomplete="false"
17+
stopOnSkipped="false"
18+
verbose="false"
19+
>
20+
<testsuites>
21+
<testsuite name="Shell Arguments for PHP CLI Test Suite">
22+
<directory>./tests</directory>
23+
</testsuite>
24+
</testsuites>
25+
26+
<filter>
27+
<whitelist addUncoveredFilesFromWhitelist="true">
28+
<directory suffix=".php">./src</directory>
29+
</whitelist>
30+
</filter>
31+
32+
</phpunit>

src/CLILib/Argument.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace CLILib;
3+
4+
class Argument
5+
{
6+
private $name;
7+
private $value;
8+
9+
public function __construct($name, $value)
10+
{
11+
$this->name = $name;
12+
$this->value = $value;
13+
}
14+
15+
/**
16+
* Returns the $name property
17+
*
18+
* @return string
19+
*/
20+
public function name()
21+
{
22+
return $this->name;
23+
}
24+
25+
/**
26+
* Returns the $value property
27+
*
28+
* @return string
29+
*/
30+
public function value()
31+
{
32+
return $this->value;
33+
}
34+
}

0 commit comments

Comments
 (0)