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
41 changes: 1 addition & 40 deletions src/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,8 @@
*
* @link https://github.com/adhocore/cli
*/
class Argument
class Argument extends Parameter
{
use InflectsString;

protected $name;

protected $rawArg;

protected $default;

protected $required = false;

protected $variadic = false;

public function __construct(string $arg)
{
$this->rawArg = $arg;

$this->parse($arg);
}

protected function parse(string $arg)
{
$this->required = $arg[0] === '<';
Expand All @@ -44,26 +25,6 @@ protected function parse(string $arg)
}
}

public function name(): string
{
return $this->name;
}

public function attributeName(): string
{
return $this->toCamelCase($this->name);
}

public function required(): bool
{
return $this->required;
}

public function variadic(): bool
{
return $this->variadic;
}

public function default()
{
if (!$this->variadic) {
Expand Down
36 changes: 26 additions & 10 deletions src/ArgvParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ public function arguments(string $definitions): self
throw new \InvalidArgumentException('Only last argument can be variadic');
}

$this->_arguments[$argument->name()] = $argument;
$this->_values[$argument->attributeName()] = $argument->default();
$name = $argument->attributeName();

$this->ifAlreadyRegistered($name, $argument);

$this->_arguments[$name] = $argument;
$this->_values[$name] = $argument->default();
}

return $this;
Expand All @@ -97,19 +101,26 @@ public function arguments(string $definitions): self
public function option(string $cmd, string $desc = '', callable $filter = null, $default = null): self
{
$option = new Option($cmd, $desc, $default, $filter);
$name = $option->attributeName();

if (isset($this->_options[$option->long()])) {
throw new \InvalidArgumentException(
\sprintf('The option "%s" is already registered', $option->long())
);
}
$this->ifAlreadyRegistered($name, $option);

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

return $this;
}

protected function ifAlreadyRegistered(string $name, Parameter $param)
{
if (\array_key_exists($name, $this->_values)) {
throw new \InvalidArgumentException(\sprintf(
'The parameter "%s" is already registered',
$param instanceof Option ? $param->long() : $param->name()
));
}
}

/**
* Sets event handler for last option.
*
Expand Down Expand Up @@ -176,7 +187,12 @@ public function values(bool $withDefaults = true): array
*/
public function __get(string $key)
{
return isset($this->_values[$key]) ? $this->_values[$key] : null;
return $this->_values[$key] ?? null;
}

public function args(): array
{
return \array_diff_key($this->_values, $this->_options);
}

protected function showHelp()
Expand Down
71 changes: 10 additions & 61 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,37 @@
*
* @link https://github.com/adhocore/cli
*/
class Option
class Option extends Parameter
{
use InflectsString;

protected $short;

protected $long;

protected $desc;

protected $rawCmd;

protected $default;

protected $required = true;

protected $optional = false;

protected $variadic = false;

protected $filter;

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

if ($this->variadic = \strpos($cmd, '...') !== false) {
$this->default = (array) $this->default;
}
$this->filter = $filter;

$this->parse($cmd);
parent::__construct($raw, $desc, $default);
}

protected function parse(string $cmd)
protected function parse(string $raw)
{
if (\strpos($cmd, '-with-') !== false) {
if (\strpos($raw, '-with-') !== false) {
$this->default = false;
} elseif (\strpos($cmd, '-no-') !== false) {
} elseif (\strpos($raw, '-no-') !== false) {
$this->default = true;
}

$parts = \preg_split('/[\s,\|]+/', $cmd);
$parts = \preg_split('/[\s,\|]+/', $raw);

$this->short = $this->long = $parts[0];
if (isset($parts[1])) {
$this->long = $parts[1];
}

$this->name = \str_replace(['--', 'no-', 'with-'], '', $this->long);
}

public function long(): string
Expand All @@ -74,41 +53,11 @@ public function short(): string
return $this->short;
}

public function name(): string
{
return \str_replace(['--', 'no-', 'with-'], '', $this->long);
}

public function attributeName(): string
{
return $this->toCamelCase($this->name());
}

public function is($arg): bool
{
return $this->short === $arg || $this->long === $arg;
}

