Download the PHP package webmozart/console without Composer

On this page you can find all versions of the php package webmozart/console. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package console

Webmozart Console

Build Status Build status Scrutinizer Code Quality Latest Stable Version Total Downloads Dependency Status

Latest release: 1.0.0-beta5

PHP >= 5.3.9

A usable, beautiful and easily testable console toolkit written in PHP.

Goal

The goal of this package is:

None of the existing console libraries matched these requirements, so I refactored the Symfony Console component into what you can see here.

Installation

Use Composer to install the package:

Basic Configuration

Console applications are configured via configuration classes. As example, we will create the "git" command in PHP:

This basic configuration tells the console that the executable is called "git" and that the current version is 1.0.0. Let's create the "git" executable now:

The complicated autoload block makes sure that the autoload file is found both when you run the executable directly in your package and when your package is installed in another project via Composer.

Change the permissions of your executable and try to run it:

Commands

So far, our application doesn't do much. Let's add a command "log" that displays the latest commits:

As you can see, the execution of the command is delegated to a LogCommandHandler. Since the handler is a separate class, it can easily be tested in isolation. Let's implement a basic handler:

The handle() method of our command handler retrieves up to three arguments:

You can leave away the arguments that you don't need.

Every handler should return 0 if it was processed successfully and any integer between 1 and 255 if it failed.

Let's run the command:

Arguments

Next we'll add an argument <branch> to the "log" command with which we can select the branch to display:

We added an optional argument "branch" with the default value "master". We can access the value of the argument through the Args instance in the handler:

Let's run the command with the argument:

The second argument of the addArgument() method accepts a bitwise combination of different flags:

Constant Description
Argument::OPTIONAL The argument is optional. If you don't pass a default value, the default value is null.
Argument::REQUIRED The argument must be passed when calling the command.
Argument::MULTI_VALUED The argument may be passed multiple times. The command handler receives an array of all passed values.
Argument::STRING The argument is parsed as string (the default).
Argument::BOOLEAN The argument is parsed as boolean.
Argument::INTEGER The argument is parsed as integer.
Argument::FLOAT The argument is parsed as float.
Argument::NULLABLE Convert "null" to null.

Options

Options are additional, optional settings that you can pass to your command. Let's add the option --max=<limit> to our command which limits the number of displayed commits to the passed limit:

The configuration of options is very similar to the configuration of arguments. We created a --max option, which requires a value. If the option is not set by the user, its value defaults to 25.

We can access the passed value with the getOption() method in our command handler:

Additionally, you will frequently need the isOptionSet() method, which tells you whether the user actually passed the option when calling the command.

Let's run the command with the option:

Options support short names that consist of a single character only. Instead of two leading dashes, short option names are only prefixed with one dash, for example: -m. Let's add this alias to our option by setting the second argument of the addOption() method:

Now the command can also be run like this:

Like for arguments, options support a bitwise combination of different flags that control how the option is processed:

Constant Description
Option::NO_VALUE The option accepts no value. Used for simple on/off settings.
Option::OPTIONAL_VALUE The option accepts a value, but the value is option.
Option::REQUIRED_VALUE The option value needs to be set when passing the option.
Option::MULTI_VALUED The option may be passed multiple times. The command handler receives an array of all passed values.
Option::STRING The option is parsed as string (the default).
Option::BOOLEAN The option is parsed as boolean.
Option::INTEGER The option is parsed as integer.
Option::FLOAT The option is parsed as float.
Option::NULLABLE Convert "null" to null.
Option::PREFER_LONG_NAME The help lists the long form (--max) as suggested way of passing the option (the default).
Option::PREFER_SHORT_NAME The help lists the short form (-m) as suggested way of passing the option.

Dependencies

Very often, our command handlers rely on external services to access information or execute business logic. These services can be injected through the constructor of the command handler. For example, assume that we need a CommitRepository to access the commits listed in the "log" command:

Since the CommitRepository is injected into the command handler, we can easily replace the repository with a mock object when we test the handler.

We also need to change the configuration to inject the repository:

If your application grows, a lot of objects will be created whenever the configure() method is executed - even if the commands that need these objects are not executed. Let's change our call to setHandler() to a closure so that the handler is executed on demand:

Now, the LogCommandHandler and its dependencies are only created when "log" command is executed.

Sub-Commands

The "log" command was a very simple example, but many real-world use cases are more complicated than that. Consider the "git remote" command, which is split into several sub-commands:

Such sub-commands can be introduced with the beginSubCommand() method in the configuration:

Like regular commands, sub-commands accept options, arguments and command handlers. However, instead of creating one command handler per sub-command, it is often more convenient to create a single handler with one method per sub-command. The handler method can be selected with setHandlerMethod().

The basic implementation of our RemoteCommandHandler is very similar to the command handler of the "log" command:

If we inject a working RemoteManager, we can execute both commands now:

If we want to execute the "list" command by default if no sub-command is selected, we need to mark it as default command with markDefault():

Now we can run the list command with:

At last, you can use markAnonymous() if you want to run the command just with git remote, but not with git remote list.

Colors and Styles

The console supports colors and styles in the output that you pass to the IO class. Let's output some bold text for example:

Just like in HTML, the styles are inserted in your text using markup tags. The following styles are defined by default:

Tag Description
<b> Bold text
<u> Underlined text
<bu> Bold and underlined text
<c1> Colored text (default: cyan)
<c2> Colored text (default: yellow)
<warn> Black text with a yellow background
<error> White text with a red background

You can change the existing styles or add custom styles using the addStyle() method in your configuration:

Here is a list of methods that can be called on the Style class:

Method Description
bold() Bold text
underlined() Underlined text
blinking() Blinking text
inverse() Invert the foreground and background color
hidden() Hidden text
fgDefault() Use the system's default text color
fgBlack() Black text
fgRed() Red text
fgGreen() Green text
fgYellow() Yellow text
fgBlue() Blue text
fgMagenta() Magenta text
fgCyan() Cyan text
fgWhite() White text
bgDefault() Use the system's default background color
bgBlack() Black background
bgRed() Red background
bgGreen() Green background
bgYellow() Yellow background
bgBlue() Blue background
bgMagenta() Magenta background
bgCyan() Cyan background
bgWhite() White background

Tables

You can draw tables with the Table class:

Tables support word wrapping, so that overlong text does not break the output. The default output of the above table is similar to this:

You can change the style of the table by passing a custom TableStyle to the constructor. Either build your own TableStyle or use one of the predefined ones:

The following predefined table styles exist:

Style Description
TableStyle::asciiBorder() Uses ASCII characters for the border (the default)
TableStyle::solidBorder() Uses Unicode characters for the border
TableStyle::borderless() No border

Using Symfony Helpers

Since this library is a complete refactor of the Symfony Console component you can not use the Symfony Console helpers out of the box. If you want to use them you need to wrap the input and output accordingly:

If you find this to cumbersome and you use it many times in your code, consider using traits:

Authors

Contribute

Contributions to are very welcome!

Support

If you are having problems, send a mail to [email protected] or shout out to @webmozart on Twitter.

License

All contents of this package are licensed under the MIT license.


All versions of console with dependencies

PHP Build Version
Package Version
Requires php Version >=5.3.9
webmozart/assert Version ^1.0
symfony/console Version ^2.7|^3.0
symfony/process Version ^2.5|^3.0
symfony/event-dispatcher Version ^2.5|^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package webmozart/console contains the following files

Loading the files please wait ....