PHPackages                             mix/cli - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. mix/cli

ActiveLibrary[CLI &amp; Console](/categories/cli)

mix/cli
=======

PHP CLI Interactive Commander

v3.0.3(4y ago)22.1k↓50%5Apache-2.0PHPPHP &gt;=7.0.0

Since Aug 4Pushed 3y ago1 watchersCompare

[ Source](https://github.com/mix-php/cli)[ Packagist](https://packagist.org/packages/mix/cli)[ Docs](https://openmix.org/mix-php)[ RSS](/packages/mix-cli/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (4)Used By (5)

> OpenMix 出品：[https://openmix.org](https://openmix.org/mix-php)

Mix CLI
-------

[](#mix-cli)

PHP CLI Interactive Commander

PHP 命令行交互指挥官

> go 版本：

Overview
--------

[](#overview)

一个命令行交互与指挥管理工具，它可以让单个 CLI 可执行多种功能，同时它还包括命令行参数获取、全局异常捕获与处理等命令行开发常用功能。

Installation
------------

[](#installation)

```
composer require mix/cli

```

Quick start
-----------

[](#quick-start)

```
Mix\Cli\Cli::setName('app')->setVersion('0.0.0-alpha');
$cmd = new Mix\Cli\Command([
    'name' => 'hello',
    'short' => 'Echo demo',
    'run' => function () {
        $name = Mix\Cli\Flag::match('n', 'name')->string('default');
        // do something
    }
]);
$opt = new Mix\Cli\Option([
    'names' => ['n', 'name'],
    'usage' => 'Your name'
]);
$cmd->addOption($opt);
Mix\Cli\Cli::addCommand($cmd)->run();
```

上面是采用闭包，也可以使用对象

```
class FooCommand implements Mix\Cli\RunInterface
{
    public function main(): void
    {
        // do something
    }
}
$cmd = new Mix\Cli\Command([
    'name' => 'hello',
    'short' => 'Echo demo',
    'run' => new FooCommand(),
]);
```

查看整个命令行程序的帮助

```
$ php app.php
Usage: app.php [OPTIONS] COMMAND [ARG...]

Commands:
  hello         Echo demo

Global Options:
  -h, --help    Print usage
  -v, --version Print version information

Run 'app.php COMMAND --help' for more information on a command.

Developed with Mix PHP framework. (openmix.org/mix-php)

```

查看命令行程序的版本信息

```
$ php app.php -v
app 0.0.0-alpha

```

查看 `hello` 命令的帮助

```
$ php app.php hello --help
Usage: app.php hello [ARG...]

Command Options:
  -n, --name    Your name

Developed with Mix PHP framework. (openmix.org/mix-php)

```

执行 `hello` 命令

```
$ php app.php hello

```

Flag 参数获取
---------

[](#flag-参数获取)

参数规则 (部分UNIX风格+GNU风格)

```
php /examples/app.php home -d -rf --debug -v vvv --page 23 -s=test --name=john arg0

```

- 命令：
    - 第一个参数，可以为空：`home`
- 选项：
    - 短选项：一个中杠，如 `-d`、`-rf`
    - 长选项：二个中杠，如：`--debug`
- 选项值：
    - 无值：`-d`、`-rf`、 `--debug`
    - 有值(空格)：`-v vvv`、`--page 23`
    - 有值(等号)：`-s=test`、`--name=john`
- 参数：
    - 没有定义 `-` 的参数：`arg0`

获取选项，可以获取 `string`、`bool`、`int`、`float` 多种类型，也可以指定默认值。

```
$name = Mix\Cli\Flag::match('n', 'name')->string('Xiao Ming');
```

获取第一个参数

```
$arg0 = Mix\Cli\Flag::arguments()->first()->string();
```

获取全部参数

```
foreach (Mix\Cli\Flag::arguments()->values() as $k => $v) {
    // do something
}
```

Daemon 后台执行
-----------

[](#daemon-后台执行)

我们可以通过配合 `flag` 获取参数，实现通过某几个参数控制程序后台执行。

- 使用了 [Swoole Daemon](https://wiki.swoole.com/#/process/process?id=daemon) 方法

```
if (Mix\Cli\Flag::match('d', 'daemon')->bool()) {
    \Swoole\Process::daemon();
}
```

Middleware 与 Handle exception
-----------------------------

[](#middleware-与-handle-exception)

可以使用全局中间件给所有命令捕获异常，也可以单独对某个命令配置中间件

```
$h = function ($next) {
    try {
        $next();
    } catch (\Throwable $ex) {
        // handle exception
        echo(sprintf("ERROR: %s\n", $ex->getMessage()));
    }
};
$cmd = new Mix\Cli\Command([
    'name' => 'hello',
    'short' => 'Echo demo',
    'run' => function () {
        // do something
    }
]);
Mix\Cli\Cli::use($h)->addCommand($cmd)->run();
```

Application
-----------

[](#application)

我们在编写代码时，可能会要用到 App 中的一些信息。

```
// 获取基础路径(入口文件所在目录路径)
Mix\Cli\Cli::app()->basePath

// App名称
Mix\Cli\Cli::app()->name

// App版本号
Mix\Cli\Cli::app()->version

// 是否开启debug
Mix\Cli\Cli::app()->debug

```

Singleton 单命令
-------------

[](#singleton-单命令)

当我们的 CLI 只有一个命令时，只需要配置一下 `Singleton`：

```
$cmd = new Mix\Cli\Command([
    'name' => 'hello',
    'short' => 'Echo demo',
    'run' => function () {
        // do something
    },
    'singleton' => true,
]);
```

命令的 Options 将会在 `-h/--help` 中打印

```
$ php app.php
Usage: app.php [OPTIONS] COMMAND [ARG...]

Command Options:
  -n, --name    Your name

Global Options:
  -h, --help    Print usage
  -v, --version Print version information

Run 'app.php COMMAND --help' for more information on a command.

Developed with Mix PHP framework. (openmix.org/mix-php)

```

License
-------

[](#license)

Apache License Version 2.0,

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

3

Last Release

1746d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/16074765?v=4)[LIU JIAN](/maintainers/onanying)[@onanying](https://github.com/onanying)

---

Top Contributors

[![onanying](https://avatars.githubusercontent.com/u/16074765?v=4)](https://github.com/onanying "onanying (21 commits)")[![SyanH](https://avatars.githubusercontent.com/u/6157072?v=4)](https://github.com/SyanH "SyanH (1 commits)")

---

Tags

climixcommander

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mix-cli/health.svg)

```
[![Health](https://phpackages.com/badges/mix-cli/health.svg)](https://phpackages.com/packages/mix-cli)
```

###  Alternatives

[symfony/console

Eases the creation of beautiful and testable command line interfaces

9.8k1.1B11.3k](/packages/symfony-console)[nunomaduro/collision

Cli error handling for console/command-line PHP applications.

4.6k331.8M8.5k](/packages/nunomaduro-collision)[nunomaduro/termwind

It's like Tailwind CSS, but for the console.

2.5k239.8M286](/packages/nunomaduro-termwind)[wp-cli/wp-cli

WP-CLI framework

5.1k17.2M320](/packages/wp-cli-wp-cli)[wp-cli/php-cli-tools

Console utilities for PHP

68325.0M367](/packages/wp-cli-php-cli-tools)[socialengine/sniffer-rules

A Lumen 5 and Laravel 5 SquizLabs Code Sniffer 2.0 artisan command. Detect violations of a defined coding standard. It helps your code remains clean and consistent.

1248.2k1](/packages/socialengine-sniffer-rules)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
