PHPackages                             treffynnon/command-wrap - 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. treffynnon/command-wrap

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

treffynnon/command-wrap
=======================

Wraps command line access into a builder and provides argument escaping

v1.0.0(10y ago)425BSDPHPPHP &gt;5.6

Since Mar 14Pushed 9y ago1 watchersCompare

[ Source](https://github.com/treffynnon/command-wrap)[ Packagist](https://packagist.org/packages/treffynnon/command-wrap)[ Docs](https://github.com/treffynnon/CmdWrap)[ RSS](/packages/treffynnon-command-wrap/feed)WikiDiscussions master Synced 2d ago

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

Command Wrap
============

[](#command-wrap)

A PHP library to wrap the command line/terminal/shell. It provides a clean builder that escapes arguments and commands - you can extract a command string to run as you please or pass the builder to a runner.

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

[](#installation)

```
composer require treffynnon/command-wrap
```

Example
-------

[](#example)

```
$bld = new Builder();
$bld->addEnvVar('JAVA_BIN', '/usr/bin/java')
    ->addEnvVar('TMP_DIR', '/tmp')
    ->addCommand('foo')
    ->addFlag('f')
    ->addFlag('t', 'xml')
    ->addCommand('src/')
    ->addArgument('verbose')
    ->addArgument('results-log', '/tmp/results.log')
    ->addRaw('> /dev/null 2>&1');
$sp = new SymfonyProcess();
$response = $sp->run($bld);
// JAVA_BIN='/usr/bin/java' TMP_DIR='/tmp' foo -f -t='xml' src/ --verbose --results-log='/tmp/results.log' > /dev/null 2>&1
```

### Using a callback to process output

[](#using-a-callback-to-process-output)

Each line of the command's output can be processed by a callback/anonymous/lambda function. You can of course pass in a closure too!

```
$bld = new Builder();
$bld->addEnvVar('JAVA_BIN', '/usr/bin/java')
    ->addEnvVar('TMP_DIR', '/tmp')
    ->addCommand('foo')
    ->addFlag('f')
    ->addFlag('t', 'xml')
    ->addCommand('src/')
    ->addArgument('verbose')
    ->addArgument('results-log', '/tmp/results.log')
    ->addRaw('> /dev/null 2>&1');
$sp = new SymfonyProcess();
$response = $sp->run($bld, function ($line) {
    return str_replace("\t", '    ', $line);
});
// JAVA_BIN='/usr/bin/java' TMP_DIR='/tmp' foo -f -t='xml' src/ --verbose --results-log='/tmp/results.log' > /dev/null 2>&1
```

This would replace all tabs with four (4) spaces in each line of output from the command. Note that the new value must be returned from your lambda.

You can then get the output in the usual way by calling `$response->getOutput()`.

If you want to have updates in real time from long running command then you need to use the `SymfonyProcess` runner and log/echo from your custom callback function.

```
$response = $sp->run($bld, function ($line) use ($logger) {
    $logger->push("New line added: $line");
    return str_replace("\t", '    ', $line);
});
```

### Getting a command as a string

[](#getting-a-command-as-a-string)

```
$bld = new Builder();
$bld->addEnvVar('JAVA_BIN', '/usr/bin/java')
    ->addEnvVar('TMP_DIR', '/tmp')
    ->addCommand('hint&&hint')
    ->addCommand('foo')
    ->addFlag('f')
    ->addFlag('t', 'xml')
    ->addCommand('src/')
    ->addArgument('verbose')
    ->addArgument('results-log', '/tmp/results.log')
    ->addRaw('> /dev/null 2>&1');
$cmd = $bld->getCommandAssembler()
           ->getCommandString();
// JAVA_BIN='/usr/bin/java' TMP_DIR='/tmp' foo -f -t='xml' src/ --verbose --results-log='/tmp/results.log' > /dev/null 2>&1
```

Available command line types
----------------------------

[](#available-command-line-types)

TypeBuilder methodExample final output`Command``addCommand('ls');``ls``Flag``addFlag('-t');``-t``Flag``addFlag('-t', '/tmp');``-t='/tmp'``Argument``addArgument('results');``--results``Argument``addArgument('results', '/tmp/results.log');``--results='/tmp/results.log'``Parameter``addParameter('parameter');``'parameter'``EnvVar``addEnvVar('MY_ENV_VAR', 'value');``MY_ENV_VAR='value'``Raw``addRaw('> /dev/null 2>&1')``> /dev/null 2>&1`**Note:** As the name implies `Raw` does not perform any escaping - use with appropriate caution.

Available runners
-----------------

[](#available-runners)

- Symfony Process (`Treffynnon\CommandWrap\Runners\SymfonyProcess`) *recommended*
- `exec()` (`Treffynnon\CommandWrap\Runners\Exec`)
- `passthru()` (`Treffynnon\CommandWrap\Runners\Passthru`)
- `system()` (`Treffynnon\CommandWrap\Runners\System`)

By implementing `Treffynnon\CommandWrap\Runners\RunnerInterface` you can also provide your own custom runner.

When a command is executed via a runner it will return an instance of `\Treffynnon\CommandWrap\Response`containing the response from STDOUT and STDERR if available.

Command combinators
-------------------

[](#command-combinators)

POSIX commands can be combined with a few characters such as:

- &amp;&amp; (`Treffynnon\CommandWrap\Combinators\AndAnd`)
- | (`Treffynnon\CommandWrap\Combinators\Pipe`)
- ; (`Treffynnon\CommandWrap\Combinators\Semicolon`)

These have been wrapped up into objects that you can use to combine commands/builders. You can also combine combinators too!

```
$combinator = new AndAnd(
    $builder,
    $builder2
);
$combinator2 = new Semicolon(
    $combinator,
    $builder3,
    $builder4
);
```

These can then be passed to a runner just like any builder can be:

```
$Exec = new Exec();
$Exec->run($combinator2);
```

Command assemblers
------------------

[](#command-assemblers)

When a command is being converted into a string an assembler will be used by default it will use the `ChronoAssembler`, but you can also use `OrderedAssembler` or even provide your own by implementing `AssemblerInterface`.

`ChronoAssembler` compiles a command in the order it was added to the builder (chronologically). `OrderedAssembler` combines into a specified order - see the class for details.

To specify the assembler to use you provide an instance of it to the `Builder`object.

```
$bld = new Builder(new OrderedAssembler());
$bld->addEnvVar('JAVA_BIN', '/usr/bin/java')
    ->addCommand('hint&&hint')
    ->addCommand('foo')
    ->addFlag('f')
    ->addFlag('t', 'xml')
    ->addCommand('src/')
    ->addArgument('verbose')
    ->addArgument('results-log', '/tmp/results.log');
```

Custom command collection
-------------------------

[](#custom-command-collection)

It is also possible to provide your own command collection class as the second parameter to your `Builder` instance.

```
$bld = new Builder(new ChronoAssembler(), new MyCommandCollection());
$bld->addEnvVar('JAVA_BIN', '/usr/bin/java')
    ->addCommand('hint&&hint')
    ->addCommand('foo')
    ->addFlag('f')
    ->addFlag('t', 'xml')
    ->addCommand('src/')
    ->addArgument('verbose')
    ->addArgument('results-log', '/tmp/results.log');
```

Again implement the `CommandCollectionInterface`.

Tests
-----

[](#tests)

Unit testing is completed with phpspec, integration testing with phpunit and the code is also linted with `php -l`, phpcs and phpcpd. To run the tests you can use the following composer command:

```
composer test
```

Licence
-------

[](#licence)

BSD 3 clause licence - see LICENCE.md.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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

3761d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/65215?v=4)[Simon Holywell](/maintainers/Treffynnon)[@treffynnon](https://github.com/treffynnon)

---

Top Contributors

[![treffynnon](https://avatars.githubusercontent.com/u/65215?v=4)](https://github.com/treffynnon "treffynnon (9 commits)")

---

Tags

assemblerbinarybuildercallbackcommand-lineescapes-argumentsphprunnerconsolecommand-lineshellwrappercommand

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/treffynnon-command-wrap/health.svg)

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

###  Alternatives

[helhum/typo3-console

A reliable and powerful command line interface for TYPO3 CMS

2959.3M241](/packages/helhum-typo3-console)[mrrio/shellwrap

Use any command-line tool as a PHP function.

738214.3k2](/packages/mrrio-shellwrap)[inhere/console

php console library, provide console argument parse, console controller/command run, color style, user interactive, information show.

3487.5k12](/packages/inhere-console)

PHPackages © 2026

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