Skip to content

Commit 2987de9

Browse files
committed
style: update readme, remove some unused codes
1 parent 36903c1 commit 2987de9

File tree

5 files changed

+132
-60
lines changed

5 files changed

+132
-60
lines changed

.github/workflows/php.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ jobs:
2323
- description: 'Log Code Coverage'
2424
php: '8.0'
2525
coverage: 'xdebug'
26-
# include: # will not testing on php 7.2
27-
# - os: 'ubuntu-latest'
28-
# php: '7.2'
29-
# phpunit: '8.5.13'
26+
# will not testing on php 7.2
27+
- os: 'ubuntu-latest'
28+
php: '7.2'
29+
phpunit: '8.5.13'
3030

3131
steps:
3232
- name: Checkout

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ php example/clicmd.php --age 23 --name inhere value1
284284

285285
### Create an multi commands app
286286

287-
Build and run a simple command handler. see example file [example/cliapp.php](example/cliapp.php)
287+
Create an multi commands application, run subcommand. see example file [example/cliapp.php](example/cliapp.php)
288288

289289
```php
290290
use Toolkit\Cli\Cli;

README.zh-CN.md

+119
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,125 @@ $arrArg = $fs->getArg('arrArg'); // array{"arr0", "arr1"}
354354

355355
-----------
356356

357+
## 创建简单的独立命令或应用程序
358+
359+
在 pflag 中,内置了 `CliApp``CliCmd` 两个独立类,用于快速创建和运行一个简单的控制台应用程序。
360+
361+
### 创建简单的单独命令
362+
363+
使用 `CliCmd` 可以方便的构建并运行一个简单的命令处理程序。查看示例文件 [example/clicmd.php](example/clicmd.php)
364+
365+
```php
366+
use Toolkit\Cli\Cli;
367+
use Toolkit\PFlag\CliCmd;
368+
use Toolkit\PFlag\FlagsParser;
369+
370+
CliCmd::new()
371+
->config(function (CliCmd $cmd) {
372+
$cmd->name = 'demo';
373+
$cmd->desc = 'description for demo command';
374+
375+
// config flags
376+
$cmd->options = [
377+
'age, a' => 'int;the option age, is int',
378+
'name, n' => 'the option name, is string and required;true',
379+
'tags, t' => 'array;the option tags, is array',
380+
];
381+
// or use property
382+
// $cmd->arguments = [...];
383+
})
384+
->withArguments([
385+
'arg1' => 'this is arg1, is string'
386+
])
387+
->setHandler(function (FlagsParser $fs) {
388+
Cli::info('options:');
389+
vdump($fs->getOpts());
390+
Cli::info('arguments:');
391+
vdump($fs->getArgs());
392+
})
393+
->run();
394+
```
395+
396+
**使用:**
397+
398+
```php
399+
# show help
400+
php example/clicmd.php -h
401+
# run command
402+
php example/clicmd.php --age 23 --name inhere value1
403+
```
404+
405+
- 显示帮助:
406+
407+
![cmd-demo-help](example/images/cli-cmd-help.png)
408+
409+
- 运行命令:
410+
411+
![cmd-demo-run](example/images/cli-cmd-run.png)
412+
413+
### Create an multi commands app
414+
415+
Create an multi commands application, run subcommand. see example file [example/cliapp.php](example/cliapp.php)
416+
417+
```php
418+
use Toolkit\Cli\Cli;
419+
use Toolkit\PFlag\CliApp;
420+
use Toolkit\PFlag\FlagsParser;
421+
422+
$app = new CliApp();
423+
424+
$app->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
425+
'desc' => 'the test 1 command',
426+
'options' => [
427+
'opt1' => 'opt1 for command test1',
428+
'opt2' => 'int;opt2 for command test1',
429+
],
430+
]);
431+
432+
$app->add('test2', function (FlagsParser $fs) {
433+
Cli::info('options:');
434+
vdump($fs->getOpts());
435+
Cli::info('arguments:');
436+
vdump($fs->getArgs());
437+
}, [
438+
// 'desc' => 'the test2 command',
439+
'options' => [
440+
'opt1' => 'a string opt1 for command test2',
441+
'opt2' => 'int;a int opt2 for command test2',
442+
],
443+
'arguments' => [
444+
'arg1' => 'required arg1 for command test2;true',
445+
]
446+
]);
447+
448+
$app->add('show-err', fn() => throw new RuntimeException('test show exception'));
449+
450+
$app->run();
451+
```
452+
453+
**使用:**
454+
455+
```php
456+
# show help
457+
php example/cliapp.php -h
458+
# run command
459+
php example/cliapp.php test2 --opt1 val1 --opt2 23 value1
460+
```
461+
462+
- 显示帮助,命令列表:
463+
464+
![cli-app-help](example/images/cli-app-help.png)
465+
466+
- 显示子命令帮助:
467+
468+
![cli-app-cmd-help](example/images/cli-app-cmd-help.png)
469+
470+
- 运行一个命令:
471+
472+
![cli-app-cmd-run](example/images/cli-app-cmd-run.png)
473+
474+
-----------
475+
357476
## 扩展:规则定义
358477

359478
选项参数规则。使用规则可以快速定义一个选项或参数。

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
],
1313
"require": {
14-
"php": ">7.1.0",
14+
"php": ">7.2.0",
1515
"ext-mbstring": "*",
1616
"toolkit/cli-utils":"~1.0",
1717
"toolkit/stdlib":"~1.0"

src/CliApp.php

+7-54
Original file line numberDiff line numberDiff line change
@@ -433,68 +433,19 @@ public function displayCommands(string $err = ''): void
433433
Cli::println("<red>ERROR</red>: $err\n");
434434
}
435435

436-
$script = $this->scriptName;
437-
438-
$help = "<comment>Commands:</comment>\n";
436+
$help = "<ylw>Commands:</ylw>\n";
439437
$data = $this->metadata;
440-
ksort($data);
441-
442-
// $globalOptions = $this->flags->getOptsHelpLines();
443-
// Cli::println($globalOptions);
444438

439+
ksort($data);
445440
foreach ($data as $command => $item) {
446441
$command = str_pad($command, $this->keyWidth);
447442

448443
$desc = $item['desc'] ? ucfirst($item['desc']) : 'No description for the command';
449444
$help .= " <green>$command</green> $desc\n";
450445
}
451446

452-
$help .= "\nFor command usage please run: <cyan>$script COMMAND -h</cyan>";
453-
454-
Cli::println($help);
455-
}
456-
457-
/**
458-
* @param string $name
459-
*/
460-
public function displayCommandHelp(string $name): void
461-
{
462-
$checkVar = false;
463-
$fullCmd = $this->scriptFile . " $name";
464-
465-
$config = $this->metadata[$name] ?? [];
466-
$usage = "$fullCmd [args ...] [--opts ...]";
467-
468-
if (!$config) {
469-
$nodes = [
470-
'No description for the command',
471-
"<comment>Usage:</comment> \n $usage"
472-
];
473-
} else {
474-
$checkVar = true;
475-
$userHelp = rtrim($config['help'], "\n");
476-
477-
$usage = $config['usage'] ?: $usage;
478-
$nodes = [
479-
ucfirst($config['desc']),
480-
"<comment>Usage:</comment> \n $usage\n",
481-
$userHelp ? $userHelp . "\n" : ''
482-
];
483-
}
484-
485-
$help = implode("\n", $nodes);
486-
487-
if ($checkVar && strpos($help, '{{')) {
488-
$vars = [
489-
'command' => $name,
490-
'fullCmd' => $fullCmd,
491-
'workDir' => $this->pwd,
492-
'pwdDir' => $this->pwd,
493-
'script' => $this->scriptFile,
494-
];
495-
496-
$help = Str::renderTemplate($help, $vars, '${%s}');
497-
}
447+
$script = $this->scriptName;
448+
$help .= "\nFor command usage please run: <cyan>$script COMMAND -h</cyan>";
498449

499450
Cli::println($help);
500451
}
@@ -629,7 +580,9 @@ public function getParams(): array
629580
*/
630581
public function setParams(array $params): void
631582
{
632-
$this->params = array_merge($this->params, $params);
583+
if ($params) {
584+
$this->params = array_merge($this->params, $params);
585+
}
633586
}
634587

635588
/**

0 commit comments

Comments
 (0)