Skip to content

Commit 496bb5c

Browse files
committed
remove some old methods, update some method param define.
1 parent 391938b commit 496bb5c

18 files changed

+158
-228
lines changed

examples/Command/CorCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static function aliases(): array
3838
* @param Input $input
3939
* @param Output $output
4040
*/
41-
protected function execute($input, $output)
41+
protected function execute(Input $input, Output $output)
4242
{
4343
$output->aList([
4444
'support coroutine?' => Helper::isSupportCoroutine() ? 'Y' : 'N',

examples/Controller/HomeController.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
use LogicException;
1212
use RuntimeException;
1313
use Toolkit\Cli\Cli;
14-
use Toolkit\Cli\Download;
14+
use Toolkit\Cli\Util\Download;
15+
use Toolkit\PFlag\FlagsParser;
1516
use Toolkit\Stdlib\Php;
1617
use function sleep;
1718
use function trigger_error;
@@ -114,7 +115,7 @@ public function disabledCommand(): void
114115
*/
115116
protected function defArgConfigure(): void
116117
{
117-
$fs = $this->newActionFlags();
118+
$fs = $this->curActionFlags();
118119
$fs->addOptByRule('yes,y', "bool;description for the option 'yes'");
119120
$fs->addOptByRule('opt1', "bool;description for the option 'opt1'");
120121

@@ -311,29 +312,29 @@ public function dynamicTextCommand(): void
311312
* a progress bar example show, by Show::progressBar()
312313
*
313314
* @options
314-
* --type the progress type, allow: bar,txt. <cyan>txt</cyan>
315-
* --done-char the done show char. <info>=</info>
316-
* --wait-char the waiting show char. <info>-</info>
317-
* --sign-char the sign char show. <info>></info>
315+
* --type the progress type, allow: bar,txt. <cyan>txt</cyan>
316+
* --done-char the done show char. <info>=</info>
317+
* --wait-char the waiting show char. <info>-</info>
318+
* --sign-char the sign char show. <info>></info>
318319
*
319-
* @param Input $input
320+
* @param FlagsParser $fs
320321
*
321322
* @return int
322323
* @example
323324
* {script} {command}
324325
* {script} {command} --done-char '#' --wait-char ' '
325326
*/
326-
public function progressCommand($input): int
327+
public function progressCommand(FlagsParser $fs): int
327328
{
328329
$i = 0;
329330
$total = 120;
330-
if ($input->getOpt('type') === 'bar') {
331+
if ($fs->getOpt('type') === 'bar') {
331332
$bar = $this->output->progressBar($total, [
332333
'msg' => 'Msg Text',
333334
'doneMsg' => 'Done Msg Text',
334-
'doneChar' => $input->getOpt('done-char', '='), // ▓
335-
'waitChar' => $input->getOpt('wait-char', '-'), // ░
336-
'signChar' => $input->getOpt('sign-char', '>'),
335+
'doneChar' => $fs->getOpt('done-char', '='), // ▓
336+
'waitChar' => $fs->getOpt('wait-char', '-'), // ░
337+
'signChar' => $fs->getOpt('sign-char', '>'),
337338
]);
338339
} else {
339340
$bar = $this->output->progressTxt($total, 'Doing go g...', 'Done');
@@ -496,6 +497,7 @@ public function paddingCommand(): void
496497
* a example for use arguments on command
497498
*
498499
* @usage home:useArg [arg1=val1 arg2=arg2] [options]
500+
*
499501
* @example
500502
* 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
501503
* 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
@@ -533,20 +535,28 @@ public function envCommand(): void
533535

534536
/**
535537
* This is a demo for download a file to local
536-
* @usage {command} url=url saveTo=[saveAs] type=[bar|text]
538+
*
539+
* @usage {command} url=url saveTo=[saveAs] --type=[bar|text]
540+
*
541+
* @options
542+
* --type The progress bar type. allow: bar, text(default)
543+
*
544+
* @arguments
545+
* url string;The remote file url;required
546+
* saveAs The local file path for save download file.
537547
*
538548
* @example {command} url=https://github.com/inhere/php-console/archive/master.zip type=bar
539549
*/
540-
public function downCommand(): int
550+
public function downCommand(FlagsParser $fs): int
541551
{
542-
$url = $this->input->getArg('url');
543-
552+
$url = $fs->getArg('url');
544553
if (!$url) {
545-
$this->output->liteError('Please input you want to downloaded file url, use: url=[url]', 1);
554+
$this->output->liteError('Please input the downloaded file url');
555+
return 1;
546556
}
547557

548-
$saveAs = $this->input->getArg('saveAs');
549-
$type = $this->input->getArg('type', 'text');
558+
$saveAs = $fs->getArg('saveAs');
559+
$type = $fs->getOpt('type', 'text');
550560

551561
if (!$saveAs) {
552562
$saveAs = __DIR__ . '/' . basename($url);

examples/Controller/ShowController.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ReflectionException;
1919
use Toolkit\Cli\Color;
2020
use Toolkit\Cli\Highlighter;
21+
use Toolkit\PFlag\FlagsParser;
2122
use function file_get_contents;
2223

2324
/**
@@ -61,14 +62,17 @@ public function titleCommand(): int
6162

6263
/**
6364
* output format message: splitLine
65+
*
6466
* @options
65-
* -w, --width WIDTH The split line width. default is current screen width.
67+
* -w, --width int;The split line width. default is current screen width.
6668
*/
67-
public function splitLineCommand(): int
69+
public function splitLineCommand(FlagsParser $fs): int
6870
{
69-
$this->output->splitLine('', '=', $this->getSameOpt(['w', 'width'], 0));
70-
$this->output->splitLine('split Line', '-', $this->getSameOpt(['w', 'width'], 0));
71-
$this->output->splitLine('split 中文 Line', '-', $this->getSameOpt(['w', 'width'], 0));
71+
$width = $fs->getOpt('width');
72+
73+
$this->output->splitLine('', '=', $width);
74+
$this->output->splitLine('split Line', '-', $width);
75+
$this->output->splitLine('split 中文 Line', '-', $width);
7276

7377
return 0;
7478
}

src/BuiltIn/SelfUpdateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class SelfUpdateCommand extends Command
4949
* @param Input $input
5050
* @param Output $output
5151
*/
52-
protected function execute($input, $output)
52+
protected function execute(Input $input, Output $output)
5353
{
5454
$this->version = $this->getApp()->getVersion();
5555
$parser = new VersionParser;

src/Command.php

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
namespace Inhere\Console;
1010

1111
use Inhere\Console\Contract\CommandInterface;
12-
use Inhere\Console\IO\Input;
12+
use Inhere\Console\Handler\AbstractHandler;
1313
use ReflectionException;
14+
use Toolkit\PFlag\FlagsParser;
1415

1516
/**
1617
* Class Command
@@ -20,7 +21,7 @@
2021
* ```php
2122
* class MyCommand extends Command
2223
* {
23-
* protected function execute($input, $output)
24+
* protected function execute(Input $input, Output $output)
2425
* {
2526
* // some logic ...
2627
* }
@@ -49,44 +50,39 @@ protected function init(): void
4950
}
5051

5152
/**
52-
* @throws ReflectionException
53+
* @param FlagsParser $fs
5354
*/
54-
protected function initForRun(Input $input): void
55+
protected function beforeInitFlagsParser(FlagsParser $fs): void
5556
{
56-
parent::initForRun($input);
57+
$fs->setStopOnFistArg(false);
5758

58-
$this->flags->setStopOnFistArg(false);
5959
// old mode: options and arguments at method annotations
6060
if ($this->compatible) {
61-
$this->flags->setSkipOnUndefined(true);
61+
$fs->setSkipOnUndefined(true);
6262
}
63+
}
6364

64-
$this->debugf('load flags configure for command: %s', self::getName());
65-
// load input definition configure
65+
/**
66+
* @param FlagsParser $fs
67+
*
68+
* @throws ReflectionException
69+
*/
70+
protected function afterInitFlagsParser(FlagsParser $fs): void
71+
{
72+
$this->debugf('load flags configure for command: %s', $this->getRealName());
6673
$this->configure();
6774

75+
$isEmpty = $this->flags->isEmpty();
76+
77+
// load built in options
78+
$fs->addOptsByRules(GlobalOption::getAloneOptions());
79+
6880
// not config flags. load rules from method doc-comments
69-
if ($this->flags->isEmpty()) {
70-
$this->loadRulesByDocblock(self::METHOD, $this->flags);
81+
if ($isEmpty) {
82+
$this->loadRulesByDocblock(self::METHOD, $fs);
7183
}
7284
}
7385

74-
// protected function doRun(array $args)
75-
// {
76-
// parent::doRun($args);
77-
// }
78-
79-
/*
80-
* Configure command
81-
*/
82-
// protected function configure()
83-
// {
84-
// $this
85-
// ->createDefinition()
86-
// ->addArgument('test')
87-
// ->addOption('test');
88-
// }
89-
9086
/**
9187
* @param Command $parent
9288
*/
@@ -98,10 +94,10 @@ public function setParent(Command $parent): void
9894
/**
9995
* @return $this
10096
*/
101-
public function getRootCommand(): Command
97+
public function getRoot(): Command
10298
{
10399
if ($this->parent) {
104-
return $this->parent->getRootCommand();
100+
return $this->parent->getRoot();
105101
}
106102

107103
return $this;

src/Component/ConsoleAppIShell.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
namespace Inhere\Console\Component;
44

55
use Inhere\Console\Application;
6-
use Toolkit\Stdlib\Obj\AbstractObj;
6+
use Inhere\Console\Component\Interact\IShell;
77

88
/**
99
* Class ConsoleAppIShell
1010
*
1111
* @package Inhere\Console
1212
*/
13-
class ConsoleAppIShell extends AbstractObj
13+
class ConsoleAppIShell extends IShell
1414
{
1515
/**
1616
* @var Application

src/Concern/ApplicationHelpTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Inhere\Console\Concern;
1010

11-
use Inhere\Console\AbstractHandler;
11+
use Inhere\Console\Handler\AbstractHandler;
1212
use Inhere\Console\Console;
1313
use Inhere\Console\ConsoleEvent;
1414
use Inhere\Console\Contract\CommandInterface;

src/Concern/CommandHelpTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Inhere\Console\Concern;
44

5-
use Inhere\Console\AbstractHandler;
5+
use Inhere\Console\Handler\AbstractHandler;
66
use Inhere\Console\Annotate\DocblockRules;
77
use Inhere\Console\Console;
88
use Inhere\Console\Util\FormatUtil;

src/Concern/ControllerHelpTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Inhere\Console\Concern;
44

55
use Inhere\Console\Console;
6+
use Inhere\Console\GlobalOption;
67
use Inhere\Console\Util\FormatUtil;
78
use ReflectionClass;
89
use Toolkit\Cli\ColorTag;
@@ -32,7 +33,6 @@ trait ControllerHelpTrait
3233
* -s, --search Search command by input keywords
3334
* --format Set the help information dump format(raw, xml, json, markdown)
3435
* @return int
35-
* @throws \ReflectionException
3636
* @example
3737
* {script} {name} -h
3838
* {script} {name}:help
@@ -96,7 +96,7 @@ public function showCommandList(): void
9696
}
9797

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

102102
/**
@@ -144,7 +144,7 @@ public function showCommandList(): void
144144

145145
// if is alone running.
146146
if ($detached = $this->isDetached()) {
147-
$name = $sName . ' ';
147+
// $name = $sName . ' ';
148148
$usage = "$script <info>COMMAND</info> [--options ...] [arguments ...]";
149149
} else {
150150
$name = $sName . $this->delimiter;

src/Concern/InputFlagsWareTrait.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)