Skip to content

Commit

Permalink
update: add new methods for cli cmd,app
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 14, 2021
1 parent b1099ef commit 7a99f46
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 28 deletions.
21 changes: 11 additions & 10 deletions example/cliapp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@
// run demo:
// php example/cliapp.php

$app = new CliApp();

$app->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
'desc' => 'the test 1 command',
'options' => [
'opt1' => 'opt1 for command test1',
'opt2' => 'int;opt2 for command test1',
],
]);
$app = CliApp::newWith(function (CliApp $app) {
$app->setName('myApp');
})
->add('test1', fn(FlagsParser $fs) => vdump($fs->getOpts()), [
'desc' => 'the test 1 command',
'options' => [
'opt1' => 'opt1 for command test1',
'opt2' => 'int;opt2 for command test1',
],
]);

$app->add('test2', function (FlagsParser $fs) {
Cli::info('options:');
Expand All @@ -27,7 +28,7 @@
vdump($fs->getArgs());
}, [
// 'desc' => 'the test2 command',
'options' => [
'options' => [
'opt1' => 'string;a string opt1 for command test2, and is required;true',
'opt2' => 'int;a int opt2 for command test2',
],
Expand Down
26 changes: 13 additions & 13 deletions example/clicmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
// php example/clicmd.php --name inhere value1
// php example/clicmd.php --age 23 --name inhere value1

CliCmd::new()
->config(function (CliCmd $cmd) {
$cmd->name = 'demo';
$cmd->desc = 'description for demo command';
CliCmd::newWith(function (CliCmd $cmd) {
$cmd->name = 'demo';
$cmd->desc = 'description for demo command';

// config flags
$cmd->options = [
'age, a' => 'int;the option age, is int',
'name, n' => 'the option name, is string and required;true',
'tags, t' => 'array;the option tags, is array',
];
// or use property
// $cmd->arguments = [...];
})
// config flags
$cmd->options = [
'age, a' => 'int;the option age, is int',
'name, n' => 'the option name, is string and required;true',
'tags, t' => 'array;the option tags, is array',
];

// or use property
// $cmd->arguments = [...];
})
->withArguments([
'arg1' => 'this is arg1, is string'
])
Expand Down
50 changes: 45 additions & 5 deletions src/CliApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ public static function setGlobal(self $global): void
self::$global = $global;
}

/**
* @param callable(self): void $fn
*
* @return $this
*/
public static function newWith(callable $fn): self
{
return (new self)->config($fn);
}

/**
* Class constructor.
*
Expand All @@ -158,6 +168,17 @@ public function __construct(array $config = [])
$this->flags->setAutoBindArgs(false);
}

/**
* @param callable(self): void $fn
*
* @return $this
*/
public function config(callable $fn): self
{
$fn($this);
return $this;
}

/**
* @param FlagsParser $fs
*/
Expand Down Expand Up @@ -358,16 +379,20 @@ public static function handleException(Throwable $e): int
* @param string $command
* @param callable $handler
* @param array{desc:string,options:array,arguments:array} $config
*
* @return self
*/
public function add(string $command, callable $handler, array $config = []): void
public function add(string $command, callable $handler, array $config = []): self
{
$this->addCommand($command, $handler, $config);
return $this->addCommand($command, $handler, $config);
}

/**
* @param class-string|CmdHandlerInterface $handler
*
* @return self
*/
public function addHandler(string|CmdHandlerInterface $handler): void
public function addHandler(string|CmdHandlerInterface $handler): self
{
if (is_string($handler)) {
// class string.
Expand All @@ -382,15 +407,19 @@ public function addHandler(string|CmdHandlerInterface $handler): void
$config = $handler->metadata();
$command = Valid::arrayHasNoEmptyKey($config, 'name');

$this->addCommand($command, $handler, $config);
return $this->addCommand($command, $handler, $config);
}

/**
* Add a command
*
* @param string $command
* @param callable|object|class-string $handler
* @param array{desc:string,options:array,arguments:array} $config
*
* @return self
*/
public function addCommand(string $command, callable|object|string $handler, array $config = []): void
public function addCommand(string $command, callable|object|string $handler, array $config = []): self
{
if (!$command) {
throw new InvalidArgumentException('command name can not be empty');
Expand Down Expand Up @@ -418,6 +447,7 @@ public function addCommand(string $command, callable|object|string $handler, arr
}

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

/**
Expand Down Expand Up @@ -628,6 +658,16 @@ public function setParams(array $params): void
}
}

/**
* @param string $name
*
* @return void
*/
public function setName(string $name): void
{
$this->params['name'] = $name;
}

/**
* @return FlagsParser
*/
Expand Down
22 changes: 22 additions & 0 deletions src/CliCmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ class CliCmd
public string $name = '';
public string $desc = 'command description';

/**
* @var array<string, string|array>
*/
public array $options = [];

/**
* @var array<string, string|array>
*/
public array $arguments = [];

/**
Expand All @@ -33,6 +40,21 @@ class CliCmd
*/
private $handler;

/**
* @param callable(self): void $fn
*
* @return $this
*/
public static function newWith(callable $fn): self
{
return (new self)->config($fn);
}

/**
* Class constructor.
*
* @param array $config
*/
public function __construct(array $config = [])
{
$this->supper($config);
Expand Down

0 comments on commit 7a99f46

Please sign in to comment.