{"id":20441727,"url":"https://github.com/mix-php/cli","last_synced_at":"2026-03-04T23:32:23.252Z","repository":{"id":57017804,"uuid":"392643396","full_name":"mix-php/cli","owner":"mix-php","description":"PHP CLI Interactive Commander / PHP 命令行交互指挥官","archived":false,"fork":false,"pushed_at":"2022-06-01T13:57:20.000Z","size":31,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-20T12:47:15.692Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mix-php.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-04T10:19:35.000Z","updated_at":"2024-12-16T14:56:57.000Z","dependencies_parsed_at":"2022-08-22T11:31:37.287Z","dependency_job_id":null,"html_url":"https://github.com/mix-php/cli","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/mix-php/cli","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fcli","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fcli/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fcli/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fcli/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mix-php","download_url":"https://codeload.github.com/mix-php/cli/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mix-php%2Fcli/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30099412,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-04T23:31:22.529Z","status":"ssl_error","status_checked_at":"2026-03-04T23:31:22.112Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T09:34:28.065Z","updated_at":"2026-03-04T23:32:23.230Z","avatar_url":"https://github.com/mix-php.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e OpenMix 出品：[https://openmix.org](https://openmix.org/mix-php)\n\n## Mix CLI\n\nPHP CLI Interactive Commander\n\nPHP 命令行交互指挥官\n\n\u003e go 版本：https://github.com/mix-go/xcli\n\n## Overview\n\n一个命令行交互与指挥管理工具，它可以让单个 CLI 可执行多种功能，同时它还包括命令行参数获取、全局异常捕获与处理等命令行开发常用功能。\n\n## Installation\n\n```\ncomposer require mix/cli\n```\n\n## Quick start\n\n```php\nMix\\Cli\\Cli::setName('app')-\u003esetVersion('0.0.0-alpha');\n$cmd = new Mix\\Cli\\Command([\n    'name' =\u003e 'hello',\n    'short' =\u003e 'Echo demo', \n    'run' =\u003e function () {\n        $name = Mix\\Cli\\Flag::match('n', 'name')-\u003estring('default');\n        // do something\n    }\n]);\n$opt = new Mix\\Cli\\Option([\n    'names' =\u003e ['n', 'name'],\n    'usage' =\u003e 'Your name'\n]);\n$cmd-\u003eaddOption($opt);\nMix\\Cli\\Cli::addCommand($cmd)-\u003erun();\n```\n\n上面是采用闭包，也可以使用对象\n\n```php\nclass FooCommand implements Mix\\Cli\\RunInterface\n{\n    public function main(): void\n    {\n        // do something\n    }\n}\n$cmd = new Mix\\Cli\\Command([\n    'name' =\u003e 'hello',\n    'short' =\u003e 'Echo demo', \n    'run' =\u003e new FooCommand(),\n]);\n```\n\n查看整个命令行程序的帮助\n\n```\n$ php app.php\nUsage: app.php [OPTIONS] COMMAND [ARG...]\n\nCommands:\n  hello         Echo demo\n\nGlobal Options:\n  -h, --help    Print usage\n  -v, --version Print version information\n\nRun 'app.php COMMAND --help' for more information on a command.\n\nDeveloped with Mix PHP framework. (openmix.org/mix-php)\n```\n\n查看命令行程序的版本信息\n\n```\n$ php app.php -v\napp 0.0.0-alpha\n```\n\n查看 `hello` 命令的帮助\n\n```\n$ php app.php hello --help\nUsage: app.php hello [ARG...]\n\nCommand Options:\n  -n, --name    Your name\n\nDeveloped with Mix PHP framework. (openmix.org/mix-php)\n```\n\n执行 `hello` 命令\n\n```\n$ php app.php hello\n```\n\n## Flag 参数获取\n\n参数规则 (部分UNIX风格+GNU风格)\n\n```\nphp /examples/app.php home -d -rf --debug -v vvv --page 23 -s=test --name=john arg0\n```\n\n- 命令：\n    - 第一个参数，可以为空：`home`\n- 选项：\n    - 短选项：一个中杠，如 `-d`、`-rf`\n    - 长选项：二个中杠，如：`--debug`\n- 选项值：\n    - 无值：`-d`、`-rf`、 `--debug`\n    - 有值(空格)：`-v vvv`、`--page 23`\n    - 有值(等号)：`-s=test`、`--name=john`\n- 参数：\n    - 没有定义 `-` 的参数：`arg0`\n\n获取选项，可以获取 `string`、`bool`、`int`、`float` 多种类型，也可以指定默认值。\n\n```php\n$name = Mix\\Cli\\Flag::match('n', 'name')-\u003estring('Xiao Ming');\n```\n\n获取第一个参数\n\n```php\n$arg0 = Mix\\Cli\\Flag::arguments()-\u003efirst()-\u003estring();\n```\n\n获取全部参数\n\n```php\nforeach (Mix\\Cli\\Flag::arguments()-\u003evalues() as $k =\u003e $v) {\n    // do something\n}\n```\n\n## Daemon 后台执行\n\n我们可以通过配合 `flag` 获取参数，实现通过某几个参数控制程序后台执行。\n\n- 使用了 [Swoole Daemon](https://wiki.swoole.com/#/process/process?id=daemon) 方法\n\n```php\nif (Mix\\Cli\\Flag::match('d', 'daemon')-\u003ebool()) {\n    \\Swoole\\Process::daemon();\n}\n```\n\n## Middleware 与 Handle exception\n\n可以使用全局中间件给所有命令捕获异常，也可以单独对某个命令配置中间件\n\n```php\n$h = function ($next) {\n    try {\n        $next();\n    } catch (\\Throwable $ex) {\n        // handle exception\n        echo(sprintf(\"ERROR: %s\\n\", $ex-\u003egetMessage()));\n    }\n};\n$cmd = new Mix\\Cli\\Command([\n    'name' =\u003e 'hello',\n    'short' =\u003e 'Echo demo', \n    'run' =\u003e function () {\n        // do something\n    }\n]);\nMix\\Cli\\Cli::use($h)-\u003eaddCommand($cmd)-\u003erun();\n```\n\n## Application\n\n我们在编写代码时，可能会要用到 App 中的一些信息。\n\n```\n// 获取基础路径(入口文件所在目录路径)\nMix\\Cli\\Cli::app()-\u003ebasePath\n\n// App名称\nMix\\Cli\\Cli::app()-\u003ename\n\n// App版本号\nMix\\Cli\\Cli::app()-\u003eversion\n\n// 是否开启debug\nMix\\Cli\\Cli::app()-\u003edebug\n```\n\n## Singleton 单命令\n\n当我们的 CLI 只有一个命令时，只需要配置一下 `Singleton`：\n\n~~~php\n$cmd = new Mix\\Cli\\Command([\n    'name' =\u003e 'hello',\n    'short' =\u003e 'Echo demo', \n    'run' =\u003e function () {\n        // do something\n    },\n    'singleton' =\u003e true,\n]);\n~~~\n\n命令的 Options 将会在 `-h/--help` 中打印\n\n~~~\n$ php app.php\nUsage: app.php [OPTIONS] COMMAND [ARG...]\n\nCommand Options:\n  -n, --name    Your name\n\nGlobal Options:\n  -h, --help    Print usage\n  -v, --version Print version information\n\nRun 'app.php COMMAND --help' for more information on a command.\n\nDeveloped with Mix PHP framework. (openmix.org/mix-php)\n~~~\n\n## License\n\nApache License Version 2.0, http://www.apache.org/licenses/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fcli","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmix-php%2Fcli","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmix-php%2Fcli/lists"}