arnapou/console
最新稳定版本:v1.0.1
Composer 安装命令:
composer require arnapou/console
包简介
Library - Minimalist PHP framework to build CLI applications.
README 文档
README
Minimalist PHP framework to build CLI applications.
Installation
composer require arnapou/console
packagist 👉️ arnapou/console
Introduction
Main ideas:
- why bother with definitions when you have type-hinting and phpDoc ?
- explicit command-line arguments
- open (interfaces everywhere, you can hack and change almost all)
- KISS (Keep it simple, stupid!")
Type-hinting and phpDoc
phpDoc is used to render the help, only when needed: zero overhead. Near 100% of the time, when we execute our commands, we don't need the doc.
Unlike Symfony console or other similar projects, we don't check the "definition" before running the command: extra flags or parameters are just ignored.
use Arnapou\Console\Application;
use Arnapou\Console\Command\ArrayCommandLoader;
$loader = new ArrayCommandLoader();
$loader->addCommand(
'dummy',
/**
* My dummy command.
*
* @param int $number Dummy parameter
*/
function (int $number): void {
echo "dummy parameter = '$number' \n";
}
);
$app = new Application($loader);
$app->run();
// $ php <file.php> dummy --help
// My dummy command.
//
// Usage:
// dummy [parameters] [flags]
//
// Parameters:
// number=int Dummy parameter
//
// Flags:
// --colors Force the use of ansi colors, --no-colors to disable
// --debug Print error, warning, success, info, debug
// --info Print error, warning, success, info (default)
// --success Print error, warning, success
// --warning Print error, warning
// --error Print error
// --quiet Print nothing but exceptions
// --help Show help
Explicit command-line arguments
I couldn't find a proper RFC-like around command-line arguments and options.
The closest I could find is around GNU and POSIX, but they have the disadvantages of being too versatile, not enough explicit to my point of view.
Thus, I introduce 2 notions.
Parameters
Inspired from URI query string on the pattern name=value.
👉️ They are injected in the command parameters.
Case of lists
If you repeat a same parameter, it automatically means that this is a list.
Example:
foo=1 foo=2 foo=3will be parsed asfoo: ["1", "2", "3"]
Case of array
You can use a "path" notation with a dot as separator to express an array.
Example:
user.name=John user.age=20will be parsed asuser: {"name": "John", "age": "20"}Tip: it reaches its full power when used with DTOs or Value Objects in the command parameter.
Flags
Boolean values on the patterns --flag (true) or --no-flag (false).
👉️ They can be used by any object which need them (TerminalInterface, HelpInterface, ...) to change their behaviour.
Note: you may use them into the command too, if you inject a ArgumentsInterface parameter in your command function signature.
Examples
- Basic commands with
Closureandcallableobject - Injected DTO to show how commands can use DTOs
- PSR-11 container which can retrieve commands dynamically
- PSR-11 container which can inject services into the command
- Single command is possible by setting a default command name
Php versions
| Date | Ref | 8.5 | 8.4 | 8.3 |
|---|---|---|---|---|
| 25/10/2025 | 1.0.x, main | × | × | × |
统计信息
- 总下载量: 45
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-05