Skip to content

Commit

Permalink
remove some old methods, update some method param define.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 18, 2021
1 parent 391938b commit 496bb5c
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 228 deletions.
2 changes: 1 addition & 1 deletion examples/Command/CorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function aliases(): array
* @param Input $input
* @param Output $output
*/
protected function execute($input, $output)
protected function execute(Input $input, Output $output)
{
$output->aList([
'support coroutine?' => Helper::isSupportCoroutine() ? 'Y' : 'N',
Expand Down
48 changes: 29 additions & 19 deletions examples/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use LogicException;
use RuntimeException;
use Toolkit\Cli\Cli;
use Toolkit\Cli\Download;
use Toolkit\Cli\Util\Download;
use Toolkit\PFlag\FlagsParser;
use Toolkit\Stdlib\Php;
use function sleep;
use function trigger_error;
Expand Down Expand Up @@ -114,7 +115,7 @@ public function disabledCommand(): void
*/
protected function defArgConfigure(): void
{
$fs = $this->newActionFlags();
$fs = $this->curActionFlags();
$fs->addOptByRule('yes,y', "bool;description for the option 'yes'");
$fs->addOptByRule('opt1', "bool;description for the option 'opt1'");

Expand Down Expand Up @@ -311,29 +312,29 @@ public function dynamicTextCommand(): void
* a progress bar example show, by Show::progressBar()
*
* @options
* --type the progress type, allow: bar,txt. <cyan>txt</cyan>
* --done-char the done show char. <info>=</info>
* --wait-char the waiting show char. <info>-</info>
* --sign-char the sign char show. <info>></info>
* --type the progress type, allow: bar,txt. <cyan>txt</cyan>
* --done-char the done show char. <info>=</info>
* --wait-char the waiting show char. <info>-</info>
* --sign-char the sign char show. <info>></info>
*
* @param Input $input
* @param FlagsParser $fs
*
* @return int
* @example
* {script} {command}
* {script} {command} --done-char '#' --wait-char ' '
*/
public function progressCommand($input): int
public function progressCommand(FlagsParser $fs): int
{
$i = 0;
$total = 120;
if ($input->getOpt('type') === 'bar') {
if ($fs->getOpt('type') === 'bar') {
$bar = $this->output->progressBar($total, [
'msg' => 'Msg Text',
'doneMsg' => 'Done Msg Text',
'doneChar' => $input->getOpt('done-char', '='), // ▓
'waitChar' => $input->getOpt('wait-char', '-'), // ░
'signChar' => $input->getOpt('sign-char', '>'),
'doneChar' => $fs->getOpt('done-char', '='), // ▓
'waitChar' => $fs->getOpt('wait-char', '-'), // ░
'signChar' => $fs->getOpt('sign-char', '>'),
]);
} else {
$bar = $this->output->progressTxt($total, 'Doing go g...', 'Done');
Expand Down Expand Up @@ -496,6 +497,7 @@ public function paddingCommand(): void
* a example for use arguments on command
*
* @usage home:useArg [arg1=val1 arg2=arg2] [options]
*
* @example
* home:useArg status=2 name=john arg0 -s=test --page=23 -d -rf --debug --test=false -a v1 --ab -c -g --cd val -h '' -i stat=online
* home:useArg status=2 name=john name=tom name=jack arg0 -s=test --page=23 --id=23 --id=154 --id=456 -d -rf --debug --test=false
Expand Down Expand Up @@ -533,20 +535,28 @@ public function envCommand(): void

/**
* This is a demo for download a file to local
* @usage {command} url=url saveTo=[saveAs] type=[bar|text]
*
* @usage {command} url=url saveTo=[saveAs] --type=[bar|text]
*
* @options
* --type The progress bar type. allow: bar, text(default)
*
* @arguments
* url string;The remote file url;required
* saveAs The local file path for save download file.
*
* @example {command} url=https://github.com/inhere/php-console/archive/master.zip type=bar
*/
public function downCommand(): int
public function downCommand(FlagsParser $fs): int
{
$url = $this->input->getArg('url');

$url = $fs->getArg('url');
if (!$url) {
$this->output->liteError('Please input you want to downloaded file url, use: url=[url]', 1);
$this->output->liteError('Please input the downloaded file url');
return 1;
}

$saveAs = $this->input->getArg('saveAs');
$type = $this->input->getArg('type', 'text');
$saveAs = $fs->getArg('saveAs');
$type = $fs->getOpt('type', 'text');

if (!$saveAs) {
$saveAs = __DIR__ . '/' . basename($url);
Expand Down
14 changes: 9 additions & 5 deletions examples/Controller/ShowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ReflectionException;
use Toolkit\Cli\Color;
use Toolkit\Cli\Highlighter;
use Toolkit\PFlag\FlagsParser;
use function file_get_contents;

/**
Expand Down Expand Up @@ -61,14 +62,17 @@ public function titleCommand(): int

/**
* output format message: splitLine
*
* @options
* -w, --width WIDTH The split line width. default is current screen width.
* -w, --width int;The split line width. default is current screen width.
*/
public function splitLineCommand(): int
public function splitLineCommand(FlagsParser $fs): int
{
$this->output->splitLine('', '=', $this->getSameOpt(['w', 'width'], 0));
$this->output->splitLine('split Line', '-', $this->getSameOpt(['w', 'width'], 0));
$this->output->splitLine('split 中文 Line', '-', $this->getSameOpt(['w', 'width'], 0));
$width = $fs->getOpt('width');

$this->output->splitLine('', '=', $width);
$this->output->splitLine('split Line', '-', $width);
$this->output->splitLine('split 中文 Line', '-', $width);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/BuiltIn/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SelfUpdateCommand extends Command
* @param Input $input
* @param Output $output
*/
protected function execute($input, $output)
protected function execute(Input $input, Output $output)
{
$this->version = $this->getApp()->getVersion();
$parser = new VersionParser;
Expand Down
54 changes: 25 additions & 29 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
namespace Inhere\Console;

use Inhere\Console\Contract\CommandInterface;
use Inhere\Console\IO\Input;
use Inhere\Console\Handler\AbstractHandler;
use ReflectionException;
use Toolkit\PFlag\FlagsParser;

/**
* Class Command
Expand All @@ -20,7 +21,7 @@
* ```php
* class MyCommand extends Command
* {
* protected function execute($input, $output)
* protected function execute(Input $input, Output $output)
* {
* // some logic ...
* }
Expand Down Expand Up @@ -49,44 +50,39 @@ protected function init(): void
}

/**
* @throws ReflectionException
* @param FlagsParser $fs
*/
protected function initForRun(Input $input): void
protected function beforeInitFlagsParser(FlagsParser $fs): void
{
parent::initForRun($input);
$fs->setStopOnFistArg(false);

$this->flags->setStopOnFistArg(false);
// old mode: options and arguments at method annotations
if ($this->compatible) {
$this->flags->setSkipOnUndefined(true);
$fs->setSkipOnUndefined(true);
}
}

$this->debugf('load flags configure for command: %s', self::getName());
// load input definition configure
/**
* @param FlagsParser $fs
*
* @throws ReflectionException
*/
protected function afterInitFlagsParser(FlagsParser $fs): void
{
$this->debugf('load flags configure for command: %s', $this->getRealName());
$this->configure();

$isEmpty = $this->flags->isEmpty();

// load built in options
$fs->addOptsByRules(GlobalOption::getAloneOptions());

// not config flags. load rules from method doc-comments
if ($this->flags->isEmpty()) {
$this->loadRulesByDocblock(self::METHOD, $this->flags);
if ($isEmpty) {
$this->loadRulesByDocblock(self::METHOD, $fs);
}
}

// protected function doRun(array $args)
// {
// parent::doRun($args);
// }

/*
* Configure command
*/
// protected function configure()
// {
// $this
// ->createDefinition()
// ->addArgument('test')
// ->addOption('test');
// }

/**
* @param Command $parent
*/
Expand All @@ -98,10 +94,10 @@ public function setParent(Command $parent): void
/**
* @return $this
*/
public function getRootCommand(): Command
public function getRoot(): Command
{
if ($this->parent) {
return $this->parent->getRootCommand();
return $this->parent->getRoot();
}

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Component/ConsoleAppIShell.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Inhere\Console\Component;

use Inhere\Console\Application;
use Toolkit\Stdlib\Obj\AbstractObj;
use Inhere\Console\Component\Interact\IShell;

/**
* Class ConsoleAppIShell
*
* @package Inhere\Console
*/
class ConsoleAppIShell extends AbstractObj
class ConsoleAppIShell extends IShell
{
/**
* @var Application
Expand Down
2 changes: 1 addition & 1 deletion src/Concern/ApplicationHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Inhere\Console\Concern;

use Inhere\Console\AbstractHandler;
use Inhere\Console\Handler\AbstractHandler;
use Inhere\Console\Console;
use Inhere\Console\ConsoleEvent;
use Inhere\Console\Contract\CommandInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Concern/CommandHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Inhere\Console\Concern;

use Inhere\Console\AbstractHandler;
use Inhere\Console\Handler\AbstractHandler;
use Inhere\Console\Annotate\DocblockRules;
use Inhere\Console\Console;
use Inhere\Console\Util\FormatUtil;
Expand Down
6 changes: 3 additions & 3 deletions src/Concern/ControllerHelpTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Inhere\Console\Concern;

use Inhere\Console\Console;
use Inhere\Console\GlobalOption;
use Inhere\Console\Util\FormatUtil;
use ReflectionClass;
use Toolkit\Cli\ColorTag;
Expand Down Expand Up @@ -32,7 +33,6 @@ trait ControllerHelpTrait
* -s, --search Search command by input keywords
* --format Set the help information dump format(raw, xml, json, markdown)
* @return int
* @throws \ReflectionException
* @example
* {script} {name} -h
* {script} {name}:help
Expand Down Expand Up @@ -96,7 +96,7 @@ public function showCommandList(): void
}

$commands = [];
$showDisabled = (bool)$this->getOpt('show-disabled', false);
$showDisabled = $this->flags->getOpt(GlobalOption::SHOW_DISABLED, false);
$defaultDes = 'No description message';

/**
Expand Down Expand Up @@ -144,7 +144,7 @@ public function showCommandList(): void

// if is alone running.
if ($detached = $this->isDetached()) {
$name = $sName . ' ';
// $name = $sName . ' ';
$usage = "$script <info>COMMAND</info> [--options ...] [arguments ...]";
} else {
$name = $sName . $this->delimiter;
Expand Down
13 changes: 0 additions & 13 deletions src/Concern/InputFlagsWareTrait.php

This file was deleted.

Loading

0 comments on commit 496bb5c

Please sign in to comment.