Skip to content

Commit

Permalink
up: update some console logic, will strict opts and args type
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 15, 2019
1 parent 0ccc4f0 commit 7dc0d58
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 82 deletions.
5 changes: 3 additions & 2 deletions src/console/src/Annotation/Parser/CommandArgumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Swoft\Annotation\Exception\AnnotationException;
use Swoft\Console\Annotation\Mapping\CommandArgument;
use Swoft\Console\CommandRegister;
use Swoft\Console\FlagType;
use Toolkit\Cli\Flags;

/**
Expand Down Expand Up @@ -44,8 +45,8 @@ public function parse(int $type, $annotation): array
'name' => $annotation->getName(),
'desc' => $annotation->getDesc(),
'mode' => $annotation->getMode(),
'type' => $annotation->getType(),
'default' => $valType === 'BOOL' ? Flags::filterBool($defVal) : $defVal,
'type' => $valType,
'default' => $valType === FlagType::BOOL ? Flags::filterBool($defVal) : $defVal,
]);

return [];
Expand Down
5 changes: 3 additions & 2 deletions src/console/src/Annotation/Parser/CommandOptionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Swoft\Annotation\Exception\AnnotationException;
use Swoft\Console\Annotation\Mapping\CommandOption;
use Swoft\Console\CommandRegister;
use Swoft\Console\FlagType;
use Toolkit\Cli\Flags;

/**
Expand Down Expand Up @@ -50,8 +51,8 @@ public function parse(int $type, $option): array
'short' => $option->getShort(),
'desc' => $option->getDesc(),
'mode' => $option->getMode(),
'type' => $option->getType(),
'default' => $valType === 'BOOL' ? Flags::filterBool($defVal) : $defVal,
'type' => $valType,
'default' => $valType === FlagType::BOOL ? Flags::filterBool($defVal) : $defVal,
]);

return [];
Expand Down
6 changes: 3 additions & 3 deletions src/console/src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class Application implements ConsoleInterface
'--debug' => 'Setting the application runtime debug level(0 - 4)',
// '--profile' => 'Display timing and memory usage information',
'--no-color' => 'Disable color/ANSI for message output',
'-h, --help' => 'Display this help message',
'-V, --version' => 'Show application version information',
'-h, --help' => 'Display help message for application or command',
'-V, --version' => 'Display application version information',
];

/**
Expand Down Expand Up @@ -194,7 +194,7 @@ protected function doRun(string $inputCmd): void
}

// Parse default options and arguments
$this->input->bindingFlags($info);
$this->input->parseFlags($info, true);
$this->input->setCommandId($info['cmdId']);

Swoft::triggerByArray(ConsoleEvent::DISPATCH_BEFORE, $this, $info);
Expand Down
8 changes: 4 additions & 4 deletions src/console/src/CommandRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ public static function bindArgument(string $class, string $method, string $argNa
* @param string $class
* @param string $method
* @param string $optName
* @param array $info
* @param array $optInfo
*/
public static function bindOption(string $class, string $method, string $optName, array $info): void
public static function bindOption(string $class, string $method, string $optName, array $optInfo): void
{
// if not 'commands', is bind group options.
if (!isset(self::$commands[$class]['commands'])) {
self::$commands[$class]['options'][$optName] = $info;
self::$commands[$class]['options'][$optName] = $optInfo;
return;
}

$cmdInfo = self::$commands[$class]['commands'][$method];
// add option info
$cmdInfo['options'][$optName] = $info;
$cmdInfo['options'][$optName] = $optInfo;
// re-setting
self::$commands[$class]['commands'][$method] = $cmdInfo;
}
Expand Down
53 changes: 25 additions & 28 deletions src/console/src/Concern/RenderHelpInfoTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use Swoft;
use Swoft\Console\Console;
use Swoft\Console\ConsoleEvent;
use Swoft\Console\FlagType;
use Swoft\Console\Helper\FormatUtil;
use Swoft\Console\Helper\Show;
use Swoft\Console\Output\Output;
use Swoft\Console\Router\Router;
use Swoft\Stdlib\Helper\Arr;
use Swoft\Stdlib\Helper\JsonHelper;
use Swoft\Stdlib\Helper\Str;
use function array_shift;
use function explode;
use function implode;
use function is_array;
use function is_bool;
use function is_scalar;
Expand All @@ -23,13 +24,15 @@
use function sprintf;
use function strlen;
use function strpos;
use function strtoupper;
use function trim;
use const PHP_EOL;
use const PHP_VERSION;
use const SWOOLE_VERSION;

/**
* Trait RenderHelpInfoTrait
*
* @package Swoft\Console\Concern
*/
trait RenderHelpInfoTrait
Expand All @@ -56,10 +59,8 @@ protected function showVersionInfo(): void
$this->renderBannerLogo();

// Display some information
$output->writef(
'PHP: <info>%s</info>, Swoft: <info>%s</info>, Swoole: <info>%s</info>',
$phpVersion, $swoftVersion, $swooleVersion
);
$output->writef('PHP: <info>%s</info>, Swoft: <info>%s</info>, Swoole: <info>%s</info>', $phpVersion,
$swoftVersion, $swooleVersion);
}

protected function renderBannerLogo(): void
Expand Down Expand Up @@ -111,21 +112,15 @@ protected function showApplicationHelp(bool $showLogo = true): void
Console::writeln('<comment>Available Commands:</comment>');

