Skip to content

Commit 3e06b07

Browse files
committed
breaking: delete some Input methods, use flags instead the input
1 parent 87c8fff commit 3e06b07

13 files changed

+90
-374
lines changed

examples/Controller/HomeController.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Inhere\Console\Component\Symbol\ArtFont;
66
use Inhere\Console\Controller;
7-
use Inhere\Console\IO\Input;
87
use Inhere\Console\Util\Interact;
98
use Inhere\Console\Util\ProgressBar;
109
use Inhere\Console\Util\Show;
@@ -123,15 +122,8 @@ protected function defArgConfigure(): void
123122
}
124123

125124
// desc set at $this->commandMetas.
126-
public function defArgCommand(): void
125+
public function defArgCommand(FlagsParser $fs): void
127126
{
128-
$this->output->dump(
129-
$this->input->getArgs(),
130-
$this->input->getOpts(),
131-
$this->input->getBoolOpt('y')
132-
);
133-
134-
$fs = $this->curActionFlags();
135127
$this->output->dump(
136128
$fs->getArgs(),
137129
$fs->getOpts(),
@@ -198,12 +190,12 @@ public function colorCheckCommand(): void
198190
* output art font text
199191
*
200192
* @options
201-
* --font Set the art font name(allow: {internalFonts}).
202-
* --italic Set the art font type is italic.
203-
* --style Set the art font style.
193+
* --font Set the art font name(allow: {internalFonts}).
194+
* --italic bool;Set the art font type is italic.
195+
* --style Set the art font style.
204196
* @return int
205197
*/
206-
public function artFontCommand(): int
198+
public function artFontCommand(FlagsParser $fs): int
207199
{
208200
$name = $this->input->getLongOpt('font', '404');
209201

@@ -212,8 +204,8 @@ public function artFontCommand(): int
212204
}
213205

214206
ArtFont::create()->show($name, ArtFont::INTERNAL_GROUP, [
215-
'type' => $this->input->getBoolOpt('italic') ? 'italic' : '',
216-
'style' => $this->input->getOpt('style'),
207+
'type' => $fs->getOpt('italic') ? 'italic' : '',
208+
'style' => $fs->getOpt('style'),
217209
]);
218210

219211
return 0;

examples/Controller/InteractController.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Inhere\Console\Util\Interact;
1313
use Inhere\Console\Util\Show;
1414
use Toolkit\Cli\Util\Terminal;
15+
use Toolkit\PFlag\FlagsParser;
1516
use function preg_match;
1617

1718
/**
@@ -97,15 +98,16 @@ public function askCommand(): void
9798

9899
/**
99100
* This is a demo for use <magenta>Interact::limitedAsk()</magenta> method
101+
*
100102
* @options
101-
* --nv Not use validator.
102-
* --limit limit times.(default: 3)
103+
* --nv bool;Not use validator.
104+
* --limit int;limit times.(default: 3)
103105
*/
104-
public function limitedAskCommand(): void
106+
public function limitedAskCommand(FlagsParser $fs): void
105107
{
106-
$times = (int)$this->input->getOpt('limit', 3);
108+
$times = $fs->getOpt('limit', 3);
107109

108-
if ($this->input->getBoolOpt('nv')) {
110+
if ($fs->getOpt('nv')) {
109111
$a = Interact::limitedAsk('you name is: ', '', null, $times);
110112
} else {
111113
$a = Interact::limitedAsk('you name is: ', '', static function ($val) {

src/AbstractApplication.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Inhere\Console\Util\Interact;
2626
use InvalidArgumentException;
2727
use Throwable;
28+
use Toolkit\Cli\Helper\FlagHelper;
2829
use Toolkit\Cli\Style;
2930
use Toolkit\Cli\Util\LineParser;
3031
use Toolkit\PFlag\SFlags;
@@ -353,7 +354,7 @@ public function stop(int $code = 0)
353354
*/
354355
public function runWithArgs(array $args)
355356
{
356-
$this->input->setArgs($args);
357+
$this->input->setFlags($args);
357358
return $this->run(false);
358359
}
359360

@@ -473,10 +474,16 @@ protected function handleGlobalCommand(string $command): bool
473474
return true;
474475
}
475476

476-
$this->logf(Console::VERB_DEBUG, 'run the global command: %s', $command);
477+
$this->debugf('run the global command: %s', $command);
477478
switch ($command) {
478479
case 'help':
479-
$this->showHelpInfo($this->input->getFirstArg());
480+
$cmd = '';
481+
$args = $this->flags->getRawArgs();
482+
if ($args && FlagHelper::isValidName($args[0])) {
483+
$cmd = $args[0];
484+
}
485+
486+
$this->showHelpInfo($cmd);
480487
break;
481488
case 'list':
482489
$this->showCommandList();

src/BuiltIn/PharController.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ public function packCommand(Input $input, Output $output, FlagsParser $fs): int
100100
$workDir = $input->getPwd();
101101

102102
$dir = $fs->getOpt('dir') ?: $workDir;
103-
$cpr = $this->configCompiler($dir);
103+
// get config file
104+
$confFile = $fs->getOpt('config') ?: $dir . '/phar.build.inc';
105+
106+
$cpr = $this->configCompiler($dir, $confFile);
104107

105108
$refresh = $fs->getOpt('refresh');
106109
$outFile = $fs->getOpt('output', $this->defPkgName);
@@ -155,10 +158,11 @@ public function packCommand(Input $input, Output $output, FlagsParser $fs): int
155158

156159
/**
157160
* @param string $dir
161+
* @param string $confFile
158162
*
159163
* @return PharCompiler
160164
*/
161-
protected function configCompiler(string $dir): PharCompiler
165+
protected function configCompiler(string $dir, string $confFile): PharCompiler
162166
{
163167
// create compiler
164168
$compiler = new PharCompiler($dir);
@@ -171,14 +175,12 @@ protected function configCompiler(string $dir): PharCompiler
171175
}
172176

173177
// use config file
174-
$configFile = $this->input->getSameOpt(['c', 'config']) ?: $dir . '/phar.build.inc';
175-
176-
if ($configFile && is_file($configFile)) {
177-
require $configFile;
178+
if ($confFile && is_file($confFile)) {
179+
require $confFile;
178180
return $compiler->in($dir);
179181
}
180182

181-
throw new RuntimeException("The phar build config file not exists! File: $configFile");
183+
throw new RuntimeException("The phar build config file not exists! File: $confFile");
182184
}
183185

184186
/**

src/Concern/ApplicationHelpTrait.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ public function showHelpInfo(string $command = ''): void
117117
// display help for a special command
118118
if ($command) {
119119
$this->debugf('display command help by use help: help COMMAND');
120-
$in->setCommand($command);
121-
$in->setSOpt('h', true);
122-
$in->clearArgs();
123-
$this->dispatch($command);
120+
$this->dispatch($command, ['-h']);
124121
return;
125122
}
126123

@@ -183,7 +180,7 @@ public function showCommandList(): void
183180
/** @var Input $input */
184181
$input = $this->input;
185182
// has option: --auto-completion
186-
$autoComp = $input->getBoolOpt('auto-completion');
183+
$autoComp = $input->getOpt('auto-completion');
187184
// has option: --shell-env
188185
$shellEnv = (string)$input->getLongOpt('shell-env', '');
189186
// input is an path: /bin/bash
@@ -325,7 +322,7 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
325322

326323
// info
327324
$glue = ' ';
328-
$genFile = $input->getStringOpt('gen-file', 'none');
325+
$genFile = $input->getOpt('gen-file', 'none');
329326
$tplDir = dirname(__DIR__, 2) . '/resource/templates';
330327

331328
if ($shellEnv === 'bash') {
@@ -347,7 +344,7 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
347344
}
348345

349346
// new: support custom tpl file for gen completion script
350-
$userTplFile = $input->getStringOpt('tpl-file');
347+
$userTplFile = $input->getOpt('tpl-file');
351348
if ($userTplFile && file_exists($userTplFile)) {
352349
$tplFile = $userTplFile;
353350
}

src/Concern/AttachApplicationTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function isInteractive(): bool
8383
return $this->app->isInteractive();
8484
}
8585

86-
$value = $this->input->getBoolOpt(GlobalOption::NO_INTERACTIVE);
86+
$value = $this->input->getOpt(GlobalOption::NO_INTERACTIVE);
8787
return $value === false;
8888
}
8989

src/Concern/ControllerHelpTrait.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ trait ControllerHelpTrait
3232
* @options
3333
* -s, --search Search command by input keywords
3434
* --format Set the help information dump format(raw, xml, json, markdown)
35+
*
3536
* @return int
3637
* @example
37-
* {script} {name} -h
38-
* {script} {name}:help
39-
* {script} {name}:help index
40-
* {script} {name}:index -h
41-
* {script} {name} index
38+
* {binName} {name} -h
39+
* {binName} {name}:help
40+
* {binName} {name}:help index
41+
* {binName} {name}:index -h
42+
* {binName} {name} index
4243
*/
4344
public function helpCommand(): int
4445
{

src/Concern/InputArgumentsTrait.php

Lines changed: 0 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -167,131 +167,6 @@ public function getRequiredArg($name, string $errMsg = '')
167167
throw new PromptException($errMsg);
168168
}
169169

170-
/**
171-
* Get first argument
172-
*
173-
* @param string $default
174-
*
175-
* @return string
176-
*/
177-
public function getFirstArg(string $default = ''): string
178-
{
179-
return $this->get(0, $default);
180-
}
181-
182-
/**
183-
* Get second argument
184-
*
185-
* @param string $default
186-
*
187-
* @return string
188-
*/
189-
public function getSecondArg(string $default = ''): string
190-
{
191-
return $this->get(1, $default);
192-
}
193-
194-
/**
195-
* Get an string argument value
196-
*
197-
* @param string|int $key
198-
* @param string $default
199-
*
200-
* @return string
201-
*/
202-
public function getStringArg($key, string $default = ''): string
203-
{
204-
return (string)$this->get($key, $default);
205-
}
206-
207-
/**
208-
* Get an int argument value
209-
*
210-
* @param string|int $key
211-
* @param int $default
212-
*
213-
* @return int
214-
*/
215-
public function getInt($key, int $default = 0): int
216-
{
217-
return $this->getIntArg($key, $default);
218-
}
219-
220-
/**
221-
* Get an int argument value
222-
*
223-
* @param string|int $key
224-
* @param int $default
225-
*
226-
* @return int
227-
*/
228-
public function getIntArg($key, int $default = 0): int
229-
{
230-
$value = $this->get($key);
231-
232-
return $value === null ? $default : (int)$value;
233-
}
234-
235-
/**
236-
* Get an array argument value
237-
*
238-
* @param string|int $key
239-
* @param array $default
240-
*
241-
* @return array
242-
*/
243-
public function getArrayArg($key, array $default = []): array
244-
{
245-
$value = $this->get($key);
246-
if (is_array($value)) {
247-
return $value;
248-
}
249-
250-
return $value ? [$value] : $default;
251-
}
252-
253-
/**
254-
* Get same args value
255-
* eg: des = description
256-
*
257-
* ```php
258-
* $input->sameArg('des, description');
259-
* $input->sameArg(['des', 'description']);
260-
* ```
261-
*
262-
* @param string|array $names
263-
* @param mixed $default
264-
*
265-
* @return mixed
266-
*/
267-
public function getSameArg($names, $default = null)
268-
{
269-
if (is_string($names)) {
270-
$names = array_map('trim', explode(',', $names));
271-
} elseif (!is_array($names)) {
272-
$names = (array)$names;
273-
}
274-
275-
foreach ($names as $name) {
276-
if ($this->hasArg($name)) {
277-
return $this->get($name);
278-
}
279-
}
280-
281-
return $default;
282-
}
283-
284-
/**
285-
* @param string|array $names
286-
* @param mixed $default
287-
*
288-
* @return mixed
289-
*/
290-
public function sameArg($names, $default = null)
291-
{
292-
return $this->getSameArg($names, $default);
293-
}
294-
295170
/**
296171
* clear args
297172
*/

0 commit comments

Comments
 (0)