khalyomede/command-builder
Composer 安装命令:
composer require khalyomede/command-builder
包简介
Create executable strings using a fluent API.
README 文档
README
A PHP class to build executable with using fluent API.
Summary
About
I need to have a fluent way to call my executable. I did not found any other command builder providing such interface that was updated recently or provide a large test coverage.
Features
- Use a fluent API to construct the string to be executed
- Class-based
- Support arguments, long/short options and flags
- Preserves order of elements
- Does not handle executing the command
Installation
Install the package using Composer:
composer require khalyomede/command-builder
Examples
- 1. Create a simple command
- 2. Add an argument
- 3. Add a flag
- 4. Add an option
- 5. Configure the standard
- 6. Get the number of arguments
- 7. Get the number of flags
- 8. Get the number of options
- 9. Check if a flag has been added
- 10. Check if an option has been added
1. Create a simple command
In this example, we will just pass a command name, without arguments/options/flags.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); echo $command; // composer
2. Add an argument
In this example, we will add an argument to our command.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->argument("require"); echo $command; // composer require
3. Add a flag
In this example, we will add a "long" flag to the command.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->argument("require") ->longFlag("ignore-platform-reqs"); echo $command; // composer require --ignore-platform-reqs
And this is how to add a "short" flag.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->argument("require") ->flag("i"); echo $command; // composer require -i
4. Add an option
You can add options to your command.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->argument("require") ->longOption("prefer-install", "source"); echo $command; // composer require --prefer-install=source
If your option contains spaces, it will automatically be escaped using double quotes.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->argument("require") ->longOption("prefer-install", "auto source"); echo $command; // composer require --prefer-install="auto source"
And if your option contains spaces and doubles quotes, they will also be escaped.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->argument("require") ->longOption("prefer-install", 'auto "source"'); echo $command; // composer require --prefer-install="auto \"source\""
You can also use "short" option.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->argument("require") ->option("p", 'source'); echo $command; // composer require -p=source
5. Configure the standard
You can specify the standard used for the option. By default, it is set to "GNU".
use Khalyomede\CommandBuilder\Command; $command = new Command("composer", "POSIX"); $command->argument("require") ->option("p", 'source'); echo $command; // composer require -p source $command = new Command("composer") $command->argument("require") ->option("p", 'source'); echo $command; // composer require -p=source
You can also use constants if you prefer
use Khalyomede\CommandBuilder\Command; use Khalyomede\CommandBuilder\Standard; $command = new Command("composer", Standard::POSIX);
6. Get the number of arguments
You can know how many arguments were added to the command.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->argument("create-project") ->argument("laravel/laravel"); echo $command->argumentCount(); // 2
7. Get the number of flags
You can know the number of flags added to your command.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->flag("i") ->longFlag("prefer-dist"); echo $command->flagCount(); // 2
8. Get the number of options
You can know the number of options added to your command.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->option("a", "source") ->longFlag("apcu-autoloader-prefix", "app"); echo $command->optionCount(); // 2
9. Check if a flag has been added
You can know if a flag has been added already.
use Khalyomede\CommandBuilder\Command; $command = new Command("composer"); $command->longFlag("dev"); var_dump( $command->hasFlag("d", "dev") ); // bool(true) var_dump( $command->hasFlag("o", "optimize-autoloader") ); // bool(false)
10. Check if an option has been added
You can know if an option has been added or not.
use Khalyomede\CommandBuilder\Command; use Khalyomede\CommandBuilder\Style; $command = new Command("composer"); $command->longOption("prefer-install", "source"); var_dump( $command->hasOption("p", "prefer-install") ); // bool(true) var_dump( $command->hasOption("i", "ignore-platform-req") ); // bool(false)
Compatibility table
This is the compatibility for this version only. To check the compatibility with other version of this package, please browse the version of your choice.
| PHP Version | Compatibility |
|---|---|
| 8.1.* | ✔️ |
| 8.0.* | ✔️ |
| 7.4.* | ✔️ |
Tests
composer run test
composer run mutate
composer run analyse
composer run lint
composer run install-security-checker
composer run check-security
composer run check-updates
khalyomede/command-builder 适用场景与选型建议
khalyomede/command-builder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 173 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「fluent」 「command」 「builder」 「executable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 khalyomede/command-builder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 khalyomede/command-builder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 khalyomede/command-builder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Wrapper for exec and it's results
Symfony bundle to monitor and execute commands
Shell command module for PHP.
Presentation and Formatting helper with nice fluent interface.
A Sphinx Query Builder extension for the Laravel Database package
Anax Database Active Record module for model classes.
统计信息
- 总下载量: 173
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-01-18