$grpHandler = function (string $group, array $info) use ($keyWidth) {
Console::writef(
' <info>%s</info>%s%s',
Str::padRight($group, $keyWidth),
Console::writef(' <info>%s</info>%s%s', Str::padRight($group, $keyWidth),
$info['desc'] ?: 'No description message',
$info['alias'] ? "(alias: <info>{$info['alias']}</info>)" : ''
);
$info['alias'] ? "(alias: <info>{$info['alias']}</info>)" : '');
};

$cmdHandler = function (string $cmdId, array $info) use ($keyWidth) {
Console::writef(
' <info>%s</info> %s%s',
Str::padRight($cmdId, $keyWidth),
Console::writef(' <info>%s</info> %s%s', Str::padRight($cmdId, $keyWidth),
$info['desc'] ?: 'No description message',
$info['alias'] ? "(alias: <info>{$info['alias']}</info>)" : ''
);
$info['alias'] ? "(alias: <info>{$info['alias']}</info>)" : '');
};

$router->sortedEach($grpHandler, $expand ? $cmdHandler : null);
Expand All @@ -138,7 +133,7 @@ protected function showApplicationHelp(bool $showLogo = true): void
* Display help, command list of the group
*
* @param string $group Group name
* @param array $info Some base info of the group
* @param array $info Some base info of the group
*/
protected function showGroupHelp(string $group, array $info = []): void
{
Expand Down Expand Up @@ -174,12 +169,9 @@ protected function showGroupHelp(string $group, array $info = []): void
foreach ($names as $name) {
$cmdId = $router->buildCommandID($group, $name);
$cInfo = $router->getRouteByID($cmdId);
Console::writef(
' <info>%s</info> %s%s',
Str::padRight($name, $keyWidth),
Console::writef(' <info>%s</info> %s%s', Str::padRight($name, $keyWidth),
$cInfo['desc'] ?: 'No description message',
$cInfo['alias'] ? "(alias: <info>{$cInfo['alias']}</info>)" : ''
);
$cInfo['alias'] ? "(alias: <info>{$cInfo['alias']}</info>)" : '');
}

if ($info['example']) {
Expand Down Expand Up @@ -229,7 +221,7 @@ protected function showCommandHelp(array $info): void
foreach ($arguments as $name => $meta) {
Console::writef(
' <info>%s</info> %s %s%s',
Str::padRight($name, $keyWidth), $meta['type'],
Str::padRight($name, $keyWidth), strtoupper($meta['type']),
$meta['desc'],
$this->renderDefaultValue($meta['default'])
);
Expand Down Expand Up @@ -268,7 +260,11 @@ protected function renderCommandOptions(array $options): void
continue;
}

$typeName = $meta['type'] === 'BOOL' ? '' : $meta['type'];
$typeName = $meta['type'] === FlagType::BOOL ? '' : $meta['type'];
if ($typeName) {
$typeName = strtoupper($typeName);
}

if ($len === 1) {
$key = sprintf('-<info>%s</info> %s', $name, $typeName);
} else {
Expand All @@ -281,9 +277,9 @@ protected function renderCommandOptions(array $options): void
$key = sprintf('<info>%s--%s</info> %s', $shortMark, $name, $typeName);
}

$kenLen = strlen($key);
if ($kenLen > $maxLen) {
$maxLen = $kenLen;
$keyLen = strlen($key);
if ($keyLen > $maxLen) {
$maxLen = $keyLen;
}

$newOpts[$key] = $meta;
Expand Down Expand Up @@ -323,6 +319,7 @@ protected function renderCommandOptions(array $options): void

/**
* @param mixed $defVal
*
* @return string
*/
private function renderDefaultValue($defVal): string
Expand All @@ -336,9 +333,9 @@ private function renderDefaultValue($defVal): string
if (is_bool($defVal)) {
$text .= $defVal ? 'True' : 'False';
} elseif (is_scalar($defVal)) {
$text .= $defVal;
$text .= $defVal;
} elseif (is_array($defVal)) {
$text .= implode(', ', $defVal);
$text .= JsonHelper::encode($defVal);
} else {
$text .= $defVal;
}
Expand Down
1 change: 0 additions & 1 deletion src/console/src/FlagType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@
*/
class FlagType extends PhpType
{

}
10 changes: 6 additions & 4 deletions src/console/src/Input/AbstractFlag.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Swoft\Console\Input;

use Swoft\Console\Annotation\Mapping\Command;
use function strtoupper;
use function strtolower;
use function trim;
use function ucfirst;

Expand Down Expand Up @@ -66,6 +66,7 @@ abstract class AbstractFlag
private $required = false;

/**
* TODO ...
* @var mixed
*/
private $_value;
Expand Down Expand Up @@ -128,13 +129,14 @@ public function getMode(): int
}

/**
* @param bool $upper
* @param bool $lower
*
* @return string
*/
public function getType(bool $upper = true): string
public function getType(bool $lower = true): string
{
if ($this->type) {
return $upper ? strtoupper($this->type) : $this->type;
return $lower ? strtolower($this->type) : $this->type;
}

return '';
Expand Down
23 changes: 23 additions & 0 deletions src/console/src/Input/AbstractInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ abstract class AbstractInput implements InputInterface
*/
protected $tokens;

/**
* Input flags data
*
* @var array
*/
protected $flags = [];

/**
* Input args data
*
Expand Down Expand Up @@ -749,4 +756,20 @@ public function setTokens(array $tokens): void
{
$this->tokens = $tokens;
}

/**
* @return array
*/
public function getFlags(): array
{
return $this->flags;
}

/**
* @param array $flags
*/
public function setFlags(array $flags): void
{
$this->flags = $flags;
}
}
Loading

0 comments on commit 7dc0d58

Please sign in to comment.