PHPackages                             brzuchal/command-line - 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. brzuchal/command-line

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

brzuchal/command-line
=====================

Simple PHP CommandLine argument and option parser

1.0.0(9y ago)06MITPHPPHP &gt;=7.1

Since Mar 9Pushed 9y ago1 watchersCompare

[ Source](https://github.com/brzuchal/command-line)[ Packagist](https://packagist.org/packages/brzuchal/command-line)[ RSS](/packages/brzuchal-command-line/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (2)Used By (0)

PHP CommandLine argument and option parser
==========================================

[](#php-commandline-argument-and-option-parser)

Purpose of this project is to provide modern and complete API for command line argument and option parsing in CLI.

[![PHP 7.1](https://camo.githubusercontent.com/2d596223a710c9884506accc0a6398e3d6f8639b98ab327688f92ef6cf3d07eb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e312d3843394342362e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/2d596223a710c9884506accc0a6398e3d6f8639b98ab327688f92ef6cf3d07eb/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e312d3843394342362e7376673f7374796c653d666c6174)[![Build Status](https://camo.githubusercontent.com/8a2cff9b6c502a78ac34ec31d2261026cde95e000c41d4e4b3b828b91009f250/68747470733a2f2f7472617669732d63692e6f72672f62727a756368616c2f636f6d6d616e642d6c696e652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/plumbok/plumbok)

CommandLine generics
--------------------

[](#commandline-generics)

When you type `ENV=prod php -d "error_reporting=E_ALL" script.php test --debug` only last command line components are visible in `script.php` scope at runtime.

Following analysis such command line consists of:

```
ENV=prod                   // this is shell way for setting environment variables per process
php                        // this part is pointing to php interpreter
-d "error_reporting=E_ALL" // this part holds php interpreter options
script.php                 // visible at runtime script filename
test                       // visible at runtime arguments
--debug                    // visible at runtime options

```

Runtime command line generic components are: `command` + `arguments` + `options`.

Examples
--------

[](#examples)

The simplest way to retrieve parameters from CLI with definitions:

```
$commandLine = (new \Brzuchal\CommandLine\CommandLineBuilder())
    ->withArgument('command')
    ->withOption('file', 'f')
    ->withOption('count', 'c', true)
    ->build($_SERVER['argv'], $_SERVER['PWD']);
```

The simplest way to retrieve parameters from CLI without definition:

```
$parser = new \Brzuchal\CommandLine\ArrayCommandLineParser($_SERVER['argv']);
$commandLine = $parser->parse();
```

Which for `php test.php command --env=prod --debug -f -c` gives:

```
PHP\CLI\CommandLine Object
(
    [command:protected] => test.php
    [parameters:protected] => Array
        (
            [0] => PHP\CLI\Argument Object
                (
                    [name:protected] => arg0
                    [value:protected] => command
                )

            [1] => PHP\CLI\Option Object
                (
                    [name:protected] => env
                    [value:protected] => prod
                )

            [2] => PHP\CLI\Option Object
                (
                    [name:protected] => debug
                    [value:protected] =>
                )

            [3] => PHP\CLI\Option Object
                (
                    [name:protected] => f
                    [value:protected] =>
                )

            [4] => PHP\CLI\Option Object
                (
                    [name:protected] => c
                    [value:protected] => 1
                )

        )

    [cwd:protected] => /home/brzuchal/Workspace/command-line
)

```

There is also a way to retrieve parameters with it's definitions and their requirements:

```
$definition = new \Brzuchal\CommandLine\Definition([
    new \Brzuchal\CommandLine\ArgumentDefinition('command'),
    new \Brzuchal\CommandLine\OptionDefinition('env', 'e'),
    new \Brzuchal\CommandLine\OptionDefinition('file', 'f'),
    new \Brzuchal\CommandLine\OptionDefinition('count', 'c'),
]);
$parser = new \Brzuchal\CommandLine\ArrayParameterParser($_SERVER['argv']);
$parameters = $parser->parse();
```

Which also validates if options have values and are required. Parsing command `php test.php command --env=prod --debug -f -c` gives:

```
PHP\CLI\CommandLine Object
(
    [command:protected] => test.php
    [parameters:protected] => Array
        (
            [0] => PHP\CLI\Argument Object
                (
                    [name:protected] => command
                    [value:protected] => command
                )

            [1] => PHP\CLI\Option Object
                (
                    [name:protected] => env
                    [value:protected] => prod
                )

            [2] => PHP\CLI\Option Object
                (
                    [name:protected] => debug
                    [value:protected] =>
                )

            [3] => PHP\CLI\Option Object
                (
                    [name:protected] => file
                    [value:protected] =>
                )

            [4] => PHP\CLI\Option Object
                (
                    [name:protected] => count
                    [value:protected] => 1
                )

        )

    [cwd:protected] => /home/brzuchal/Workspace/command-line
)

```

TODO
----

[](#todo)

- Parse options from array
- Parse options from string
- Validates required values
- Provide default values
- Parse arguments from array
- Parse arguments from string
- Validates existence of arguments

License
-------

[](#license)

The MIT License (MIT)

Copyright (c) 2017 Michał Brzuchalski

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFc MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

25

—

LowBetter than 36% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

3400d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ce3143c657246081b652b3f2a6b370c8fcb58ca66c15d52f7557bfecbc38f9df?d=identicon)[brzuchal](/maintainers/brzuchal)

---

Top Contributors

[![brzuchal](https://avatars.githubusercontent.com/u/3149753?v=4)](https://github.com/brzuchal "brzuchal (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/brzuchal-command-line/health.svg)

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

###  Alternatives

[seld/cli-prompt

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

24726.4M22](/packages/seld-cli-prompt)[illuminate/console

The Illuminate Console package.

13045.3M6.2k](/packages/illuminate-console)[styleci/cli

The CLI tool for StyleCI

71464.1k9](/packages/styleci-cli)[winbox/args

Windows command-line formatter

20718.9k21](/packages/winbox-args)

PHPackages © 2026

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