Skip to content

Commit 606384c

Browse files
committed
update, some logic update
1 parent 5991db2 commit 606384c

File tree

9 files changed

+118
-38
lines changed

9 files changed

+118
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ composer.lock
1212
*.swo
1313
*.zip
1414
.DS_Store
15+
.interactive_history

examples/HomeController.php

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,6 @@ public function indexCommand()
3838
$this->write('hello, welcome!! this is ' . __METHOD__);
3939
}
4040

41-
/**
42-
* command config
43-
*/
44-
protected function colorConfigure()
45-
{
46-
$this->createDefinition()
47-
->setDescription('the color command description')
48-
->addArgument('name', Input::ARG_REQUIRED, 'description for the argument [name]')
49-
->addOption('yes', 'y', Input::OPT_BOOLEAN, 'description for the option [yes]')
50-
->addOption('opt1', null, Input::OPT_REQUIRED, 'description for the option [opt1]');
51-
}
52-
5341
/**
5442
* a example for use color text output on command
5543
* @usage ./bin/app home/color
@@ -61,7 +49,7 @@ public function colorCommand()
6149

6250
return 0;
6351
}
64-
$this->output->dumpVars($this->input->getArgs(), $this->input->getBoolOpt('y'));
52+
6553
$this->write('color text output:');
6654
$styles = $this->output->getStyle()->getStyleNames();
6755

@@ -220,6 +208,26 @@ public function useArgCommand()
220208
// var_dump($this->input);
221209
}
222210

211+
/**
212+
* command `defArgCommand` config
213+
*/
214+
protected function defArgConfigure()
215+
{
216+
$this->createDefinition()
217+
->setDescription('the command arg/opt config use defined configure, it like symfony console: argument define by position')
218+
->addArgument('name', Input::ARG_REQUIRED, 'description for the argument [name]')
219+
->addOption('yes', 'y', Input::OPT_BOOLEAN, 'description for the option [yes]')
220+
->addOption('opt1', null, Input::OPT_REQUIRED, 'description for the option [opt1]');
221+
}
222+
223+
/**
224+
* the command arg/opt config use defined configure, it like symfony console: argument define by position
225+
*/
226+
public function defArgCommand()
227+
{
228+
$this->output->dumpVars($this->input->getArgs(), $this->input->getOpts(), $this->input->getBoolOpt('y'));
229+
}
230+
223231
/**
224232
* use <red>Interact::confirm</red> method
225233
*

examples/baks/.interactive_history

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

examples/home

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/env/php
2+
<?php
3+
4+
use inhere\console\examples\HomeController;
5+
6+
define('PROJECT_PATH', dirname(__DIR__));
7+
8+
require __DIR__ . '/s-autoload.php';
9+
10+
$app = new \inhere\console\App([
11+
'debug' => true,
12+
'rootPath' => PROJECT_PATH,
13+
]);
14+
15+
$app->controller('home', HomeController::class);
16+
17+
exit(
18+
(int)$app->runAction('home', $app->getInput()->getCommand(), false, true)
19+
);

src/AbstractApp.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use inhere\console\io\Output;
1313
use inhere\console\traits\InputOutputTrait;
1414
use inhere\console\traits\SimpleEventStaticTrait;
15-
use inhere\console\utils\Helper;
1615

1716
/**
1817
* Class AbstractApp
@@ -33,6 +32,9 @@ abstract class AbstractApp
3332
const ON_STOP_RUN = 'stopRun';
3433
const ON_NOT_FOUND = 'notFound';
3534

35+
/** @var bool render no color */
36+
private static $noColor = false;
37+
3638
/**
3739
* @var string
3840
*/
@@ -69,6 +71,7 @@ abstract class AbstractApp
6971
protected static $internalOptions = [
7072
'--debug' => 'setting the application runtime debug level',
7173
'--no-color' => 'setting no color for message output',
74+
'--version' => 'Show application version information',
7275
];
7376

