Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/ArgvParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ class ArgvParser

protected $_desc;

protected $_options = [];
protected $_options = [];

protected $_values = [];
protected $_values = [];

protected $_args = [];
protected $_args = [];

protected $_events = [];
protected $_events = [];

protected $_allowUnknown = false;

protected $_wasVariadic = false;
protected $_wasVariadic = false;

public function __construct($name, $desc = null, $allowUnknown = false)
{
$this->_name = $name;
$this->_desc = $desc;
$this->_name = $name;
$this->_desc = $desc;
$this->_allowUnknown = $allowUnknown;

$this->addDefaultOptions();
Expand Down Expand Up @@ -65,7 +65,7 @@ public function option($cmd, $desc = '', callable $filter = null, $default = nul
}

$this->_values[$option->attributeName()] = $option->default();
$this->_options[$option->long()] = $option;
$this->_options[$option->long()] = $option;

return $this;
}
Expand All @@ -83,8 +83,8 @@ public function parse(array $argv)
{
\array_shift($argv);

$argv = $this->normalize($argv);
$count = \count($argv);
$argv = $this->normalize($argv);
$count = \count($argv);

for ($i = 0; $i < $count; $i++) {
list($arg, $nextArg) = [$argv[$i], isset($argv[$i + 1]) ? $argv[$i + 1] : null];
Expand All @@ -110,10 +110,10 @@ protected function parseArgs($arg)

protected function parseOptions($arg, $nextArg, &$i)
{
$value = \substr($nextArg, 0, 1) === '-' ? null : $nextArg;
$value = \substr($nextArg, 0, 1) === '-' ? null : $nextArg;
$isValue = $value !== null;

$this->_lastOption = $option = $this->optionFor($arg);
$this->_lastOption = $option = $this->optionFor($arg);
$this->_wasVariadic = $option ? $option->variadic() : false;

if (!$option) {
Expand All @@ -139,7 +139,7 @@ protected function handleUnknown($arg, $value)
// Has some value, error!
if ($this->_values) {
throw new \RuntimeException(
\sprintf("Option %s not registered", $arg)
\sprintf('Option %s not registered', $arg)
);
}

Expand Down Expand Up @@ -262,8 +262,6 @@ protected function optionFor($arg)
return $option;
}
}

return null;
}

protected function emit($event)
Expand Down
8 changes: 4 additions & 4 deletions src/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
*/
class Color
{
const FG_RED = 31;
const FG_GREEN = 32;
const FG_RED = 31;
const FG_GREEN = 32;
const FG_YELLOW = 33;
const FG_BLUE = 36;
const FG_BLUE = 36;
// @todo

protected static $format = "\033[:bold:;:fg:;:bg:m:text:\033[0m";

protected static $styles = [];

protected static $muted = false;
protected static $muted = false;

public static function error($text, array $style = [], $eol = false)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class Option

public function __construct($cmd, $desc = '', $default = null, callable $filter = null)
{
$this->desc = $desc;
$this->default = $default;
$this->filter = $filter;
$this->desc = $desc;
$this->default = $default;
$this->filter = $filter;
$this->required = \strpos($cmd, '<') !== false;
$this->optional = \strpos($cmd, '[') !== false;

Expand Down
4 changes: 2 additions & 2 deletions tests/ArgvParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function test_new()
{
$p = new ArgvParser('ArgvParser');

$p->version('0.0.' . rand(1, 10));
$p->version('0.0.'.rand(1, 10));

$data = $this->data();
foreach ($data['options'] as $option) {
Expand All @@ -36,6 +36,6 @@ public function test_new()

public function data()
{
return require __DIR__ . '/fixture.php';
return require __DIR__.'/fixture.php';
}
}
4 changes: 2 additions & 2 deletions tests/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function test_filter()
$this->assertSame(10, $o->filter('10'));

$in = 'apple';
$o = new Option('-f, --fruit', 'Age', 'orange', 'strtoupper');
$o = new Option('-f, --fruit', 'Age', 'orange', 'strtoupper');

$this->assertNotSame($o->filter($in), $in);
$this->assertSame('APPLE', $o->filter($in));
Expand All @@ -64,7 +64,7 @@ public function test_filter()

public function data()
{
$f = require __DIR__ . '/fixture.php';
$f = require __DIR__.'/fixture.php';

return $f['options'];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__.'/../vendor/autoload.php';
38 changes: 19 additions & 19 deletions tests/fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
return [
'options' => [
'space req' => [
'cmd' => '-v --virtual <req>',
'cmd' => '-v --virtual <req>',
'expect' => [
'long' => '--virtual',
'short' => '-v',
'long' => '--virtual',
'short' => '-v',
'required' => true,
'variadic' => false,
'name' => 'virtual',
'aname' => 'virtual',
],
],
'comma opt' => [
'cmd' => '-f,--fruit [opt]',
'cmd' => '-f,--fruit [opt]',
'expect' => [
'long' => '--fruit',
'short' => '-f',
'long' => '--fruit',
'short' => '-f',
'required' => false,
'variadic' => false,
'name' => 'fruit',
'aname' => 'fruit',
],
],
'pipe ...' => [
'cmd' => '-a|--apple [opt...]',
'cmd' => '-a|--apple [opt...]',
'expect' => [
'long' => '--apple',
'short' => '-a',
'long' => '--apple',
'short' => '-a',
'required' => false,
'variadic' => true,
'name' => 'apple',
Expand All @@ -37,10 +37,10 @@
],
],
'--no' => [
'cmd' => '-n|--no-shit',
'cmd' => '-n|--no-shit',
'expect' => [
'long' => '--no-shit',
'short' => '-n',
'long' => '--no-shit',
'short' => '-n',
'required' => false,
'variadic' => false,
'name' => 'shit',
Expand All @@ -49,10 +49,10 @@
],
],
'--with' => [
'cmd' => '-w|--with-this',
'cmd' => '-w|--with-this',
'expect' => [
'long' => '--with-this',
'short' => '-w',
'long' => '--with-this',
'short' => '-w',
'required' => false,
'variadic' => false,
'name' => 'this',
Expand All @@ -62,10 +62,10 @@
],
],
'camel case' => [
'cmd' => '-C|--camel-case',
'cmd' => '-C|--camel-case',
'expect' => [
'long' => '--camel-case',
'short' => '-C',
'long' => '--camel-case',
'short' => '-C',
'required' => false,
'variadic' => false,
'name' => 'camel-case',
Expand Down Expand Up @@ -98,6 +98,6 @@
[
'argv' => ['--virtual'],
'throws' => [\RuntimeException::class, 'Option -v|--virtual is required'],
]
],
],
];