This package is abandoned and no longer maintained. No replacement package was suggested.

Small functional library to interpret CLI arguments

2.0.0 2017-10-15 17:04 UTC

This package is not auto-updated.

Last update: 2020-01-24 17:11:17 UTC


README

Small functional library to interpret CLI arguments ✨

Build Status

Install

$ composer require troublete/argv

Usage

<?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)

API

Methods

cleanArguments(array $argv): string

Will remove the script from the input arguments.

reduceFlagName(string $flagName): string

Will remove - and -- from flags and flag aliases.

isCommandCall(array $arguments): bool

Will determine by checking the first argument if a command is called or only a flag call is occuring.

isFlag(string $argument): bool

Will determine if argument is a flag by checking if first two characters are --.

isFlagAlias(string $argument): bool

Will determine if argument is a flag alias by checking if the first character is -.

getFlags(array $arguments, array $aliases = []): class@anonymous

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.

getValues(array $arguments): class@anonymous

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.

getCommand(array $arguments): string

Alias method to return the command name off the values, if set.

License

GPL-2.0 Willi Eßer