Skip to content

Commit

Permalink
up: update some flag logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Feb 15, 2021
1 parent b29df83 commit 1cb7cce
Show file tree
Hide file tree
Showing 15 changed files with 631 additions and 77 deletions.
Empty file added src/Anotation/.keep
Empty file.
20 changes: 20 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ public function setParent(Command $parent): void
$this->parent = $parent;
}

/**
* @return $this
*/
public function getRootCommand(): Command
{
if ($this->parent) {
return $this->parent->getRootCommand();
}

return $this;
}

/**
* @return Command|null
*/
public function getParent(): ?Command
{
return $this->parent;
}

/**
* Show help information
*
Expand Down
15 changes: 15 additions & 0 deletions src/Contract/InputFlagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@
*/
interface InputFlagInterface
{
// fixed args and opts for a command/controller-command
public const ARG_REQUIRED = 1;

public const ARG_OPTIONAL = 2;

public const ARG_IS_ARRAY = 4;

public const OPT_BOOLEAN = 1; // eq symfony InputOption::VALUE_NONE

public const OPT_REQUIRED = 2;

public const OPT_OPTIONAL = 4;

public const OPT_IS_ARRAY = 8;

/**
* @param int $mode
*
Expand Down
8 changes: 4 additions & 4 deletions src/Flag/InputArgument.php → src/Flag/Argument.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*
* @package Inhere\Console\IO\Input
*/
class InputArgument extends InputFlag
class Argument extends Flag
{
/**
* The argument position
Expand All @@ -28,23 +28,23 @@ class InputArgument extends InputFlag
*/
public function isArray(): bool
{
return $this->hasMode(Input::ARG_IS_ARRAY);
return $this->hasMode(Flag::ARG_IS_ARRAY);
}

/**
* @return bool
*/
public function isOptional(): bool
{
return $this->hasMode(Input::ARG_OPTIONAL);
return $this->hasMode(Flag::ARG_OPTIONAL);
}

/**
* @return bool
*/
public function isRequired(): bool
{
return $this->hasMode(Input::ARG_REQUIRED);
return $this->hasMode(Flag::ARG_REQUIRED);
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Flag/InputArguments.php → src/Flag/CmdArgumentsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@
*
* @package Inhere\Console\IO\Input
*/
class InputArguments
trait CmdArgumentsTrait
{
/**
* @var array
* @var array [name => index]
*/
private $name2index = [];

/**
* @var Argument[]
*/
private $arguments = [];

/**
* @param Argument $argument
*/
public function addArgument(Argument $argument): void
{
$this->arguments[] = $argument;
}

/**
* @param string $name
* @param int|null $mode
Expand Down
137 changes: 120 additions & 17 deletions src/Flag/InputFlag.php → src/Flag/Flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,57 @@

/**
* Class InputFlag
* - definition a input item(option|argument)
* - definition a input flag item(option|argument)
*
* @package Inhere\Console\IO\Input
*/
abstract class InputFlag implements InputFlagInterface
abstract class Flag implements InputFlagInterface
{
public const TYPE_INT = 'int';

public const TYPE_BOOL = 'bool';

public const TYPE_FLOAT = 'float';

public const TYPE_STRING = 'string';

public const TYPE_ARRAY = 'array';

// extend types

public const TYPE_INTS = 'int[]';

public const TYPE_STRINGS = 'string[]';

public const TYPE_MIXED = 'mixed';

public const TYPE_CUSTOM = 'custom';

public const TYPE_UNKNOWN = '';

/**
* @var string
*/
private $name;

/**
* The flag description
*
* @var string
*/
private $description;
private $desc;

/**
* @var int
*/
private $mode;

/**
* The argument data type. (eg: 'int', 'bool', 'string', 'array', 'mixed')
* The flag data type. (eg: 'int', 'bool', 'string', 'array', 'mixed')
*
* @var string
*/
private $type = '';
private $type = self::TYPE_UNKNOWN;

/**
* The default value
Expand All @@ -48,6 +72,21 @@ abstract class InputFlag implements InputFlagInterface
*/
private $default;

/**
* The flag value
*
* @var mixed
*/
private $value;

/**
* The flag value validator
* - if validate fail, please throw FlagException
*
* @var callable
*/
private $validator;

/**
* @param string $name
* @param int $mode see Input::ARG_* or Input::OPT_*
Expand All @@ -56,28 +95,35 @@ abstract class InputFlag implements InputFlagInterface
*
* @return static
*/
public static function make(string $name, int $mode = 0, string $description = '', $default = null)
public static function new(string $name, string $description = '', int $mode = 0, $default = null)
{
return new static($name, $mode, $description, $default);
}

/**
* Class constructor.
*
* @param string $name
* @param string $name The flag name
* @param int $mode see Input::ARG_* or Input::OPT_*
* @param string $description
* @param string $desc
* @param mixed $default The default value
* - for Input::ARG_OPTIONAL mode only
* - must be null for InputOption::OPT_BOOL
*/
public function __construct(string $name, int $mode = 0, string $description = '', $default = null)
public function __construct(string $name, int $mode = 0, string $desc = '', $default = null)
{
$this->name = $name;
$this->mode = $mode;

$this->default = $default;
$this->setDescription($description);
$this->setDesc($desc);
}

public function init(): void
{
if ($this->isArray()) {
$this->type = self::TYPE_ARRAY;
}
}

/******************************************************************
Expand All @@ -94,7 +140,6 @@ public function hasMode(int $mode): bool
return ($this->mode & $mode) > 0;
}


/******************************************************************
*
*****************************************************************/
Expand Down Expand Up @@ -166,17 +211,17 @@ public function setDefault($default): void
/**
* @return string
*/
public function getDescription(): string
public function getDesc(): string
{
return $this->description;
return $this->desc;
}

/**
* @param string $description
* @param string $desc
*/
public function setDescription(string $description): void
public function setDesc(string $desc): void
{
$this->description = $description;
$this->desc = $desc;
}

/**
Expand All @@ -192,7 +237,65 @@ public function toArray(): array
'isArray' => $this->isArray(),
'isOptional' => $this->isOptional(),
'isRequired' => $this->isRequired(),
'description' => $this->description,
'description' => $this->desc,
];
}

/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}

/**
* @param mixed $value
*/
public function setValue($value): void
{
// filter value by type
switch ($this->type) {
case self::TYPE_INT:
$value = (int)$value;
break;
case self::TYPE_BOOL:
$value = (bool)$value;
break;
case self::TYPE_FLOAT:
$value = (float)$value;
break;
case self::TYPE_STRING:
$value = (string)$value;
break;
// case self::TYPE_ARRAY:
// $value = (string)$value;
// break;
default:
// nothing
break;
}

// has validator
if ($cb = $this->validator) {
$value = $cb($value);
// if (false === $ok) {
// throw new FlagException('');
// }
}

if ($this->isArray()) {
$this->value[] = $value;
} else {
$this->value = $value;
}
}

/**
* @param callable $validator
*/
public function setValidator(callable $validator): void
{
$this->validator = $validator;
}
}
4 changes: 2 additions & 2 deletions src/Flag/Parser.php → src/Flag/FlagException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Inhere\Console\Flag;

/**
* Class Parser
* Class FlagException
*
* @package Inhere\Console\Flag
*/
class Parser
class FlagException extends \RuntimeException
{

}
Loading

0 comments on commit 1cb7cce

Please sign in to comment.