99namespace Inhere \Console \Concern ;
1010
1111use Inhere \Console \AbstractHandler ;
12- use Toolkit \Cli \Style ;
1312use Inhere \Console \Console ;
1413use Inhere \Console \Contract \CommandInterface ;
1514use Inhere \Console \IO \Input ;
1817use Inhere \Console \Util \FormatUtil ;
1918use Inhere \Console \Util \Show ;
2019use Toolkit \Cli \ColorTag ;
20+ use Toolkit \Cli \Style ;
2121use function array_merge ;
22+ use function basename ;
2223use function date ;
2324use function dirname ;
2425use function file_get_contents ;
@@ -68,7 +69,7 @@ public function showVersionInfo(): void
6869 /** @var Output $out */
6970 $ out = $ this ->output ;
7071 $ out ->aList ([
71- "$ logo \n <info> { $ name} </info>, Version <comment> $ version</comment> \n" ,
72+ "$ logo \n <info> $ name</info>, Version <comment> $ version</comment> \n" ,
7273 'System Info ' => "PHP version <info> $ phpVersion</info>, on <info> $ os</info> system " ,
7374 'Application Info ' => "Update at <info> $ updateAt</info>, publish at <info> $ publishAt</info>(current $ date) " ,
7475 ], '' , [
@@ -100,26 +101,37 @@ public function showHelpInfo(string $command = ''): void
100101 $ binName = $ in ->getScriptName ();
101102
102103 // built in options
103- $ globalOptions = FormatUtil::alignOptions (self ::$ globalOptions );
104+ $ globalOptions = self ::$ globalOptions ;
105+ // append generate options:
106+ // php examples/app --auto-completion --shell-env zsh --gen-file
107+ // php examples/app --auto-completion --shell-env zsh --gen-file stdout
108+ if ($ this ->isDebug ()) {
109+ $ globalOptions ['--auto-completion ' ] = 'Open generate auto completion script ' ;
110+ $ globalOptions ['--shell-env ' ] = 'The shell env name for generate auto completion script ' ;
111+ $ globalOptions ['--gen-file ' ] = 'The output file for generate auto completion script ' ;
112+ }
113+
114+ $ globalOptions = FormatUtil::alignOptions ($ globalOptions );
104115
105116 /** @var Output $out */
106117 $ out = $ this ->output ;
107118 $ out ->helpPanel ([
108- 'Usage ' => "$ binName <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...] " ,
119+ 'Usage ' => "$ binName <info>{command}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...] " ,
109120 'Options ' => $ globalOptions ,
110- 'Example ' => [
121+ 'Example ' => [
111122 "$ binName test run a independent command " ,
112123 "$ binName home index run a sub-command of the group " ,
113124 sprintf ("$ binName home%sindex run a sub-command of the group " , $ delimiter ),
114125 "$ binName help {command} see a command help information " ,
115126 "$ binName home index -h see a sub-command help of the group " ,
116127 sprintf ("$ binName home%sindex -h see a sub-command help of the group " , $ delimiter ),
117128 ],
118- 'Help ' => [
129+ 'Help ' => [
119130 'Generate shell auto completion scripts: ' ,
120131 " <info> $ binName --auto-completion --shell-env [zsh|bash] [--gen-file stdout]</info> " ,
121132 ' eg: ' ,
122133 " $ binName --auto-completion --shell-env bash --gen-file stdout " ,
134+ " $ binName --auto-completion --shell-env zsh --gen-file stdout " ,
123135 " $ binName --auto-completion --shell-env bash --gen-file myapp.sh " ,
124136 ],
125137 ]);
@@ -139,7 +151,7 @@ public function showCommandList(): void
139151
140152 // php bin/app list --only-name
141153 if ($ autoComp && $ shellEnv === 'bash ' ) {
142- $ this ->dumpAutoCompletion ($ shellEnv , []);
154+ $ this ->dumpAutoCompletion (' bash ' , []);
143155 return ;
144156 }
145157
@@ -233,11 +245,11 @@ public function showCommandList(): void
233245 $ scriptName = $ this ->getScriptName ();
234246
235247 // built in options
236- $ internalOptions = FormatUtil:: alignOptions ( self ::$ globalOptions) ;
248+ $ globOpts = self ::$ globalOptions ;
237249
238250 Show::mList ([
239251 'Usage: ' => "$ scriptName <info>{COMMAND}</info> [--opt -v -h ...] [arg0 arg1 arg2=value2 ...] " ,
240- 'Options: ' => $ internalOptions ,
252+ 'Options: ' => FormatUtil:: alignOptions ( $ globOpts ) ,
241253 'Internal Commands: ' => $ internalCommands ,
242254 'Available Commands: ' => array_merge ($ groupArr , $ commandArr ),
243255 ], [
@@ -273,27 +285,31 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
273285
274286 // info
275287 $ glue = ' ' ;
276- $ genFile = $ input ->getStringOpt ('gen-file ' );
277- $ filename = 'auto-completion. ' . $ shellEnv ;
288+ $ genFile = $ input ->getStringOpt ('gen-file ' , 'none ' );
278289 $ tplDir = dirname (__DIR__ , 2 ) . '/resource/templates ' ;
279290
280291 if ($ shellEnv === 'bash ' ) {
281292 $ tplFile = $ tplDir . '/bash-completion.tpl ' ;
282- $ list = array_merge ($ router ->getCommandNames (), $ router ->getControllerNames (),
283- $ this ->getInternalCommands ());
293+
294+ $ list = array_merge (
295+ $ router ->getCommandNames (),
296+ $ router ->getControllerNames (),
297+ $ this ->getInternalCommands ()
298+ );
284299 } else {
285- $ glue = PHP_EOL ;
286- $ list = [];
287300 $ tplFile = $ tplDir . '/zsh-completion.tpl ' ;
301+
302+ $ glue = PHP_EOL ;
303+ $ list = [];
288304 foreach ($ data as $ name => $ desc ) {
289305 $ list [] = $ name . ': ' . str_replace (': ' , '\: ' , $ desc );
290306 }
291307 }
292308
293309 $ commands = implode ($ glue , $ list );
294310
295- // dump to stdout.
296- if (! $ genFile ) {
311+ // only dump commands to stdout.
312+ if ($ genFile === ' none ' ) {
297313 $ output ->write ($ commands , true , false , ['color ' => false ]);
298314 return ;
299315 }
@@ -303,7 +319,19 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
303319 $ commands = Style::stripColor ($ commands );
304320 }
305321
306- // dump at script file
322+ $ toStdout = $ genFile === 'stdout ' ;
323+ $ filename = 'auto-completion. ' . $ shellEnv ;
324+ if (!$ toStdout ) {
325+ if ($ genFile === 'true ' ) {
326+ $ targetFile = $ input ->getPwd () . '/ ' . $ filename ;
327+ } else {
328+ $ filename = basename ($ genFile );
329+ // $targetDir = dirname($genFile);
330+ $ targetFile = $ genFile ;
331+ }
332+ }
333+
334+ // dump to script file
307335 $ binName = $ input ->getBinName ();
308336 $ tplText = file_get_contents ($ tplFile );
309337 $ content = strtr ($ tplText , [
@@ -315,19 +343,18 @@ protected function dumpAutoCompletion(string $shellEnv, array $data): void
315343 '{{fmtBinName}} ' => str_replace ('/ ' , '_ ' , $ binName ),
316344 ]);
317345
318- // dump to stdout
319- if ($ genFile === ' stdout ' ) {
346+ // dump script contents to stdout
347+ if ($ toStdout ) {
320348 file_put_contents ('php://stdout ' , $ content );
321349 return ;
322350 }
323351
324- $ targetFile = $ input ->getPwd () . '/ ' . $ filename ;
325352 $ output ->write (['Target File: ' , $ targetFile , '' ]);
326353
327354 if (file_put_contents ($ targetFile , $ content ) > 10 ) {
328- $ output ->success ("O_O! Generate $ filename successful! " );
355+ $ output ->success ("O_O! Generate file: $ filename successful! " );
329356 } else {
330- $ output ->error ("O^O! Generate $ filename failure! " );
357+ $ output ->error ("O^O! Generate file: $ filename failure! " );
331358 }
332359 }
333360}
0 commit comments