Skip to content

Commit e30dfbc

Browse files
committed
add gen auto completion script support
1 parent c37a349 commit e30dfbc

File tree

12 files changed

+251
-62
lines changed

12 files changed

+251
-62
lines changed

res/auto-completion.bash

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
# ------------------------------------------------------------------------------
3+
# FILE: auto-completion.bash
4+
# AUTHOR: inhere (https://github.com/inhere)
5+
# VERSION: 1.0.0
6+
# DESCRIPTION: zsh shell complete for cli app: cliapp
7+
# ------------------------------------------------------------------------------
8+
# usage: source auto-completion.bash
9+
# run 'complete' to see registered complete function.
10+
11+
_console_get_command_list () {
12+
php ./examples/app --no-color --only-name
13+
}
14+
15+
_complete_for_cliapp () {
16+
local cur prev
17+
commands=(`_console_get_command_list`)
18+
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
19+
}
20+
21+
complete -F _complete_for_cliapp examples/app

res/auto-completion.zsh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#compdef examples/app
2+
# ------------------------------------------------------------------------------
3+
# FILE: auto-completion.zsh
4+
# AUTHOR: inhere (https://github.com/inhere)
5+
# VERSION: 1.0.0
6+
# DESCRIPTION: zsh shell complete for cli app: cliapp
7+
# ------------------------------------------------------------------------------
8+
# usage: source auto-completion.zsh
9+
10+
_console_get_command_list () {
11+
IFS=" "
12+
php ./examples/app --no-color | \
13+
sed "1,/Available Commands/d" | \
14+
awk '/ [a-z]+/ { print $0 }' | \
15+
sed -E 's/^[ ]+//g' | \
16+
sed -E 's/[:]+/\\:/g' | \
17+
sed -E 's/[ ]{2,}/\:/g'
18+
}
19+
20+
_console () {
21+
local -a commands
22+
IFS=$'\n'
23+
commands=(`_console_get_command_list`)
24+
# commands="$commands\nhelp:Show application help information"
25+
_describe 'commands' commands
26+
}
27+
28+
compdef _console php examples/app
29+
compdef _console examples/app
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
# ------------------------------------------------------------------------------
3+
# DATE: {{datetime}}
4+
# FILE: auto-completion.bash
5+
# AUTHOR: inhere (https://github.com/inhere)
6+
# VERSION: 1.0.0
7+
# DESCRIPTION: bash shell complete for cli app: cliapp
8+
# ------------------------------------------------------------------------------
9+
# usage: source auto-completion.bash
10+
# run 'complete' to see registered complete function.
11+
12+
_complete_for_cliapp () {
13+
local cur prev
14+
commands= "{{commands}}"
15+
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
16+
}
17+
18+
complete -F _complete_for_cliapp examples/app
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#compdef examples/app
2+
# ------------------------------------------------------------------------------
3+
# FILE: auto-completion.zsh
4+
# AUTHOR: inhere (https://github.com/inhere)
5+
# VERSION: 1.0.0
6+
# DESCRIPTION: zsh shell complete for cli app: cliapp
7+
# ------------------------------------------------------------------------------
8+
# usage: source auto-completion.zsh
9+
10+
_console_get_command_list () {
11+
IFS=" "
12+
php ./examples/app --no-color | \
13+
sed "1,/Available Commands/d" | \
14+
awk '/ [a-z]+/ { print $0 }' | \
15+
sed -E 's/^[ ]+//g' | \
16+
sed -E 's/[:]+/\\:/g' | \
17+
sed -E 's/[ ]{2,}/\:/g'
18+
}
19+
20+
_console () {
21+
local -a commands
22+
IFS=$'\n'
23+
commands=(`_console_get_command_list`)
24+
# commands="$commands\nhelp:Show application help information"
25+
_describe 'commands' commands
26+
}
27+
28+
compdef _console php examples/app
29+
compdef _console examples/app

