Skip to content

Commit aa5d43a

Browse files
authored
Merge pull request #1 from adhocore/develop
Develop Version 0.0.1
2 parents e5dcd54 + 836535c commit aa5d43a

19 files changed

+1564
-0
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; http://editorconfig.org
2+
;
3+
; Sublime: https://github.com/sindresorhus/editorconfig-sublime
4+
; Phpstorm: https://plugins.jetbrains.com/plugin/7294-editorconfig
5+
6+
root = true
7+
8+
[*]
9+
indent_style = space
10+
indent_size = 2
11+
end_of_line = lf
12+
charset = utf-8
13+
trim_trailing_whitespace = true
14+
insert_final_newline = true
15+
16+
[{*.md,*.php,composer.json,composer.lock}]
17+
indent_size = 4

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# standards
2+
/.cache/
3+
/.env
4+
/.idea/
5+
/vendor/
6+
composer.lock
7+
*.local.*

.travis.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
- 7.2
7+
8+
install:
9+
- composer install --prefer-dist
10+
11+
before_script:
12+
- for P in src tests; do find $P -type f -name '*.php' -exec php -l {} \;; done
13+
14+
script:
15+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
16+
17+
after_success:
18+
- bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
## adhocore/cli
2+
3+
Command Line Interface utilities and helpers for PHP.
4+
5+
... a work in progress ...
6+
7+
[![Latest Version](https://img.shields.io/github/release/adhocore/cli.svg?style=flat-square)](https://github.com/adhocore/cli/releases)
8+
[![Travis Build](https://img.shields.io/travis/adhocore/cli/master.svg?style=flat-square)](https://travis-ci.org/adhocore/cli?branch=master)
9+
[![Scrutinizer CI](https://img.shields.io/scrutinizer/g/adhocore/cli.svg?style=flat-square)](https://scrutinizer-ci.com/g/adhocore/cli/?branch=master)
10+
[![Codecov branch](https://img.shields.io/codecov/c/github/adhocore/cli/master.svg?style=flat-square)](https://codecov.io/gh/adhocore/cli)
11+
[![StyleCI](https://styleci.io/repos/139012552/shield)](https://styleci.io/repos/139012552)
12+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
13+
14+
- Command line arguments parsing made easy
15+
- Inspired by nodejs [commander](https://github.com/tj/commander.js) (thanks tj)
16+
- For PHP5.6 or new and for good
17+
18+
(*PS: It is just argv parser and not ditto commander, so spawning new commands or actions are not supported*)
19+
20+
**Bonus**: Text coloring for the cli
21+
22+
## Installation
23+
```bash
24+
composer require adhocore/cli
25+
```
26+
27+
## Usage
28+
```php
29+
# coming soon
30+
```

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "adhocore/cli",
3+
"description": "Command line interface library for PHP",
4+
"type": "library",
5+
"keywords": [
6+
"php", "command", "argv-parser", "cli", "cli-color", "cli-action", "console",
7+
"cli-writer", "argument-parser", "cli-option"
8+
],
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Jitendra Adhikari",
13+
"email": "[email protected]"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {
18+
"Ahc\\Cli\\": "src/"
19+
}
20+
},
21+
"autoload-dev": {
22+
"psr-4": {
23+
"Ahc\\Cli\\Test\\": "tests/"
24+
}
25+
},
26+
"require": {
27+
"php": ">=7.0"
28+
},
29+
"require-dev": {
30+
"phpunit/phpunit": "^6.0"
31+
}
32+
}

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="tests/bootstrap.php"
13+
>
14+
<testsuites>
15+
<testsuite name="Test Suite">
16+
<directory>./tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
<filter>
20+
<whitelist processUncoveredFilesFromWhitelist="true">
21+
<directory suffix=".php">./src</directory>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/Argument.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Ahc\Cli;
4+
5+
/**
6+
* Cli Option.
7+
*
8+
* @author Jitendra Adhikari <[email protected]>
9+
* @license MIT
10+
*
11+
* @link https://github.com/adhocore/cli
12+
*/
13+
class Argument
14+
{
15+
use InflectsString;
16+
17+
protected $name;
18+
19+
protected $rawArg;
20+
21+
protected $default;
22+
23+
protected $required = false;
24+
25+
protected $variadic = false;
26+
27+
public function __construct(string $arg)
28+
{
29+
$this->rawArg = $arg;
30+
31+
$this->parse($arg);
32+
}
33+
34+
protected function parse(string $arg)
35+
{
36+
$this->required = $arg[0] === '<';
37+
$this->variadic = \strpos($arg, '...') !== false;
38+
$this->name = $name = \str_replace(['<', '>', '[', ']', '.'], '', $arg);
39+
40+
// Format is "name:default+value1,default+value2" ('+'' => ' ')!
41+
if (\strpos($name, ':') !== false) {
42+
$name = \str_replace('+', ' ', $name);
43+
list($this->name, $this->default) = \explode(':', $name, 2);
44+
}
45+
}
46+
47+
public function name(): string
48+
{
49+
return $this->name;
50+
}
51+
52+
public function attributeName(): string
53+
{
54+
return $this->toCamelCase($this->name);
55+
}
56+
57+
public function required(): bool
58+
{
59+
return $this->required;
60+
}
61+
62+
public function variadic(): bool
63+
{
64+
return $this->variadic;
65+
}
66+
67+
public function default()
68+
{
69+
if (!$this->variadic) {
70+
return $this->default;
71+
}
72+
73+
return null === $this->default ? [] : \explode(',', $this->default);
74+
}
75+
}

0 commit comments

Comments
 (0)