Small functional library to interpret CLI arguments ✨
$ composer require troublete/argv
<?php
require_once 'path/to/autoload.php';
use function Argv\{cleanArguments, getFlags};
$cleanedArguments = cleanArguments($argv);
$flags = getFlags($cleanedArguments);
if ($flags->rainbow) {
echo 'rainbow 🌈';
}
On CLI execution like $ php file-with-script.php --rainbow
the CLI will output rainbow 🌈
. (see examples
for a working example)
Will remove the script from the input arguments.
Will remove -
and --
from flags and flag aliases.
Will determine by checking the first argument if a command is called or only a flag call is occuring.
Will determine if argument is a flag by checking if first two characters are --
.
Will determine if argument is a flag alias by checking if the first character is -
.
Will return an class with all flags (NOT aliases) as public properties, value set to true or the value with the following index of the flag.
If aliases are provided in the form like ['f' => 'flag']
the flag flag
will be set on script call with only -f
.
Will return all provided values encapsulated in a anonymous class (original index will be preserved). With calling ->all()
on the returned class an array with all values will be returned, where key is original index and value the value. With calling ->first()
the first value will be returned.
Alias method to return the command name off the values, if set.
GPL-2.0 Willi Eßer