7477
/**
@@ -334,6 +337,16 @@ public function exceptionHandler($e)
334337
*/
335338
protected function filterSpecialCommand($command)
336339
{
340+
if (!$command) {
341+
if ($this->input->getSameOpt(['V', 'version'])) {
342+
$this->showVersionInfo();
343+
}
344+
}
345+
346+
if ($this->input->getSameOpt(['no-color'])) {
347+
self::$noColor = true;
348+
}
349+
337350
$command = $command ?: 'list';
338351

339352
switch ($command) {
@@ -469,6 +482,14 @@ public function showCommandList($quit = true)
469482
* getter/setter methods
470483
**********************************************************/
471484

485+
/**
486+
* @return bool
487+
*/
488+
public static function isNoColor(): bool
489+
{
490+
return self::$noColor;
491+
}
492+
472493
/**
473494
* @param array $controllers
474495
*/
@@ -543,12 +564,14 @@ public function isInternalCommand(string $name): bool
543564
public function setMeta(array $meta)
544565
{
545566
if ($meta) {
546-
$this->meta = array_merge($this->meta, (array)$meta);
567+
$this->meta = array_merge($this->meta, $meta);
547568
}
548569
}
549570

550571
/**
551572
* get meta info
573+
* @param null $name
574+
* @param null $default
552575
* @return array
553576
*/
554577
public function getMeta($name = null, $default = null)

src/App.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ public function runCommand($name, $believable = false)
116116
* @param string $name Controller name
117117
* @param string $action
118118
* @param bool $believable The `$name` is believable
119+
* @param bool $standAlone
119120
* @return mixed
120121
*/
121-
public function runAction($name, $action, $believable = false)
122+
public function runAction($name, $action, $believable = false, $standAlone = false)
122123
{
123124
// if $believable = true, will skip check.
124125
if (!$believable && !$this->isController($name)) {
@@ -141,6 +142,7 @@ public function runAction($name, $action, $believable = false)
141142

142143
$object::setName($name);
143144
$object->delimiter = $this->delimiter;
145+
$object->setStandAlone($standAlone);
144146

145147
return $object->setAction($action)->run();
146148
}

src/Controller.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,16 @@ abstract class Controller extends AbstractCommand
4444
*/
4545
public $delimiter = '/'; // '/' ':'
4646

47+
/**
48+
* @var bool
49+
*/
4750
protected $showMore = true;
4851

52+
/**
53+
* @var bool
54+
*/
55+
private $standAlone = false;
56+
4957
/**
5058
* load command configure
5159
*/
@@ -104,13 +112,13 @@ protected function showHelp()
104112

105113
/**
106114
* Show help of the controller command group or specified command action
107-
* @usage <info>{name}/[command] -h</info> OR <info>{name}/help [command]</info> OR <info>{name} [command]</info>
115+
* @usage <info>{name}/[command] -h</info> OR <info>{command} [command]</info> OR <info>{name} [command] -h</info>
108116
* @example
109-
* {script} {name} -h
110-
* {script} {name}/help
111-
* {script} {name}/help index
112-
* {script} {name}/index -h
113-
* {script} {name} index
117+
* {script} {name} -h
118+
* {script} {name}/help
119+
* {script} {name}/help index
120+
* {script} {name}/index -h
121+
* {script} {name} index
114122
*
115123
* @return int
116124
*/
@@ -135,13 +143,12 @@ final public function helpCommand()
135143
final protected function showCommandList()
136144
{
137145
$ref = new \ReflectionClass($this);
138-
139-
$class = $ref->getName();
140146
$sName = lcfirst(self::getName() ?: $ref->getShortName());
141-
$this->write("This is in the console controller [<bold>$class</bold>]\n");
147+
148+
// $this->write(sprintf("This is in the console controller [<bold>%s</bold>]\n", $class = $ref->getName();));
142149

143150
if (!($classDes = self::getDescription())) {
144-
$classDes = Annotation::description($ref->getDocComment()) ?: 'No Description';
151+
$classDes = Annotation::description($ref->getDocComment()) ?: 'No Description for the console controller';
145152
}
146153

147154
$suffix = $this->actionSuffix;
@@ -172,16 +179,22 @@ final protected function showCommandList()
172179
}
173180
}
174181

175-
$sep = $this->delimiter;
176-
$name = $sName . $sep;
182+
if ($this->standAlone) {
183+
$name = $sName . ' ';
184+
$usage = '<info>{command}</info> [arguments] [options]';
185+
} else {
186+
$name = $sName . $this->delimiter;
187+
$usage = "<info>{$name}</info>{command} [arguments] [options]";
188+
}
189+
177190
$this->output->mList([
178191
'Description:' => $classDes,
179-
'Usage:' => "{$name}[command] [arguments] [options]",
180-
'Group Name:' => "<info>$sName</info>",
192+
'Usage:' => $usage,
193+
//'Group Name:' => "<info>$sName</info>",
181194
'Commands:' => $commands,
182195
'Options:' => [
183-
'--help,-h' => 'Show help of the command group or specified command action',
184-
$this->showMore ? "\nMore information please use <cyan>{$name}[command] -h</cyan> OR <cyan>{$name}help [command]</cyan>" : ''
196+
'-h,--help' => 'Show help of the command group or specified command action',
197+
$this->showMore ? "\nMore information please use <cyan>{$name}[command] -h</cyan>" : ''
185198
],
186199
]);
187200
}
@@ -254,4 +267,21 @@ public function setNotFoundCallback($notFoundCallback)
254267
{
255268
$this->notFoundCallback = $notFoundCallback;
256269
}
270+
271+
/**
272+
* @return bool
273+
*/
274+
public function isStandAlone(): bool
275+
{
276+
return $this->standAlone;
277+
}
278+
279+
/**
280+
* @param bool $standAlone
281+
*/
282+
public function setStandAlone($standAlone = true)
283+
{
284+
$this->standAlone = (bool)$standAlone;
285+
}
286+
257287
}

src/io/Input.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public function setScript(string $script)
545545
* @param null|string $default
546546
* @return string
547547
*/
548-
public function getCommand($default = null): string
548+
public function getCommand($default = ''): string
549549
{
550550
return $this->command ?: $default;
551551
}

src/io/InputInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public function read($question = null, $nl = false): string;
3838
public function getScript(): string;
3939

4040
/**
41-
* @param null|string $default
41+
* @param string $default
4242
* @return string
4343
*/
44-
public function getCommand($default = null): string;
44+
public function getCommand($default = ''): string;
4545

4646
/**
4747
* @return array

0 commit comments

Comments
 (0)