Skip to content

Commit 7a99f46

Browse files
committed
update: add new methods for cli cmd,app
1 parent b1099ef commit 7a99f46

File tree

4 files changed

+91
-28
lines changed

4 files changed

+91
-28
lines changed

example/cliapp.php

+11-10
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
// run demo:
1111
// php example/cliapp.php
1212

13-
$app = new CliApp();
14-
15-
$app->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
16-
'desc' => 'the test 1 command',
17-
'options' => [
18-
'opt1' => 'opt1 for command test1',
19-
'opt2' => 'int;opt2 for command test1',
20-
],
21-
]);
13+
$app = CliApp::newWith(function (CliApp $app) {
14+
$app->setName('myApp');
15+
})
16+
->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
17+
'desc' => 'the test 1 command',
18+
'options' => [
19+
'opt1' => 'opt1 for command test1',
20+
'opt2' => 'int;opt2 for command test1',
21+
],
22+
]);
2223

2324
$app->add('test2', function (FlagsParser $fs) {
2425
Cli::info('options:');
@@ -27,7 +28,7 @@
2728
vdump($fs->getArgs());
2829
}, [
2930
// 'desc' => 'the test2 command',
30-
'options' => [
31+
'options' => [
3132
'opt1' => 'string;a string opt1 for command test2, and is required;true',
3233
'opt2' => 'int;a int opt2 for command test2',
3334
],

example/clicmd.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
// php example/clicmd.php --name inhere value1
1212
// php example/clicmd.php --age 23 --name inhere value1
1313

14-
CliCmd::new()
15-
->config(function (CliCmd $cmd) {
16-
$cmd->name = 'demo';
17-
$cmd->desc = 'description for demo command';
14+
CliCmd::newWith(function (CliCmd $cmd) {
15+
$cmd->name = 'demo';
16+
$cmd->desc = 'description for demo command';
1817

19-
// config flags
20-
$cmd->options = [
21-
'age, a' => 'int;the option age, is int',
22-
'name, n' => 'the option name, is string and required;true',
23-
'tags, t' => 'array;the option tags, is array',
24-
];
25-
// or use property
26-
// $cmd->arguments = [...];
27-
})
18+
// config flags
19+
$cmd->options = [
20+
'age, a' => 'int;the option age, is int',
21+
'name, n' => 'the option name, is string and required;true',
22+
'tags, t' => 'array;the option tags, is array',
23+
];
24+
25+
// or use property
26+
// $cmd->arguments = [...];
27+
})
2828
->withArguments([
2929
'arg1' => 'this is arg1, is string'
3030
])

src/CliApp.php

+45-5
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ public static function setGlobal(self $global): void
138138
self::$global = $global;
139139
}
140140

141+
/**
142+
* @param callable(self): void $fn
143+
*
144+
* @return $this
145+
*/
146+
public static function newWith(callable $fn): self
147+
{
148+
return (new self)->config($fn);
149+
}
150+
141151
/**
142152
* Class constructor.
143153
*
@@ -158,6 +168,17 @@ public function __construct(array $config = [])
158168
$this->flags->setAutoBindArgs(false);
159169
}
160170

171+
/**
172+
* @param callable(self): void $fn
173+
*
174+
* @return $this
175+
*/
176+
public function config(callable $fn): self
177+
{
178+
$fn($this);
179+
return $this;
180+
}
181+
161182
/**
162183
* @param FlagsParser $fs
163184
*/
@@ -358,16 +379,20 @@ public static function handleException(Throwable $e): int
358379
* @param string $command
359380
* @param callable $handler
360381
* @param array{desc:string,options:array,arguments:array} $config
382+
*
383+
* @return self
361384
*/
362-
public function add(string $command, callable $handler, array $config = []): void
385+
public function add(string $command, callable $handler, array $config = []): self
363386
{
364-
$this->addCommand($command, $handler, $config);
387+
return $this->addCommand($command, $handler, $config);
365388
}
366389

367390
/**
368391
* @param class-string|CmdHandlerInterface $handler
392+
*
393+
* @return self
369394
*/
370-
public function addHandler(string|CmdHandlerInterface $handler): void
395+
public function addHandler(string|CmdHandlerInterface $handler): self
371396
{
372397
if (is_string($handler)) {
373398
// class string.
@@ -382,15 +407,19 @@ public function addHandler(string|CmdHandlerInterface $handler): void
382407
$config = $handler->metadata();
383408
$command = Valid::arrayHasNoEmptyKey($config, 'name');
384409

385-
$this->addCommand($command, $handler, $config);
410+
return $this->addCommand($command, $handler, $config);
386411
}
387412

388413
/**
414+
* Add a command
415+
*
389416
* @param string $command
390417
* @param callable|object|class-string $handler
391418
* @param array{desc:string,options:array,arguments:array} $config
419+
*
420+
* @return self
392421
*/
393-
public function addCommand(string $command, callable|object|string $handler, array $config = []): void
422+
public function addCommand(string $command, callable|object|string $handler, array $config = []): self
394423
{
395424
if (!$command) {
396425
throw new InvalidArgumentException('command name can not be empty');
@@ -418,6 +447,7 @@ public function addCommand(string $command, callable|object|string $handler, arr
418447
}
419448

420449
$this->metadata[$command] = array_merge(self::COMMAND_CONFIG, $config);
450+
return $this;
421451
}
422452

423453
/**
@@ -628,6 +658,16 @@ public function setParams(array $params): void
628658
}
629659
}
630660

661+
/**
662+
* @param string $name
663+
*
664+
* @return void
665+
*/
666+
public function setName(string $name): void
667+
{
668+
$this->params['name'] = $name;
669+
}
670+
631671
/**
632672
* @return FlagsParser
633673
*/

src/CliCmd.php

+22
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ class CliCmd
2020
public string $name = '';
2121
public string $desc = 'command description';
2222

23+
/**
24+
* @var array<string, string|array>
25+
*/
2326
public array $options = [];
27+
28+
/**
29+
* @var array<string, string|array>
30+
*/
2431
public array $arguments = [];
2532

2633
/**
@@ -33,6 +40,21 @@ class CliCmd
3340
*/
3441
private $handler;
3542

43+
/**
44+
* @param callable(self): void $fn
45+
*
46+
* @return $this
47+
*/
48+
public static function newWith(callable $fn): self
49+
{
50+
return (new self)->config($fn);
51+
}
52+
53+
/**
54+
* Class constructor.
55+
*
56+
* @param array $config
57+
*/
3658
public function __construct(array $config = [])
3759
{
3860
$this->supper($config);

0 commit comments

Comments
 (0)