Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
60 lines (43 sloc) 2.14 KB

Parameter Parser

Parameter Parser is a simple library used to parse intricate parameters from an array of strings.

Hint: Parameter Parser is available through Composer. composer require nafisc/parameterparser.

StyleCI Latest Stable Version Total Downloads Latest Unstable Version License

Advanced Code Examples

Features

  • Parse command line parameters.
  • Assign aliases to parameters.
  • Custom closures for each command line parameter.
  • Variadic closure support for arguments taking more than one value.
  • Customize the way the command line is parsed.

Example Usage

// Initialize a new ParameterCluster
$parameters = new ParameterCluster();

// Add a ParameterClosure to the ParameterCluster
$parameters->add(parameter('-', 'name', function ($name) {
    return $name;
}, true));

// Create a new parameter parser using the ParameterCluster
$parameterParser = new ParameterParser($argv, $parameters);

// Parse the parameters using the ParameterParser.
$results = $parameterParser->parse();

// Verify that the parameters were valid after parsing.
if (! $parameterParser->isValid()) {

    // Since it was not valid, output usage.
    echo $parameters->getFullUsage() . PHP_EOL;

} else {

    // Retrieve the name from the results
    $name = $results['name'];

    // Output the name
    echo 'Your name is ' . $name . PHP_EOL;

}

Output

~/ php test.php -name 'Nathan Fiscaletti'

   Your name is Nathan Fiscaletti
You can’t perform that action at this time.