PHPackages                             wildphp/command-parser - 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. wildphp/command-parser

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

wildphp/command-parser
======================

Command parser subsystem used in WildPHP

v0.1.2(7y ago)01.6k[1 PRs](https://github.com/WildPHP/command-parser/pulls)MITPHPPHP &gt;=7.0.0CI passing

Since Nov 7Pushed 3y ago3 watchersCompare

[ Source](https://github.com/WildPHP/command-parser)[ Packagist](https://packagist.org/packages/wildphp/command-parser)[ RSS](/packages/wildphp-command-parser/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (6)Used By (0)

Command parsing library for WildPHP
===================================

[](#command-parsing-library-for-wildphp)

---

[![Build Status](https://camo.githubusercontent.com/3b3d502cae91c7442299564c76bcaeff41f4f7c8b1694d6bc948bc8f9f678124/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f57696c645048502f636f6d6d616e642d7061727365722f6261646765732f6275696c642e706e67)](https://scrutinizer-ci.com/g/WildPHP/command-parser/build-status/master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/c80399d37b69667708c9c68d1c2b0ae331b7174bcc2b76a64af7ffa85bf222d1/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f57696c645048502f636f6d6d616e642d7061727365722f6261646765732f7175616c6974792d73636f72652e706e67)](https://scrutinizer-ci.com/g/WildPHP/command-parser/?branch=master)[![Scrutinizer Code Coverage](https://camo.githubusercontent.com/0af2b0653ee497175b8c249f20212d8edac128db5fa3322c9698ad8faeb4f637/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f57696c645048502f636f6d6d616e642d7061727365722f6261646765732f636f7665726167652e706e67)](https://scrutinizer-ci.com/g/WildPHP/command-parser/code-structure/master/code-coverage)[![Latest Stable Version](https://camo.githubusercontent.com/c9ed58e6b0837bfdcb0e810891a34fae5dd7fd94a3aa1a5cd8d6eef0f82e4393/68747470733a2f2f706f7365722e707567782e6f72672f77696c647068702f636f6d6d616e642d7061727365722f762f737461626c65)](https://packagist.org/packages/wildphp/command-parser)[![Latest Unstable Version](https://camo.githubusercontent.com/fbaecf6ec8a07e42867b541dae8c7a75f4a07d976adf7115ec488e3454751c28/68747470733a2f2f706f7365722e707567782e6f72672f77696c647068702f636f6d6d616e642d7061727365722f762f756e737461626c65)](https://packagist.org/packages/wildphp/command-parser)[![Total Downloads](https://camo.githubusercontent.com/c42ea4c653902dc71a2f2a0fe44befef0acf6c052e9e9cc92fd0592494f7ee19/68747470733a2f2f706f7365722e707567782e6f72672f77696c647068702f636f6d6d616e642d7061727365722f646f776e6c6f616473)](https://packagist.org/packages/wildphp/command-parser)

This library aims at making command parsing easy. Not only does it parse strings into commands and parameters, it allows you to define restrictions to parameters (so-called strategies) and do automatic type/content validation.

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

[](#installation)

To install this library, you will need [Composer](https://getcomposer.org/).

```
$ composer require wildphp/command-parser ^0.1

```

Getting started
---------------

[](#getting-started)

This library comes with a set of ready-to-use parameters, but you might want to develop your own. More on that later.

The most important classes in the library are the `Command`, `ParameterStrategy`, `CommandParser` and `CommandProcessor`.

### Command

[](#command)

This is the class which defines your commands. It is a storage class, meaning it does nothing more but store items you hand to it.

#### Creating a Command

[](#creating-a-command)

A `Command` takes two parameters for its constructor. A callback which should be called when the command is triggered, and one or more `ParameterStrategy` instances which will define the behavior of the command. For example:

```
$foo = function () {};
$command = new Command($foo, new ParameterStrategy(0, 1, [
    new NumericParameter(),
]);
```

### ParameterStrategy

[](#parameterstrategy)

A `ParameterStrategy` instance describes the ways in which commands may be executed.

Its constructor takes a few arguments:

```
__construct(
        int $minimumParameters = -1,
        int $maximumParameters = -1,
        array $initialValues = [],
        bool $implodeLeftover = false
)
```

- `$minimumParameters` and `$maximumParameters` describe how many parameters this particular strategy may take. They can be set to -1 or false to disable either bound. However, minimum cannot be larger than maximum.
- `$initialValues` is an array of `ParameterInterface` objects. More on those below.
- `$concatLeftover` is a boolean. When set to true, the strategy will concatenate all remaining parameters together into one when the maximum parameter count is reached. For example, consider a strategy which will only take up to 2 parameters. If you pass it a command with 3 parameters with this flag set, for example '!test 1 2 3', 2 and 3 will be concatenated as `array('1', '2 3')`

### Available parameters

[](#available-parameters)

#### NumericParameter

[](#numericparameter)

Takes any value which is numeric, it uses the php `is_numeric` function internally.

#### PredefinedStringParameter

[](#predefinedstringparameter)

Set a predefined string in its constructor and only that string will be accepted as its value, thus any other value will be rejected.

#### StringParameter

[](#stringparameter)

Returns true under any circumstance, since parameters are always strings.

### CommandParser

[](#commandparser)

As the name would suggest, this is the class in charge of actually parsing messages. When it does so, it hands out `ParsedCommand`objects. As it is a utility class, its methods are called statically.

#### findApplicableStrategy

[](#findapplicablestrategy)

`findApplicableStrategy(Command $commandObject, array $parameters): ParameterStrategy`

This method finds the most applicable strategy to use on `$parameters` and returns it.

#### parseFromString

[](#parsefromstring)

`parseFromString(string $string, string $prefix = '!'): ParsedCommand`

Parses a command and its parameters from a string, but does not process them. See below for the CommandProcessor. Set `$prefix` for the prefix to use, which should prefix any command given.

`ParsedCommand` is an object containing the command name and the original parameters given.

### CommandProcessor

[](#commandprocessor)

This class processes a ParsedCommand further and provides the value conversion facilities. Moreover, when instantiated, it acts as a storage facility for commands so they need not be stored elsewhere.

#### processParsedCommand

[](#processparsedcommand)

The heart of this class is this static function.

`public static function processParsedCommand(ParsedCommand $parsedCommand, Command $command): ProcessedCommand`

It takes a `ParsedCommand` object and accompanying `Command` object and processes it into a `ProcessedCommand` object, which contains the original `ParsedCommand` data plus the callback, the converted parameter values and also the applied strategy used for conversion.

### process

[](#process)

`process(ParsedCommand $parsedCommand): ProcessedCommand`

Much the same as the above function but uses the internal command collection.

### registerCommand

[](#registercommand)

`registerCommand(string $command, Command $commandObject): bool`

Adds a command to the internal command collection, identified by `$command`. Returns true on success, false if the given identifier already exists.

### findCommand

[](#findcommand)

`findCommand(string $command): Command`

Returns a Command object added by above function which is identified by `$command`.

Contributors
------------

[](#contributors)

You can see the full list of contributors [in the GitHub repository](https://github.com/WildPHP/command-parser/graphs/contributors).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~14 days

Total

3

Last Release

2721d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/64cbefa2b33116fa8d909bbeb6add16b949cd91864b693cd9c4797430ee7eb6d?d=identicon)[Yoshi2889](/maintainers/Yoshi2889)

---

Top Contributors

[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (24 commits)")[![NanoSector](https://avatars.githubusercontent.com/u/1280380?v=4)](https://github.com/NanoSector "NanoSector (21 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/wildphp-command-parser/health.svg)

```
[![Health](https://phpackages.com/badges/wildphp-command-parser/health.svg)](https://phpackages.com/packages/wildphp-command-parser)
```

###  Alternatives

[wp-cli/wp-cli

WP-CLI framework

5.1k17.2M320](/packages/wp-cli-wp-cli)[consolidation/annotated-command

Initialize Symfony Console commands from annotated command class methods.

22569.8M19](/packages/consolidation-annotated-command)[seld/cli-prompt

Allows you to prompt for user input on the command line, and optionally hide the characters they type

24725.8M17](/packages/seld-cli-prompt)[illuminate/console

The Illuminate Console package.

12944.1M5.1k](/packages/illuminate-console)[php-tui/php-tui

Comprehensive TUI library heavily influenced by Ratatui

589747.0k6](/packages/php-tui-php-tui)[codedungeon/php-cli-colors

Liven up you PHP Console Apps with standard colors

10210.1M26](/packages/codedungeon-php-cli-colors)

PHPackages © 2026

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