Download the PHP package weew/console-arguments without Composer
On this page you can find all versions of the php package weew/console-arguments. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download weew/console-arguments
More information about weew/console-arguments
Files in weew/console-arguments
Package console-arguments
Short Description Flexible console arguments parser.
License MIT
Informations about the package console-arguments
Console arguments parser
Table of contents
- Installation
- Introduction
- Parsing arguments
- Matching arguments
- Strict mode
Installation
composer require weew/console-arguments
Introduction
This package allows you to easily build commands with certain expectations and then match them against a set of console arguments. It consists of two major parts, a ArgumentsParser
and ArgumentsMatcher
.
Note: this package has not been tested on windows.
Parsing arguments
Arguments parser allows you to parse console arguments and takes care of many annoying things like quotes, equitation signs, combined flags etc. The parsing process consists of two steps. The first step is all about normalization and separation of arguments into pieces. The second one is for grouping of flags and options. Lets take a look at this example:
Now you can groups these arguments.
Depending on what kind of arguments you're working with, you might need grouped or ungrouped arguments to extract all the necessary information.
If your arguments are in form of an array instead of a string, simply do implode(' ', $args)
. Be careful with the $argv
arguments. First value in there is the path of the script and you need to remove it array_unshift($args)
.
Matching arguments
So now you have arguments. But working with plain arrays, ensuring that certain values are set etc. is quite annoying. Lets make it easy.
You can also create arguments and options like this.
There are several kinds of arguments and options.
As soon as you have some commands defined, you can match them against the arguments. Matcher will throw exceptions if command expectations were not matched.
If you have multiple commands and don't know which should be used, you can let the matcher find it based on the command name. First value inside the arguments array is assumed to be a command name. Command name is matched intelligently. So a command with name command
will match command
as well as c
and com
. If multiple commands qualify for the same name, an exceptions will be thrown.
You can also match only an option or an argument the same way, just use matchArgument
and matchOption
methods.
Strict mode
By default, arguments matcher runs in strict mode. This means that if a command receives to many arguments or unknown options, an exception will be thrown. This behaviour can be disabled though.