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
66 changes: 66 additions & 0 deletions src/Helper/Normalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Ahc\Cli\Helper;

use Ahc\Cli\Input\Parameter;

/**
* Internal value &/or argument normalizer. Has little to no usefulness as public api.
*
* @author Jitendra Adhikari <jiten.adhikary@gmail.com>
* @license MIT
*
* @link https://github.com/adhocore/cli
*/
class Normalizer
{
/**
* Normalize argv args. Like splitting `-abc` and `--xyz=...`.
*
* @param array $args
*
* @return array
*/
public function normalizeArgs(array $args): array
{
$normalized = [];

foreach ($args as $arg) {
if (\preg_match('/^\-\w{2,}/', $arg)) {
$splitArg = \implode(' -', \str_split(\ltrim($arg, '-')));
$normalized = \array_merge($normalized, \explode(' ', '-' . $splitArg));
} elseif (\preg_match('/^\-\-\w{2,}\=/', $arg)) {
$normalized = \array_merge($normalized, explode('=', $arg));
} else {
$normalized[] = $arg;
}
}

return $normalized;
}

/**
* Normalizes value as per context and runs thorugh filter if possible.
*
* @param Parameter $parameter
* @param string|null $value
*
* @return mixed
*/
public function normalizeValue(Parameter $parameter, string $value = null)
{
if (\is_bool($default = $parameter->default())) {
return !$default;
}

if ($parameter->variadic()) {
return (array) $value;
}

if (null === $value) {
return $parameter->required() ? null : true;
}

return $parameter->filter($value);
}
}
63 changes: 10 additions & 53 deletions src/Input/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Ahc\Cli\Input;

use Ahc\Cli\Helper\Normalizer;

/**
* Argv parser for the cli.
*
Expand All @@ -15,6 +17,9 @@ abstract class Parser
/** @var string|null The last seen variadic option name */
protected $_lastVariadic;

/** @var Normalizer */
protected $_normalizer;

/** @var Option[] Registered options */
private $_options = [];

Expand All @@ -35,9 +40,11 @@ abstract class Parser
*/
public function parse(array $argv): self
{
$this->_normalizer = new Normalizer;

\array_shift($argv);

$argv = $this->normalize($argv);
$argv = $this->_normalizer->normalizeArgs($argv);
$count = \count($argv);
$literal = false;

Expand All @@ -58,37 +65,12 @@ public function parse(array $argv): self
return $this;
}

/**
* Normalize argv args. Like splitting `-abc` and `--xyz=...`.
*
* @param array $args
*
* @return array
*/
protected function normalize(array $args): array
{
$normalized = [];

foreach ($args as $arg) {
if (\preg_match('/^\-\w{2,}/', $arg)) {
$splitArg = \implode(' -', \str_split(\ltrim($arg, '-')));
$normalized = \array_merge($normalized, \explode(' ', '-' . $splitArg));
} elseif (\preg_match('/^\-\-\w{2,}\=/', $arg)) {
$normalized = \array_merge($normalized, explode('=', $arg));
} else {
$normalized[] = $arg;
}
}

return $normalized;
}

/**
* Parse single arg.
*
* @param string $arg
*
* @return void
* @return mixed
*/
protected function parseArgs(string $arg)
{
Expand Down Expand Up @@ -183,36 +165,11 @@ abstract protected function emit(string $event, $value = null);
protected function setValue(Parameter $parameter, string $value = null): bool
{
$name = $parameter->attributeName();
$value = $this->prepareValue($parameter, $value);
$value = $this->_normalizer->normalizeValue($parameter, $value);

return $this->set($name, $value);
}

/**
* Prepares value as per context and runs thorugh filter if possible.
*
* @param Parameter $parameter
* @param string|null $value
*
* @return mixed
*/
protected function prepareValue(Parameter $parameter, string $value = null)
{
if (\is_bool($default = $parameter->default())) {
return !$default;
}

if ($parameter->variadic()) {
return (array) $value;
}

if (null === $value) {
return $parameter->required() ? null : true;
}

return $parameter->filter($value);
}

/**
* Set a raw value.
*
Expand Down