Skip to content

Commit beedbf7

Browse files
committed
feat: add a simple cli command app implements
1 parent 93c4f06 commit beedbf7

File tree

7 files changed

+755
-85
lines changed

7 files changed

+755
-85
lines changed

.github/changelog.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
options:
2+
title: '## Chanage Log'
3+
style: gh-release
4+
filters:
5+
# message length >= 12
6+
- name: msgLen
7+
minLen: 12
8+
# message words >= 3
9+
- name: wordsLen
10+
minLen: 3
11+
- name: keywords
12+
keywords: ['format code']
13+
exclude: true
14+
15+
# not matched will use 'Other' group.
16+
groups:
17+
- name: New
18+
keywords: [add, new]
19+
- name: Fixed
20+
startWiths: [add, new]
21+
keywords: [add, new]
22+
- name: Feat
23+
startWiths: [feat]
24+
keywords: [feature]
25+
- name: Update
26+
startWiths: [update, 'up:']
27+
keywords: []

example/liteApp

-68
This file was deleted.

example/mycmd

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
use Toolkit\Cli\CliApp;
5+
6+
define('BASE_PATH', dirname(__DIR__));
7+
8+
require dirname(__DIR__) . '/test/bootstrap.php';
9+
10+
// run:
11+
// php example/mycmd
12+
// php example/mycmd -i abc --lon def ag1 ag2 ag3
13+
$cmd = CliApp::new('cmd1', 'this is my cli application');
14+
$cmd->addOpt('info', 'i', 'Output some information');
15+
$cmd->addOpt('long-option-name', 'lon', 'this is a long option for command');
16+
$cmd->addArg('arg1', 'this is first argument');
17+
18+
$cmd->setHandler(function (CliApp $cmd) {
19+
var_dump($cmd->getOpts(), $cmd->getArgs(), $cmd->getRemainArgs());
20+
});
21+
22+
$cmd->run();
23+

src/Cli.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
use RuntimeException;
1313
use Toolkit\Cli\Color\Alert;
14-
use Toolkit\Cli\Color\Prompt;
1514
use Toolkit\Cli\Color\ColorTag;
15+
use Toolkit\Cli\Color\Prompt;
1616
use Toolkit\Cli\Traits\ReadMessageTrait;
1717
use Toolkit\Cli\Traits\WriteMessageTrait;
18+
use function array_shift;
1819
use function count;
1920
use function date;
2021
use function defined;
@@ -26,8 +27,10 @@
2627
use function json_encode;
2728
use function preg_replace;
2829
use function sprintf;
30+
use function str_ends_with;
2931
use function stream_isatty;
3032
use function strtoupper;
33+
use function substr;
3134
use function trim;
3235
use const DIRECTORY_SEPARATOR;
3336
use const JSON_PRETTY_PRINT;
@@ -44,18 +47,25 @@
4447
* @method static prompt(string|array|mixed $message, string $style = 'info')
4548
*
4649
* @method static red(string ...$message) Print red color message line.
50+
* @method static redf(string $format, ...$args) Print red color message, use like sprintf.
4751
* @method static cyan(string ...$message) Print cyan color message line.
52+
* @method static cyanf(string $format, ...$args) Print cyan color message, use like sprintf.
4853
* @method static blue(string ...$message) Print blue color message line.
4954
* @method static green(string ...$message) Print green color message line.
5055
* @method static magenta(string ...$message) Print cyan color message line.
5156
* @method static yellow(string ...$message) Print yellow color message line.
5257
*
5358
* @method static error(string ...$message) Print error style message line.
59+
* @method static errorf(string $format, ...$args) Print error style message, use like sprintf.
5460
* @method static warn(string ...$message) Print warn style message line.
61+
* @method static warnf(string $format, ...$args) Print warn style message, use like sprintf.
5562
* @method static info(string ...$message) Print info style message line.
63+
* @method static infof(string $format, ...$args) Print info style message, use like sprintf.
5664
* @method static note(string ...$message) Print note style message line.
65+
* @method static notef(string $format, ...$args) Print note style message, use like sprintf.
5766
* @method static notice(string ...$message) Print notice style message line.
5867
* @method static success(string ...$message) Print success style message line.
68+
* @method static successf(string $format, ...$args) Print success style message, use like sprintf.
5969
*/
6070
class Cli
6171
{
@@ -83,6 +93,18 @@ public static function __callStatic(string $method, array $args): void
8393
return;
8494
}
8595

96+
// use like sprintf
97+
if (str_ends_with($method, 'f') ) {
98+
$realName = substr($method, 0, -1);
99+
100+
if (isset(Color::STYLES[$realName]) && count($args) > 1) {
101+
$fmt = (string)array_shift($args);
102+
$msg = count($args) > 1 ? sprintf($fmt, ...$args) : $fmt;
103+
echo Color::render($msg, $realName);
104+
return;
105+
}
106+
}
107+
86108
throw new RuntimeException('call unknown method: ' . $method);
87109
}
88110

@@ -147,7 +169,7 @@ public static function color(string $text, array|string $style = null): string
147169
* @param array $data
148170
* @param string $type
149171
* @param array{writeOpts:array} $labels
150-
* @deprecated please use Clog::info();
172+
* @deprecated please use Util\Clog::log()
151173
*/
152174
public static function clog(string $msg, array $data = [], string $type = 'info', array $labels = []): void
153175
{

0 commit comments

Comments
 (0)