src/AbstractApplication.php

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function __construct(array $meta = [], Input $input = null, Output $outpu
113113
$this->runtimeCheck();
114114
$this->setConfig($meta);
115115

116-
$this->input = $input ?: new Input();
116+
$this->input = $input ?: new Input();
117117
$this->output = $output ?: new Output();
118118

119119
$this->init();
@@ -149,6 +149,10 @@ public static function getInternalOptions(): array
149149

150150
protected function prepareRun()
151151
{
152+
if ($this->input->getSameOpt(['no-color'])) {
153+
Style::setNoColor();
154+
}
155+
152156
if (!$this->errorHandler) {
153157
$this->errorHandler = new ErrorHandler();
154158
}
@@ -172,7 +176,11 @@ public function run(bool $exit = true)
172176
$command = \trim($this->input->getCommand(), $this->delimiter);
173177

174178
$this->prepareRun();
175-
$this->filterSpecialCommand($command);
179+
180+
// like: help, version, list
181+
if ($this->filterSpecialCommand($command)) {
182+
return 0;
183+
}
176184

177185
// call 'onBeforeRun' service, if it is registered.
178186
$this->fire(self::ON_BEFORE_RUN, $this);
@@ -221,8 +229,8 @@ public function stop(int $code = 0)
221229

222230
// display runtime info
223231
if ($this->isProfile()) {
224-
$title = '------ Runtime Stats(use --profile) ------';
225-
$stats = $this->stats;
232+
$title = '------ Runtime Stats(use --profile) ------';
233+
$stats = $this->stats;
226234
$this->stats = PhpHelper::runtime($stats['startTime'], $stats['startMemory'], $stats);
227235
$this->output->write('');
228236
$this->output->aList($this->stats, $title);
@@ -306,37 +314,44 @@ public function handleException($e)
306314
}
307315

308316
/**
309-
* @param $command
317+
* @param string $command
318+
* @return bool True will stop run, False will goon run give command.
310319
*/
311-
protected function filterSpecialCommand(string $command)
320+
protected function filterSpecialCommand(string $command): bool
312321
{
313322
if (!$command) {
314323
if ($this->input->getSameOpt(['V', 'version'])) {
315324
$this->showVersionInfo();
325+
return true;
316326
}
317327

318328
if ($this->input->getSameOpt(['h', 'help'])) {
319329
$this->showHelpInfo();
330+
return true;
320331
}
321-
}
322332

323-
if ($this->input->getSameOpt(['no-color'])) {
324-
Style::setNoColor();
333+
// default run list command
334+
// $command = $this->defaultCommand ? 'list';
335+
$command = 'list';
336+
// is user command
337+
} elseif (!$this->isInternalCommand($command)) {
338+
return false;
325339
}
326340

327-
$command = $command ?: 'list';
328-
329341
switch ($command) {
330342
case 'help':
331-
$this->showHelpInfo(true, $this->input->getFirstArg());
343+
$this->showHelpInfo($this->input->getFirstArg());
332344
break;
333345
case 'list':
334346
$this->showCommandList();
335347
break;
336348
case 'version':
337349
$this->showVersionInfo();
338350
break;
351+
default:
352+
return false;
339353
}
354+
return true;
340355
}
341356

342357
/**
@@ -531,9 +546,9 @@ public function getRootPath(): string
531546
/**
532547
* @return array
533548
*/
534-
public static function getInternalCommands(): array
549+
public function getInternalCommands(): array
535550
{
536-
return static::$internalCommands;
551+
return \array_keys(static::$internalCommands);
537552
}
538553

539554
/**

src/Component/Style/Color.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function __construct($fg = '', $bg = '', array $options = [], bool $extra
150150
throw new \InvalidArgumentException(
151151
sprintf('Invalid foreground color "%1$s" [%2$s]',
152152
$fg,
153-
implode(', ', $this->getKnownColors())
153+
\implode(', ', $this->getKnownColors())
154154
)
155155
);
156156
}
@@ -159,11 +159,11 @@ public function __construct($fg = '', $bg = '', array $options = [], bool $extra
159159
}
160160

161161
if ($bg) {
162-
if (false === array_key_exists($bg, static::$knownColors)) {
162+
if (false === \array_key_exists($bg, static::$knownColors)) {
163163
throw new \InvalidArgumentException(
164164
sprintf('Invalid background color "%1$s" [%2$s]',
165165
$bg,
166-
implode(', ', $this->getKnownColors())
166+
\implode(', ', $this->getKnownColors())
167167
)
168168
);
169169
}
@@ -172,11 +172,11 @@ public function __construct($fg = '', $bg = '', array $options = [], bool $extra
172172
}
173173

174174
foreach ($options as $option) {
175-
if (false === array_key_exists($option, static::$knownOptions)) {
175+
if (false === \array_key_exists($option, static::$knownOptions)) {
176176
throw new \InvalidArgumentException(
177177
sprintf('Invalid option "%1$s" [%2$s]',
178178
$option,
179-
implode(', ', $this->getKnownOptions())
179+
\implode(', ', $this->getKnownOptions())
180180
)
181181
);
182182
}

src/Component/Style/Style.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public function format(string $text)
181181
return static::stripColor($text);
182182
}
183183

184-
if (!preg_match_all(self::COLOR_TAG, $text, $matches)) {
184+
if (!\preg_match_all(self::COLOR_TAG, $text, $matches)) {
185185
return $text;
186186
}
187187

src/Face/ApplicationInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ public function controller(string $name, $class = null, $option = null);
7979
*/
8080
public function command(string $name, $handler = null, $option = null);
8181

82-
public function showCommandList($quit = true);
82+
public function showCommandList();
8383

84+
/**
85+
* @return string
86+
*/
8487
public function getRootPath(): string;
8588
}

src/IO/Input.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,12 @@ public function getRequiredOpt(string $name)
350350
* @param bool $default
351351
* @return bool
352352
*/
353-
public function getBoolOpt(string $name, $default = false): bool
353+
public function getBoolOpt(string $name, bool $default = false): bool
354354
{
355355
return (bool)$this->getOpt($name, $default);
356356
}
357357

358-
public function boolOpt(string $name, $default = false): bool
358+
public function boolOpt(string $name, bool $default = false): bool
359359
{
360360
return (bool)$this->getOpt($name, $default);
361361
}

0 commit comments

Comments
 (0)