chh/optparse

Easy to use option parser.

v0.1.0 2012-12-19 18:13 UTC

This package is not auto-updated.

Last update: 2024-10-26 14:05:01 UTC


README

Install

1. Get composer.

2. Put this into your local composer.json:

{
  "require": {
    "chh/optparse": "*@dev"
  }
}

3. php composer.phar install

Example

hello.php:

<?php

require "vendor/autoload.php";

use CHH\Optparse;

$parser = new Optparse\Parser("Says Hello");

function usage_and_exit()
{
    global $parser;
    fwrite(STDERR, "{$parser->usage()}\n");
    exit(1);
}

$parser->addFlag("help", array("alias" => "-h"), "usage_and_exit");
$parser->addFlag("shout", array("alias" => "-S"));
$parser->addArgument("name", array("required" => true));

try {
    $parser->parse();
} catch (Optparse\Exception $e) {
    usage_and_exit();
}

$msg = "Hello {$parser["name"]}!";

if ($parser["shout"]) {
    $msg = strtoupper($msg);
}

echo "$msg\n";

Try it:

% php hello.php Christoph --shout
HELLO CHRISTOPH!

Use

There are two things you will define in the parser:

  • Flags, arguments which start with one or two dashes and are considered as options of your program.
  • Arguments, everything else which is not a flag.

The main point of interest is the CHH\Optparse\Parser, which you can use to define Flags and Arguments.

Flags

To define a flag, pass the flag's name to the addFlag method:

<?php

$parser = new CHH\Optparse\Parser;

$parser->addFlag("help");
$parser->parse();

if ($parser["help"]) {
    echo $parser->usage();
    exit;
}

A flag defined with addFlag is by default available as --$flagName. To define another name (e.g. a short name) for the flag, pass it as the value of the alias option in the options array:

<?php
$parser->addFlag("help", ["alias" => "-h"]);

This way the help flag is available as --help and -h.

Flags don't expect values following them by default. To turn this on set the flag's has_value option to true:

<?php

$parser->addFlag("name", ["has_value" => true]);
$parser->parse(['--name', 'John']);

echo "Hello World {$parser["name"]}!\n";

You can assign a default value to a flag, by setting the default option:

<?php

$parser->addFlag("pid_file", ["default" => "/var/tmp/foo.pid", "has_value" => true]);

$parser->parse([]);

echo "{$parser["pid_file"]}\n";
// Output:
// /var/tmp/foo.pid

You can also bind the flag directly to a reference, by passing the reference in the var option or by using the addFlagVar method and passing it the variable:

<?php

$foo = null;
$bar = null;

$parser->addFlag("foo", ["var" => &$foo, "has_value" => true]);
$parser->addFlagVar("bar", $bar, ["has_value" => true]);

$parser->parse(['--foo', 'foo', '--bar', 'bar']);

echo "$foo\n";
echo "$bar\n";
// Output:
// foo
// bar

The parser also supports callbacks for flags. These are passed to addFlag as last argument. The callback is called everytime the parser encounters the flag. It gets passed a reference to the flag's value (true if it hasn't one). Use cases for this include splitting a string in pieces or running a method when a flag is passed:

<?php

$parser = new Parser;

function usage_and_exit()
{
    global $parser;
    echo $parser->usage(), "\n";
    exit;
}

$parser->addFlag("help", ['alias' => '-h'], "usage_and_exit");

$parser->addFlag("queues", ["has_value" => true], function(&$value) {
    $value = explode(',', $value);
});

The call to parse takes an array of arguments, or falls back to using the arguments from $_SERVER['argv']. The parse method throws an CHH\Optparse\ArgumentException when a required flag or argument is missing, so make sure to catch this Exception and provide the user with a nice error message.

The parser is also able to generate a usage message for the command by looking at the defined flags and arguments. Use the usage method to retrieve it.

Named Arguments

Named arguments can be added by using the addArgument method, which takes the argument's name as first argument and then an array of options.

These options are supported for arguments:

  • default: Default Value (default: null).
  • var_arg: Makes this a variable length argument (default: false).
  • help: Help text, used to describe the argument in the usage message generated by usage() (default: null).
  • required: Makes this argument required, the parser throws an exception when the argument is omitted in the argument list (default: false).

As opposed to flags, the order matters in which you define your arguments.

Variable length arguments can be defined by setting the var_arg option to true in the options array. Variable arguments can only be at the last position, and arguments defined after an variable argument are never set.

<?php

$parser->addArgument("files", ["var_arg" => true]);

// Will always be null, because the value will be consumed by the
// "var_arg" enabled argument.
$parser->addArgument("foo");

$parser->parse(["foo", "bar", "baz"]);

foreach ($parser["files"] as $file) {
    echo $file, "\n";
}
// Output:
// foo
// bar
// baz

Arguments can also be retrieved by using the args, arg and slice methods:

<?php

$parser->addArgument("foo");
$parser->parse(["foo", "bar", "baz"]);

echo var_export($parser->args());
// Output:
// array("foo", "bar", "baz")

// Can also be used to fetch named arguments:
echo var_export($parser->arg(0));
echo var_export($parser->arg("foo"));
// Output:
// "foo"
// "foo"

// Pass start and length:
echo var_export($parser->slice(0, 2));
// Output:
// array("foo", "bar");

License

Copyright (c) 2012 Christoph Hochstrasser

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.