Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Alannah Kearney committed Dec 2, 2018
0 parents commit 61441f5
Show file tree
Hide file tree
Showing 14 changed files with 906 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
vendor/
tests/reports
.php_cs.cache
composer.lock
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Change Log
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.0 - 2018-12-02
#### Added
- Initial release
19 changes: 19 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Contributing to this project

We encourage contribution to this project and only ask you follow some simple rules to make everyone's job a little easier.

## Found a bug?

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)

Include details on the behaviour you are seeing, and steps needed to reproduce the problem.

## Want to contribute code?

* Fork the project
* Make your feature addition or bug fix
* Ensure your code is nicely formatted
* 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/))
* Send the pull request

We will review the code and either merge it in, or leave some feedback.
27 changes: 27 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
All source code included in the "PHP Command Line Interface (CLI) Library"
archive is, unless otherwise specified, released under the MIT licence as
follows:

----- begin license block -----

Copyright 2018 Alannah Kearney

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

----- end license block -----
181 changes: 181 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
# PHP Command Line Interface (CLI) Library

- Version: v1.0.0
- Date: December 2nd 2018
- [Release notes](https://github.com/pointybeard/php-cli-lib/blob/master/CHANGELOG.md)
- [GitHub repository](https://github.com/pointybeard/php-cli-lib)

Collection of helpful classes to use when working on the command line (cli).

## Installation

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.

And run composer to update your dependencies:

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update

## Usage

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.

### Message

```php
use CLILib\Message;

## Display a message to STDOUT
(new Message)
->message("This is my message")
->prependDate(true)
->dateFormat('G:i:s > ')
->appendNewLine(true)
->foreground("light green")
->background("red")
->display(STDOUT)
;
```

### Prompt

```php
use CLILib\Prompt;
use CLILib\Message;

// Most basic usage
$name = Prompt::display(
"Please enter your name"
);
## > Please enter your name:
## >

// Asking for a password
$password = Prompt::display(
"Please enter your password", Prompt::FLAG_SILENT
);
## > Please enter your password:
## >

// Providing a Message object and default value
$message = (new Message)
->message("Enter database user name")
->prependDate(false)
->appendNewLine(false)
->foreground("light green")
->background("red")
;
$databaseUserName = Prompt::display(
$message, null, "root",
);
## > Enter database user name [root]:
## >

// Using a validator to check the input
$user = Prompt::display(
"Enter your user name", null, null, function($input){
if(strlen(trim($input)) == 0) {
(new Message
->message("Error: You must enter a user name!")
->prependDate(false)
->appendNewLine(true)
->foreground("red")
;
return false;
}
return true;
}
);
## > Enter your user name:
## >
## > Error: You must enter a user name!
## > Enter your user name:
## >
```

### Argument & Argument/Iterator

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).

#### Syntax Supported

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:

-x
--aa
--database=blah
-d:blah
--d blah
--database-name=blah
/d blah
-u http://www.theproject.com
-y something
-p:\Users\pointybeard\Sites\shellargs\
-p:"\Users\pointybeard\Sites"
-h:local:host
/host=local-host

#### Example

```php
use CLILib\Argument;

// Load up the arguments from $argv. By default
// it will ignore the first item, which is the
// script name
$args = new Argument\Iterator();

// Instead of using $argv, send in an array
// of arguments. e.g. emulates "... -i --database blah"
$args = new Argument\Iterator(false, [
'-i', '--database', 'blah'
]);

// Arguments can an entire string too [Added 1.0.1]
$args = new Argument\Iterator(false, [
'-i --database blah'
]);

// Iterate over all the arguments
foreach($args as $a){
printf("%s => %s" . PHP_EOL, $a->name(), $a->value());
}

// Find a specific argument by name
$args->find('i');

// Find also accepts an array of values, returning the first one that is valid
$args->find(['h', 'help', 'usage']);
```

## Running the Test Suite

You can check that all code is passing by running the following command from the shell-args folder:

./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/ArgumentsTest

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:

pecl channel-update pecl.php.net
pecl install xdebug-2.5.5

You'll need enable `xdebug.so`. Try adding the following to `/etc/php/5.6/mods-available`

; configuration for php xdebug module
; priority=20
zend_extension=/usr/lib/php/20131226/xdebug.so

Then enable it with `phpenmod xdebug`. The above works on Ubuntu, however, paths might be different for other distros.

## Support

If you believe you have found a bug, please report it using the [GitHub issue tracker](https://github.com/pointybeard/php-cli-lib/issues),
or better yet, fork the library and submit a pull request.

## Contributing

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.

## License

"PHP Command Line Interface (CLI) Library" is released under the [MIT License](http://www.opensource.org/licenses/MIT).
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "pointybeard/php-cli-lib",
"description": "Collection of helpful classes to use when working on the command line (cli).",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Alannah Kearney",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"CLILib\\": "src/CLILib"
}
},
"minimum-stability": "dev",
"require": {
"php": "^5.6"
},
"require-dev": {
"phpunit/phpunit": "^5"
}
}
32 changes: 32 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.3/phpunit.xsd"
backupGlobals="true"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
cacheTokens="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="false"
>
<testsuites>
<testsuite name="Shell Arguments for PHP CLI Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

</phpunit>
34 changes: 34 additions & 0 deletions src/CLILib/Argument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace CLILib;

class Argument
{
private $name;
private $value;

public function __construct($name, $value)
{
$this->name = $name;
$this->value = $value;
}

/**
* Returns the $name property
*
* @return string
*/
public function name()
{
return $this->name;
}

/**
* Returns the $value property
*
* @return string
*/
public function value()
{
return $this->value;
}
}
Loading

0 comments on commit 61441f5

Please sign in to comment.