ulrichsg/getopt-php — pull request #33 by kamermans (2014-08-03 23:21:43+00:00)
Title: Added fluent-style static constructor helper

Instantiating a new option and setting its properties in one step is currently not possible for most versions of PHP, since you can't put `->` after constructing an object via `new ...`. This means that `new Option('a', null)->setDefaultValue(10)` will not work.

Here's the nasty mess I had to use for my application:

$options = array();

$opt = new Option('s', 'strict', Getopt::REQUIRED_ARGUMENT);
$options[] = $opt->setDescription('Use strict comparison of capability values')
->setDefaultValue('true')
->setValidation(function($value) {
return in_array($value, array('true', 'false'));
});

$opt = new Option('d', 'details', Getopt::REQUIRED_ARGUMENT);
$options[] = $opt->setDescription('Include lists of all devices and capabilities instead of just the counts')
->setDefaultValue('false')
->setValidation(function($value) {
return in_array($value, array('true', 'false'));
});

$getopt = new Getopt($options);

To improve things, I've added a public static function `create()` which constructs a new `Option` object and returns it. This lets me improve my syntax as follows:

$getopt = new Getopt(array(
Option::create('s', 'strict', Getopt::REQUIRED_ARGUMENT)
->setDescription('Use strict comparison of capability values')
->setDefaultValue('true')
->setValidation(function($value) {
return in_array($value, array('true', 'false'));
}),

Option::create('d', 'details', Getopt::REQUIRED_ARGUMENT)
->setDescription('Include lists of all devices and capabilities instead of just the counts')
->setDefaultValue('false')
->setValidation(function($value) {
return in_array($value, array('true', 'false'));
}),
));