From 8377f726f1ee4d12611cd60cc5b2682cc9a852a2 Mon Sep 17 00:00:00 2001 From: inhere Date: Tue, 21 Sep 2021 15:13:24 +0800 Subject: [PATCH] convert all flags collect to new parser --- app/Common/IdeaHttp/ClientEnvReader.php | 5 + app/Common/IdeaHttp/Request.php | 4 +- app/Console/Command/Json5Command.php | 7 +- app/Console/Controller/ConvertController.php | 1 - app/Console/Controller/CronTabController.php | 18 +--- app/Console/Controller/DbController.php | 6 +- app/Console/Controller/FileController.php | 6 +- app/Console/Controller/GitController.php | 64 +++++------- app/Console/Controller/GitHubController.php | 101 ++++++------------- app/Console/Controller/JumpController.php | 77 +++++++------- app/Console/Controller/K8sController.php | 3 +- app/Console/Controller/PluginController.php | 36 +++---- app/Console/Controller/ProjectController.php | 6 +- app/Console/Controller/SelfController.php | 58 +++++------ app/Console/Controller/UtilController.php | 37 ++++--- 15 files changed, 192 insertions(+), 237 deletions(-) diff --git a/app/Common/IdeaHttp/ClientEnvReader.php b/app/Common/IdeaHttp/ClientEnvReader.php index 5f06f37..404d883 100644 --- a/app/Common/IdeaHttp/ClientEnvReader.php +++ b/app/Common/IdeaHttp/ClientEnvReader.php @@ -23,6 +23,11 @@ class ClientEnvReader */ private $envs = []; + /** + * @var string + */ + private $curEnv = ''; + /** * @param string $envFile * diff --git a/app/Common/IdeaHttp/Request.php b/app/Common/IdeaHttp/Request.php index b67e8b3..5ef97f1 100644 --- a/app/Common/IdeaHttp/Request.php +++ b/app/Common/IdeaHttp/Request.php @@ -103,7 +103,7 @@ public static function new(array $data = []): self /** * @param string $str * - * @return static + * @return static|null */ public static function fromHTTPString(string $str): ?self { @@ -399,7 +399,7 @@ public function parseHeaderRaw(string $headerRaw): array $nodes = Str::explode($line, ':', 2); $name = strtolower($nodes[0]); $value = $nodes[1] ?? ''; - if ($value && strpos(';', $value) !== false) { + if ($value && strpos($value, ';') !== false) { $headerMap[$name] = Str::explode($value, ';'); } else { $headerMap[$name] = [$value]; diff --git a/app/Console/Command/Json5Command.php b/app/Console/Command/Json5Command.php index 604e5b4..07c9b69 100644 --- a/app/Console/Command/Json5Command.php +++ b/app/Console/Command/Json5Command.php @@ -35,17 +35,12 @@ public static function aliases(): array return ['j5']; } - protected function configure(): void - { - $this->input->bindArgument('json5file', 0); - } - /** * @options * -o, --output Output the decoded contents to the file * * @arguments - * json5file The script name for execute + * json5file string;The json5 file name for parse;required * * @param Input $input * @param Output $output diff --git a/app/Console/Controller/ConvertController.php b/app/Console/Controller/ConvertController.php index 4ef2db6..230d391 100644 --- a/app/Console/Controller/ConvertController.php +++ b/app/Console/Controller/ConvertController.php @@ -153,5 +153,4 @@ public function ts2dateCommand(FlagsParser $fs, Output $output): void // opts $output->table($data, 'Time to date', []); } - } diff --git a/app/Console/Controller/CronTabController.php b/app/Console/Controller/CronTabController.php index db4c3b2..10a8315 100644 --- a/app/Console/Controller/CronTabController.php +++ b/app/Console/Controller/CronTabController.php @@ -52,15 +52,15 @@ public function parseConfigure(Input $input): void * @options * -i bool;Run an interactive environment * + * @param FlagsParser $fs + * @param Output $output + * + * @throws Exception * @example * {binWithCmd} 'every 6 mins' // '*\/6 * * * *' * {binWithCmd} 'every day 10 am' // '0 10 * * *' * {binWithCmd} 'every day 10:20 am' // '20 10 * * *' * - * @param Input $input - * @param Output $output - * - * @throws Exception */ public function parseCommand(FlagsParser $fs, Output $output): void { @@ -74,14 +74,6 @@ public function parseCommand(FlagsParser $fs, Output $output): void $output->aList($dateList, "Execution time for the next $nextNum times:"); } - /** - * @param Input $input - */ - public function execTimeConfigure(Input $input): void - { - $input->bindArgument('expression', 0); - } - /** * show next execution datetime for an cron expression. * @@ -92,7 +84,7 @@ public function execTimeConfigure(Input $input): void * -n, --next int;Show next number exec datetime. default number is 3. * -p, --prev bool;Show previsions exec datetime * - * @param Input $input + * @param FlagsParser $fs * @param Output $output * * @throws Exception diff --git a/app/Console/Controller/DbController.php b/app/Console/Controller/DbController.php index 328a058..8898193 100644 --- a/app/Console/Controller/DbController.php +++ b/app/Console/Controller/DbController.php @@ -35,9 +35,8 @@ class DbController extends Controller * -s, --source The source sql file * -o, --output The output content file * - * @param Input $input + * @param FlagsParser $fs * @param Output $output - * */ public function sql2mdCommand(FlagsParser $fs, Output $output): void { @@ -51,9 +50,8 @@ public function sql2mdCommand(FlagsParser $fs, Output $output): void * -s, --source The source markdown file * -o, --output The output sql file * - * @param Input $input + * @param FlagsParser $fs * @param Output $output - * */ public function md2sqlCommand(FlagsParser $fs, Output $output): void { diff --git a/app/Console/Controller/FileController.php b/app/Console/Controller/FileController.php index e673e89..d7a5f64 100644 --- a/app/Console/Controller/FileController.php +++ b/app/Console/Controller/FileController.php @@ -55,7 +55,7 @@ protected static function commandAliases(): array * @arguments * path The ls path * - * @param Input $input + * @param FlagsParser $fs */ public function listCommand(FlagsParser $fs): void { @@ -104,7 +104,7 @@ public function lnCommand(Output $output): void * @arguments * file The file path * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ public function vimCommand(FlagsParser $fs, Output $output): void @@ -157,7 +157,7 @@ public function renameCommand(FlagsParser $fs, Output $output): void * --pt The progress bar type. allow: txt,bar * -s, --save The save local file for downloaded. * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ public function downCommand(FlagsParser $fs, Output $output): void diff --git a/app/Console/Controller/GitController.php b/app/Console/Controller/GitController.php index bceadae..acb644e 100644 --- a/app/Console/Controller/GitController.php +++ b/app/Console/Controller/GitController.php @@ -30,8 +30,6 @@ use Toolkit\Stdlib\Obj\ConfigObject; use Toolkit\Stdlib\Str; use function array_keys; -use function array_values; -use function count; use function implode; use function sprintf; use function strlen; @@ -110,7 +108,7 @@ protected function options(): array protected function beforeRun(): void { if ($this->app && !$this->settings) { - $this->settings = ConfigObject::new($this->app->getParam('git', [])); + $this->settings = ConfigObject::new($this->app->getArrayParam('git')); } } @@ -227,7 +225,7 @@ public function statusCommand(Output $output): void * display git information for the project * * @options - * --show-commands bool;Show exec git commands + * --show-commands bool;Show exec git commands * * @param FlagsParser $fs * @param Output $output @@ -242,46 +240,38 @@ public function infoCommand(FlagsParser $fs, Output $output): void ]); } - /** - * @param Input $input - */ - protected function branchConfigure(Input $input): void - { - // $input->bindArguments(['keyword' => 0]); - } - /** * list branch by git branch * * @options - * -a, --all Display all branches - * -r, --remote Display given remote branches - * --only-name Only display branch name - * --inline Only display branch name and print inline - * -s, --search The keyword name for search branches + * -a, --all bool;Display all branches + * -r, --remote Display branches for the given remote + * --only-name bool;Only display branch name + * --inline bool;Only display branch name and print inline + * -s, --search The keyword name for search branches * * @arguments * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function branchCommand(Input $input, Output $output): void + public function branchCommand(FlagsParser $fs, Output $output): void { $opts = []; $repo = Repo::new(); $remote = ''; - $inline = $input->getBoolOpt('inline'); - if ($input->getSameBoolOpt('a, all')) { + $inline = $fs->getOpt('inline'); + if ($fs->getOpt('all')) { $opts['all'] = true; - } elseif ($remote = $input->getSameStringOpt('r,remote')) { + } elseif ($remote = $fs->getOpt('remote')) { $opts['remotes'] = true; } $list = $repo->getGit()->branch->getList($opts); - $onlyName = $input->getBoolOpt('only-name'); - $keyword = $input->getSameStringOpt('s,search'); + $onlyName = $fs->getOpt('only-name'); + $keyword = $fs->getOpt('search'); $msg = 'Branch List'; if (strlen($remote) > 1) { @@ -388,7 +378,7 @@ public function openCommand(FlagsParser $fs, Output $output): void * Clone an remote git repository to local * * @options - * --gh Define the remote repository is on github + * --gh bool;Define the remote repository is on github * * @arguments * repo The remote git repo URL or repository name @@ -411,9 +401,9 @@ public function cloneCommand(FlagsParser $fs, Output $output): void * get the latest/next git tag from the project directory * * @options - * -d, --dir The project directory path. default is current directory. - * --next-tag bool;Display the project next tag version. eg: v2.0.2 => v2.0.3 - * --only-tag bool;Only output tag information + * -d, --dir The project directory path. default is current directory. + * --next-tag bool;Display the project next tag version. eg: v2.0.2 => v2.0.3 + * --only-tag bool;Only output tag information * * @param FlagsParser $fs * @param Input $input @@ -580,7 +570,7 @@ public function tagNewCommand(FlagsParser $fs, Input $input, Output $output): vo * -v, --tag The tag version. eg: v2.0.3 * --no-remote bool;Only delete local tag * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ public function tagDeleteCommand(FlagsParser $fs, Output $output): void @@ -761,11 +751,11 @@ public function logCommand(FlagsParser $fs, Output $output): void * collect git change log information by `git log` * * @arguments - * oldVersion string;The old version. eg: v1.0.2 - * - keywords `last/latest` will auto use latest tag. - * - keywords `prev/previous` will auto use previous tag.;required - * newVersion string;The new version. eg: v1.2.3 - * - keywords `head` will use `Head` commit.;required + * oldVersion string;The old version. eg: v1.0.2 + * - keywords `last/latest` will auto use latest tag. + * - keywords `prev/previous` will auto use previous tag.;required + * newVersion string;The new version. eg: v1.2.3 + * - keywords `head` will use `Head` commit.;required * * @options * --exclude Exclude contains given sub-string. multi by comma split. @@ -791,9 +781,9 @@ public function logCommand(FlagsParser $fs, Output $output): void * @param Output $output * * @example - * {binWithCmd} last head - * {binWithCmd} last head --style gh-release --no-merges - * {binWithCmd} v2.0.9 v2.0.10 --no-merges --style gh-release --exclude "cs-fixer,format codes" + * {binWithCmd} last head + * {binWithCmd} last head --style gh-release --no-merges + * {binWithCmd} v2.0.9 v2.0.10 --no-merges --style gh-release --exclude "cs-fixer,format codes" */ public function changelogCommand(FlagsParser $fs, Output $output): void { diff --git a/app/Console/Controller/GitHubController.php b/app/Console/Controller/GitHubController.php index d05cea2..c12e957 100644 --- a/app/Console/Controller/GitHubController.php +++ b/app/Console/Controller/GitHubController.php @@ -11,6 +11,7 @@ use Inhere\Console\Console; use Inhere\Console\Controller; +use Inhere\Console\Exception\PromptException; use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; use Inhere\Kite\Common\CmdRunner; @@ -18,7 +19,9 @@ use Inhere\Kite\Helper\AppHelper; use PhpComp\Http\Client\Client; use Throwable; +use Toolkit\PFlag\FlagsParser; use function in_array; +use function strtoupper; /** * Class GitHubGroup @@ -99,25 +102,6 @@ protected function onNotFound(string $action, array $args): bool return false; } - protected function configure(): void - { - parent::configure(); - - // binding arguments - switch ($this->getAction()) { - case 'open': - $this->input->bindArgument('remote', 0); - break; - case 'project': - $this->input->bindArgument('name', 0); - break; - case 'pullRequest': - // Configure for the `pullRequestCommand` - $this->input->bindArgument('project', 0); - break; - } - } - // protected function beforeAction(): bool // { // if ($this->app) { @@ -195,48 +179,37 @@ public function releaseCommand(Input $input, Output $output): void } /** - * @param Input $input * @param Output $output */ - public function workflowCommand(Input $input, Output $output): void + public function workflowCommand(Output $output): void { $output->success('Complete'); } - /** - * @param Input $input - */ - protected function openConfigure(Input $input): void - { - $input->bindArguments([ - 'repoPath' => 0, - ]); - } - /** * Open an github repository by browser * * @options * -r, --remote The git remote name. default is `origin` - * --main Use the config `mainRemote` name + * --main bool;Use the config `mainRemote` name * * @arguments * repoPath The remote git repo URL or repository group/name. * If not input, will auto parse from current work directory * - * @param Input $input + * @param FlagsParser $fs * @param Output $output * * @example * {fullCmd} php-toolkit/cli-utils * {fullCmd} https://github.com/php-toolkit/cli-utils */ - public function openCommand(Input $input, Output $output): void + public function openCommand(FlagsParser $fs, Output $output): void { $gh = $this->getGithub(); // - input repoPath - $repoPath = $input->getStringArg('repoPath'); + $repoPath = $fs->getArg('repoPath'); if ($repoPath) { $repoUrl = $gh->parseRepoUrl($repoPath); if (!$repoUrl) { @@ -250,8 +223,8 @@ public function openCommand(Input $input, Output $output): void } // - auto parse - $remote = $input->getSameStringOpt(['r', 'remote'], 'origin'); - if ($input->getBoolOpt('main')) { + $remote = $fs->getOpt('remote', 'origin'); + if ($fs->getOpt('main')) { $remote = $gh->getMainRemote(); } @@ -261,17 +234,6 @@ public function openCommand(Input $input, Output $output): void $output->success('Complete'); } - /** - * @param Input $input - */ - protected function cloneConfigure(Input $input): void - { - $input->bindArguments([ - 'repo' => 0, - 'name' => 1, - ]); - } - /** * Clone an github repository to local * @@ -279,10 +241,10 @@ protected function cloneConfigure(Input $input): void * -w, --workdir The clone work dir, defualt is current dir. * * @arguments - * repo The remote git repo URL or repository name + * repo string;The remote git repo URL or repository name;required * name The repository name at local, default is same `repo` * - * @param Input $input + * @param FlagsParser $fs * @param Output $output * * @example @@ -290,14 +252,14 @@ protected function cloneConfigure(Input $input): void * {binWithCmd} php-toolkit/cli-utils my-repo * {binWithCmd} https://github.com/php-toolkit/cli-utils */ - public function cloneCommand(Input $input, Output $output): void + public function cloneCommand(FlagsParser $fs, Output $output): void { - $repo = $input->getRequiredArg('repo'); - $name = $input->getStringArg('name'); + $repo = $fs->getArg('repo'); + $name = $fs->getArg('name'); $gh = $this->getGithub(); - $workDir = $input->getSameStringOpt('w,workdir'); + $workDir = $fs->getOpt('workdir'); $repoUrl = $gh->parseRepoUrl($repo); if (!$repoUrl) { $output->error("invalid github 'repo' address: $repo"); @@ -316,46 +278,44 @@ public function cloneCommand(Input $input, Output $output): void } $run->run(true); - $output->success('Complete'); } /** * generate an PR link for given project information * + * @arguments + * project The project name. + * * @options * -s, --source The source branch name. will auto prepend branchPrefix * --full-source The full source branch name * -t, --target The target branch name * -o, --open Open the generated PR link on browser - * -d, --direct The PR is direct from fork to main repository + * -d, --direct bool;The PR is direct from fork to main repository * --new Open new PR page on browser * - * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function pullRequestCommand(Input $input, Output $output): void + public function pullRequestCommand(FlagsParser $fs, Output $output): void { $gh = $this->getGithub(); if (!$pjName = $gh->findProjectName()) { - $pjName = $input->getRequiredArg('project'); + $pjName = $fs->getArg('project'); + if (!$pjName) { + throw new PromptException('project is required'); + } } - // if (!$gh->hasProject($pjName)) { - // throw new PromptException("project '{$pjName}' is not found in the config"); - // } - $gh->loadProjectInfo($pjName); $p = $gh->getCurProject(); - $open = $input->getSameOpt(['o', 'open']); - $output->info('auto fetch current branch name'); $curBranch = $gh->getCurBranch(); - $srcBranch = $input->getSameStringOpt(['s', 'source']); - $tgtBranch = $input->getSameStringOpt(['t', 'target']); + $srcBranch = $fs->getOpt('source'); + $tgtBranch = $fs->getOpt('target'); $brPrefix = $gh->getValue('branchPrefix', ''); if ($srcBranch) { @@ -364,6 +324,11 @@ public function pullRequestCommand(Input $input, Output $output): void $srcBranch = $curBranch; } + $open = $fs->getOpt('open'); + if ($open && strtoupper($open) === 'HEAD') { + $open = $curBranch; + } + if ($tgtBranch) { $tgtBranch = $brPrefix . $tgtBranch; } elseif (is_string($open) && $open) { diff --git a/app/Console/Controller/JumpController.php b/app/Console/Controller/JumpController.php index de3c350..2539c79 100644 --- a/app/Console/Controller/JumpController.php +++ b/app/Console/Controller/JumpController.php @@ -17,6 +17,7 @@ use Inhere\Kite\Lib\Jump\JumpShell; use Inhere\Kite\Lib\Jump\JumpStorage; use Inhere\Kite\Lib\Template\SimpleTemplate; +use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\Str; use Toolkit\Sys\Util\ShellUtil; use function implode; @@ -86,16 +87,16 @@ protected function configure(): void * @arguments * category The data category key name. allow: namedPaths, histories, prevPath * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function listCommand(Input $input, Output $output): void + public function listCommand(FlagsParser $fs, Output $output): void { $qj = Kite::jumper(); $output->colored('Datafile: ' . $qj->getDatafile(), 'cyan'); $output->println(Str::repeat('=', 60)); - $key = $input->getFirstArg(); + $key = $fs->getFirstArg(); $data = $qj->getEngine()->toArray(true); $opts = [ 'ucTitleWords' => false, @@ -121,7 +122,7 @@ public function listCommand(Input $input, Output $output): void * @options * --bind The shell bind func name. default is `jump` * - * @param Input $input + * @param FlagsParser $fs * @param Output $output * * @help @@ -136,9 +137,9 @@ public function listCommand(Input $input, Output $output): void * eval "$(kite jump shell zsh --bind j)" * */ - public function shellCommand(Input $input, Output $output): void + public function shellCommand(FlagsParser $fs, Output $output): void { - $shell = $input->getStringArg('shellName'); + $shell = $fs->getArg('shellName'); if (!$shell) { $shell = ShellUtil::getName(true); } @@ -150,7 +151,7 @@ public function shellCommand(Input $input, Output $output): void $qj = Kite::jumper(); $tpl = new SimpleTemplate(); - $bindFunc = $input->getStringOpt('bind', 'jump'); + $bindFunc = $fs->getOpt('bind', 'jump'); $tplVars = [ 'shell' => $shell, 'bindFunc' => $bindFunc, @@ -171,29 +172,30 @@ public function shellCommand(Input $input, Output $output): void * keywords The jump target directory keywords for match. * * @options - * --flag INT The flag set for match paths. - * 1 Only match name path list - * 2 Only match history path list - * 3 match all directory path list(default) - * --no-name Not output name for named paths, useful for bash env. - * --limit INT Limit the match result rows + * --flag The flag set for match paths. + * Allow: + * 1 Only match name path list + * 2 Only match history path list + * 3 match all directory path list(default) + * --no-name bool;Not output name for named paths, useful for bash env. + * --limit bool;Limit the match result rows * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function hintCommand(Input $input, Output $output): void + public function hintCommand(FlagsParser $fs, Output $output): void { $qj = Kite::jumper(); - $kw = $input->getStringArg('keywords'); + $kw = $fs->getArg('keywords'); - $flag = $input->getIntOpt('flag', JumpStorage::MATCH_BOTH); + $flag = $fs->getOpt('flag', JumpStorage::MATCH_BOTH); $tipsStr = ''; $results = $qj->matchAll($kw, $flag); if ($results) { $tipsArr = []; - $notName = $input->getBoolOpt('no-name'); + $notName = $fs->getOpt('no-name'); foreach ($results as $name => $path) { if (false === $notName && is_string($name)) { @@ -218,17 +220,17 @@ public function hintCommand(Input $input, Output $output): void * Get the real directory path by given name. * * @arguments - * name The jump target directory name or path. + * name The jump target directory name or path. * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function getCommand(Input $input, Output $output): void + public function getCommand(FlagsParser $fs, Output $output): void { $qj = Kite::jumper(); // vdump($input, $_SERVER['argv']); - $name = $input->getStringArg('name'); + $name = $fs->getArg('name'); $dir = $qj->match($name); Kite::logger()->info("jump get directory is: $dir, name: $name"); @@ -240,23 +242,23 @@ public function getCommand(Input $input, Output $output): void * Set the name to real directory path. * * @arguments - * name The name for quick jump. - * path The target directory path. + * name string;The name for quick jump;required; + * path string;The target directory path;required; * * @options - * --override Override exist name. + * --override bool;Override exist name. * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function setCommand(Input $input, Output $output): void + public function setCommand(FlagsParser $fs, Output $output): void { $qj = Kite::jumper(); - $name = (string)$input->getRequiredArg('name'); - $path = (string)$input->getRequiredArg('path'); + $name = $fs->getArg('name'); + $path = $fs->getArg('path'); - $ok = $qj->addNamed($name, $path, $input->getBoolOpt('override')); + $ok = $qj->addNamed($name, $path, $fs->getOpt('override')); if ($ok) { $qj->dump(); $output->success("Set: $name=$path"); @@ -268,17 +270,20 @@ public function setCommand(Input $input, Output $output): void /** * record target directory path, by the jump dir hooks. * + * @arguments + * targetDir The into target dir path + * * @options - * --quiet Quiet, not print workdir. + * --quiet bool;Quiet, not print workdir. * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function chdirCommand(Input $input, Output $output): void + public function chdirCommand(FlagsParser $fs, Output $output): void { - $targetDir = $input->getStringArg(0); + $targetDir = $fs->getArg('targetDir'); if (!$targetDir) { - $targetDir = $input->getWorkDir(); + $targetDir = $this->input->getWorkDir(); } $qj = Kite::jumper(); @@ -292,7 +297,7 @@ public function chdirCommand(Input $input, Output $output): void } if (is_dir($targetDir)) { - $quiet = $input->getBoolOpt('quiet'); + $quiet = $fs->getOpt('quiet'); if (!$quiet) { $output->colored("INTO: $targetDir"); diff --git a/app/Console/Controller/K8sController.php b/app/Console/Controller/K8sController.php index 817dc9d..c7a6f03 100644 --- a/app/Console/Controller/K8sController.php +++ b/app/Console/Controller/K8sController.php @@ -34,11 +34,10 @@ class K8sController extends Controller * -s, --source The source sql file * -o, --output The output content file * - * @param Input $input * @param Output $output * */ - public function genTplCommand(Input $input, Output $output): void + public function genTplCommand(Output $output): void { $output->info('WIP'); } diff --git a/app/Console/Controller/PluginController.php b/app/Console/Controller/PluginController.php index bebdb12..98585c8 100644 --- a/app/Console/Controller/PluginController.php +++ b/app/Console/Controller/PluginController.php @@ -10,9 +10,10 @@ namespace Inhere\Kite\Console\Controller; use Inhere\Console\Controller; -use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; use Inhere\Kite\Kite; +use InvalidArgumentException; +use Toolkit\PFlag\FlagsParser; use function array_keys; /** @@ -36,17 +37,17 @@ public static function aliases(): array * list all plugins dir and file information * * @options - * --only-files Only list all plugin names + * --only-files bool;Only list all plugin names * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function listCommand(Input $input, Output $output): void + public function listCommand(FlagsParser $fs, Output $output): void { $kpm = Kite::plugManager(); $opts = ['ucFirst' => false]; - if (!$input->getBoolOpt('only-files')) { + if (!$fs->getOpt('only-files')) { $dirs = $kpm->getPluginDirs(); $output->aList($dirs, 'plugin dirs', $opts); } @@ -59,17 +60,16 @@ public function listCommand(Input $input, Output $output): void * display information for an plugin * * @arguments - * name The plugin name for display + * name string;The plugin name for display;required * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function infoCommand(Input $input, Output $output): void + public function infoCommand(FlagsParser $fs, Output $output): void { - $input->bindArgument('name', 0); - $name = $input->getRequiredArg('name'); + $kpm = Kite::plugManager(); + $name = $fs->getArg('name'); - $kpm = Kite::plugManager(); if (!$plg= $kpm->getPlugin($name)) { $output->error("the plugin '$name' is not exists"); return; @@ -87,16 +87,16 @@ public function infoCommand(Input $input, Output $output): void * name The plugin name for run * * @options - * -i, --select Run plugin by select + * -i, --select bool;Run plugin by select * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function runCommand(Input $input, Output $output): void + public function runCommand(FlagsParser $fs, Output $output): void { $kpm = Kite::plugManager(); - if ($input->getSameBoolOpt('i,select')) { + if ($fs->getOpt('select')) { $files = $kpm->loadPluginFiles()->getPluginFiles(); $list = array_keys($files); @@ -104,8 +104,10 @@ public function runCommand(Input $input, Output $output): void 'returnVal' => true, ]); } else { - $input->bindArgument('name', 0); - $name = $input->getRequiredArg('name'); + $name = $fs->getArg('name'); + if (!$name) { + throw new InvalidArgumentException('must provide plugin name for run'); + } } $kpm->run($name, $this->app); diff --git a/app/Console/Controller/ProjectController.php b/app/Console/Controller/ProjectController.php index 6ad3535..4c10ccd 100644 --- a/app/Console/Controller/ProjectController.php +++ b/app/Console/Controller/ProjectController.php @@ -3,7 +3,6 @@ namespace Inhere\Kite\Console\Controller; use Inhere\Console\Controller; -use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; /** @@ -34,16 +33,15 @@ protected static function commandAliases(): array ]; } - public function projectCommand(Input $input, Output $output): void + public function projectCommand(Output $output): void { $output->success('Complete'); } /** - * @param Input $input * @param Output $output */ - public function packageCommand(Input $input, Output $output): void + public function packageCommand(Output $output): void { diff --git a/app/Console/Controller/SelfController.php b/app/Console/Controller/SelfController.php index 508fddd..fe5604d 100644 --- a/app/Console/Controller/SelfController.php +++ b/app/Console/Controller/SelfController.php @@ -13,6 +13,7 @@ use Inhere\Kite\Kite; use Throwable; use Toolkit\Cli\Color; +use Toolkit\PFlag\FlagsParser; use Toolkit\Stdlib\OS; use Toolkit\Stdlib\Php; use Toolkit\Stdlib\Util\PhpDotEnv; @@ -106,7 +107,7 @@ public function infoCommand(Input $input, Output $output): void * get the kite paths * * @options - * --inline Output without newline. + * --inline bool;Output without newline. * * @arguments * path The sub-path in the kite. if empty, return kite path. @@ -119,12 +120,12 @@ public function infoCommand(Input $input, Output $output): void * @param Input $input * @param Output $output */ - public function pathCommand(Input $input, Output $output): void + public function pathCommand(FlagsParser $fs, Output $output): void { - $subPath = $input->getStringArg(0); + $subPath = $fs->getArg(0); $fullPath = Kite::getPath($subPath); - if ($input->getBoolOpt('inline')) { + if ($fs->getOpt('inline')) { $output->writeRaw($fullPath); return; } @@ -137,25 +138,25 @@ public function pathCommand(Input $input, Output $output): void * * @options * -s, --search Search config names by `key` argument - * --keys Only show all key names of config - * --clean Output clean value. + * --keys bool;Only show all key names of config + * --clean bool;Output clean value. * * @arguments * key The key of config * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function configCommand(Input $input, Output $output): void + public function configCommand(FlagsParser $fs, Output $output): void { $app = $this->getApp(); - if ($input->getBoolOpt('keys')) { + if ($fs->getOpt('keys')) { $output->aList(array_keys($app->getConfig()), 'Keys of config'); return; } $conf = $app->getConfig(); - $key = $input->getStringArg(0); + $key = $fs->getArg(0); // show all config if (!$key) { @@ -168,7 +169,7 @@ public function configCommand(Input $input, Output $output): void } $value = null; - $match = $input->getSameBoolOpt('s,search'); + $match = $fs->getOpt('search'); if ($match) { // match key foreach ($conf as $name => $item) { if (stripos($name, $key) !== false) { @@ -203,19 +204,18 @@ public function configCommand(Input $input, Output $output): void * @options * -l, --list List all registered object names * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function objectCommand(Input $input, Output $output): void + public function objectCommand(FlagsParser $fs, Output $output): void { $box = Kite::box(); - if ($input->getSameBoolOpt('l,list')) { + if ($fs->getOpt('l,list')) { $output->aList($box->getObjectIds(), 'Registered IDs'); return; } - $input->bindArgument('objId', 0); - $objName = $input->getStringArg('objId'); + $objName = $fs->getArg('objectName'); if (!$objName) { $output->liteError("Please input an object ID/name for see detail"); return; @@ -232,12 +232,12 @@ public function objectCommand(Input $input, Output $output): void * update {binName} to latest from github repository(by git pull) * * @options - * --no-deps Not update deps by composer update + * --no-deps bool;Not update deps by composer update * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function updateCommand(Input $input, Output $output): void + public function updateCommand(FlagsParser $fs, Output $output): void { $dir = $this->baseDir; $output->info('Will change to kite directory: ' . $dir); @@ -251,7 +251,7 @@ public function updateCommand(Input $input, Output $output): void // [, $msg,] = Sys::run($cmd); SysCmd::quickExec($cmd, $dir); - if (!$input->getBoolOpt('no-deps')) { + if (!$fs->getOpt('no-deps')) { Color::println('Run composer update:'); $cmd = 'composer update'; // [, $msg,] = Sys::run($cmd); @@ -260,7 +260,7 @@ public function updateCommand(Input $input, Output $output): void } Color::println('Add execute perm:'); - $binName = $input->getScriptName(); + $binName = $this->input->getScriptName(); // normal run [$code, $msg,] = Sys::run("chmod a+x bin/$binName", $dir); @@ -300,22 +300,22 @@ public function updateCommand(Input $input, Output $output): void * {binWithCmd} [-S HOST:PORT] * * @options - * -s, -S, --addr STRING The http server address. e.g 127.0.0.1:8552 - * -b, --php-bin STRING The php binary file(php) - * --show-info Only show serve info, not start listen + * -s, -S, --addr The http server address. e.g 127.0.0.1:8552 + * -b, --php-bin The php binary file(php) + * --show-info bool;Only show serve info, not start listen * - * @param Input $input + * @param FlagsParser $fs * @param Output $output * * @throws Throwable */ - public function webuiCommand(Input $input, Output $output): void + public function webuiCommand(FlagsParser $fs, Output $output): void { $this->webUi = array_merge($this->webUi, $this->app->getArrayParam('webui')); // vdump(BASE_PATH, $this->webUi); - $svrAddr = $input->getSameStringOpt('s,S,addr', $this->webUi['addr']); - $phpBin = $input->getStringOpt('php-bin', $this->webUi['php-bin']); + $svrAddr = $fs->getOpt('addr', $this->webUi['addr']); + $phpBin = $fs->getOpt('php-bin', $this->webUi['php-bin']); // $docRoot = $input->getSameStringOpt('t,doc-root', $conf['root']); $docRoot = $this->webUi['root']; @@ -323,7 +323,7 @@ public function webuiCommand(Input $input, Output $output): void $pds = PhpDevServe::new($svrAddr, $docRoot); $pds->setPhpBin($phpBin); - if ($input->getBoolOpt('show-info')) { + if ($fs->getOpt('show-info')) { $output->aList($pds->getInfo(), 'Listen Information', ['ucFirst' => false]); return; } diff --git a/app/Console/Controller/UtilController.php b/app/Console/Controller/UtilController.php index 18a0213..abab6eb 100644 --- a/app/Console/Controller/UtilController.php +++ b/app/Console/Controller/UtilController.php @@ -10,9 +10,9 @@ namespace Inhere\Kite\Console\Controller; use Inhere\Console\Controller; -use Inhere\Console\IO\Input; use Inhere\Console\IO\Output; use Inhere\Kite\Kite; +use Toolkit\PFlag\FlagsParser; use function date; use function explode; use function implode; @@ -64,15 +64,18 @@ protected static function commandAliases(): array /** * join multi line text * + * @arguments + * text The multi line text + * * @options * --sep The separator char. Defaults to an empty string. * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function joinCommand(Input $input, Output $output): void + public function joinCommand(FlagsParser $fs, Output $output): void { - $text = trim($input->getStringArg(0)); + $text = trim($fs->getArg(0)); if (!$text) { $output->colored('empty text'); return; @@ -80,17 +83,16 @@ public function joinCommand(Input $input, Output $output): void $lines = explode("\n", $text); - $sep = $input->getStringOpt('sep'); + $sep = $fs->getOpt('sep'); echo implode($sep, $lines), "\n"; } /** * print current datetime * - * @param Input $input * @param Output $output */ - public function dateCommand(Input $input, Output $output): void + public function dateCommand( Output $output): void { $time = time(); @@ -118,10 +120,10 @@ public function dateCommand(Input $input, Output $output): void /** * find IDEA in the machine * - * @param Input $input + * @param FlagsParser $fs * @param Output $output */ - public function findJetBrainsCommand(Input $input, Output $output): void + public function findJetBrainsCommand(FlagsParser $fs, Output $output): void { $dirs = [ // '~/Library/Preferences/PhpStorm2019.3/', @@ -130,20 +132,25 @@ public function findJetBrainsCommand(Input $input, Output $output): void '~/Library/Application\ Support/JetBrains/Toolbox/apps/', ]; - $ideName = $input->getStringArg('name', 'all'); + $ideName = $fs->getArg('name', 'all'); // rm -rf ~/Library/Application\ Support/${NAME}*/eval vdump($dirs); } /** - * @param Input $input - * @param Output $output + * @arguments + * msg The log message + * + * @options + * --type The log type name. + * + * @param FlagsParser $fs */ - public function logCommand(Input $input, Output $output): void + public function logCommand(FlagsParser $fs): void { - $msg = $input->getFirstArg(); - $type = $input->getStringOpt('type'); + $msg = $fs->getFirstArg(); + $type = $fs->getOpt('type'); if (!$msg && !$type) { return;