Giter VIP home page Giter VIP logo

parameterparser's Introduction

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

Documentation - Advanced Code Examples - Looking for the Python version?

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 Cluster
$parameters = new Cluster();

// Add a Parameter to the Cluster
$parameter = parameter('-', 'name', function ($name) {
    return $name;
});

$parameter->setRequired(true)
          ->setDescription('Your name.');

$parameters->add($parameter);

// Create a new Parser using the Cluster
$parser = new Parser($argv, $parameters);

// Parse the parameters using the Parser.
$results = $parser->parse();

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

    // Since it was not valid, output usage.
    $parameters->printFullUsage(
        "Parameter Parser",
        "An advanced parameter parser for PHP",
        "v1.0.0"
    );

} 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

parameterparser's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

parameterparser's Issues

Add child parameters

Add child parameters to existing parameters.

An example of the end-user usage would be
php myscript.php -keys remove [keyid]
php myscript.php -keys add [keyid]
php myscript.php -firewall addrule [ip] [block_value]
php myscript.php -firewall delrule [ip]

Add usage for each ParameterClosure

Instead of only leaving the usage output up to the user, add a ->getUsage() function to the ParameterClosure class. This way each parameter would have it's own usage.

Then add a ->getGlobalUsage() function to the ParameterCluster class. (Possibly add a formatting parameter to this function?) that would output formatted global usage for the script.

The code in ParameterParser would need to be refactored somewhat to allow for this.

This would need to link #1 so that when usage is returned it also returns aliases.

Add helper function for creating new ParameterClosures

Create a global helper function for creating new ParameterClosures

Example:

$loadClosure = parameter('load', function (... $arguments) {
    return $arguments;
});

function parameter($name, $closure)
{
    return new ParameterClosure($name, $closure);
}

Add aliases to ParameterClosures

Allow parameter aliases that call back to the same closure.

Example:

. . .
// Define a ParameterClosure
$readClosure = new ParameterClosure('read', function ($file) {
    return $file;
});

// Set the aliases and the prefix to associate the alias with.
$readClosure->addAlias('-', 'r');
$readClosure->addAlias('--', 'read-file');

// Register the closure with the ParameterCluster
$parameterCluster->add('-', $readClosure);
. . .

You can now access the closure with either of the following:

php test.php -read MyFile.txt

- or -

php test.php -r MyFile.txt

- or -

php test.php --read-file  #MyFile.txt

Unable to re-use ParameterParser object

Since the parameter parser object pre-loads it's parameters in the constructor, there is no way to re-use the object.

Suggested Fix:

Move the $argv initialization and pre-loading to the parse function.

. . .
$parameterParser->parse($argv);
. . .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.