public function required(): bool
{
return $this->required;
}

public function optional(): bool
{
return $this->optional;
}

public function variadic(): bool
{
return $this->variadic;
}

public function default()
{
return $this->default;
}

public function bool(): bool
{
return \preg_match('/\-no|\-with/', $this->long) > 0;
Expand Down
79 changes: 79 additions & 0 deletions src/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Ahc\Cli;

/**
* Cli Parameter.
*
* @author Jitendra Adhikari <jiten.adhikary@gmail.com>
* @license MIT
*
* @link https://github.com/adhocore/cli
*/
abstract class Parameter
{
use InflectsString;

protected $name;

protected $raw;

protected $desc;

protected $default;

protected $required = false;

protected $optional = false;

protected $variadic = false;

public function __construct(string $raw, string $desc = null, $default = null)
{
$this->raw = $raw;
$this->desc = $desc;
$this->default = $default;
$this->required = \strpos($raw, '<') !== false;
$this->optional = \strpos($raw, '[') !== false;
$this->variadic = \strpos($raw, '...') !== false;

$this->parse($raw);
}

abstract protected function parse(string $raw);

public function raw(): string
{
return $this->raw;
}

public function name(): string
{
return $this->name;
}

public function attributeName(): string
{
return $this->toCamelCase($this->name);
}

public function required(): bool
{
return $this->required;
}

public function optional(): bool
{
return $this->optional;
}

public function variadic(): bool
{
return $this->variadic;
}

public function default()
{
return $this->default;
}
}
14 changes: 6 additions & 8 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ public function parse(array $argv): self
$argv = $this->normalize($argv);
$count = \count($argv);

$literal = false;
for ($i = 0; $i < $count; $i++) {
list($arg, $nextArg) = [$argv[$i], isset($argv[$i + 1]) ? $argv[$i + 1] : null];
list($arg, $nextArg) = [$argv[$i], $argv[$i + 1] ?? null];

if ($arg[0] !== '-' || !empty($literal) || ($literal = $arg === '--')) {
$literal = $literal ?: $arg === '--';
if ($arg[0] !== '-' || $literal) {
$this->parseArgs($arg);
} else {
$i += (int) $this->parseOptions($arg, $nextArg);
Expand Down Expand Up @@ -86,7 +88,7 @@ protected function splitShort(string $arg): array

protected function parseArgs(string $arg)
{
if ($arg == '--') {
if ($arg === '--') {
return;
}

Expand Down Expand Up @@ -129,18 +131,14 @@ protected function parseOptions(string $arg, string $nextArg = null)
return $isValue;
}

$this->emit($option->long());
$this->emit($option->attributeName());
$this->setValue($option, $value);

return $isValue;
}

protected function optionFor(string $arg)
{
if (isset($this->_options[$arg])) {
return $this->_options[$arg];
}

foreach ($this->_options as $option) {
if ($option->is($arg)) {
return $option;
Expand Down
10 changes: 9 additions & 1 deletion tests/ArgvParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function test_arguments_with_options()
public function test_options_repeat()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The option "--apple" is already registered');
$this->expectExceptionMessage('The parameter "--apple" is already registered');

$p = $this->newParser()->option('-a --apple', 'Apple')->option('-a --apple', 'Apple');
}
Expand Down Expand Up @@ -203,6 +203,14 @@ public function test_no_value()
$this->assertNull($p->xyz);
}

public function test_args()
{
$p = $this->newParser()->arguments('<a> [b]')->option('-x --xyz')
->parse(['php', 'A', '-x', 'X', 'B', 'C', 'D']);

$this->assertSame(['a' => 'A', 'b' => 'B', 'C', 'D'], $p->args());
}

protected function newParser(string $version = '0.0.1', string $desc = null, bool $allowUnknown = false)
{
$p = new ArgvParser('ArgvParserTest', $desc, $allowUnknown);
Expand Down
2 changes: 2 additions & 0 deletions tests/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function test_new($cmd, $expect)
$more += ['bool' => $o->bool()];
}

$this->assertEquals($cmd, $o->raw());

$this->assertEquals($expect, [
'long' => $o->long(),
'short' => $o->short(),
Expand Down