shaneharter/PHP-Daemon — pull request #10 by wdalmut (2012-06-04 12:23:01+00:00)
Title: Improved Getopt

Hi,

I modify your ***getopt*** structure.

I think that in this new way is more simple adding personal rules for console parameters.

I decoupled the configuration and the analysis of console parameters in two methods:

* getopt()
* Configuration
* work_on_getopt
* Run the console parameter analysis

With this strategy we can add rules using an overloaded `getopt()` method for personal configuration and `work_on_getopt()` for parameter configuration.

A complete example (Namespace_MyClass extends Core_Daemon):

```php
_opts->addRules(
array("c=s" => 'Select the configuration file.')
);
}

// Add new rules for options
protected function work_on_getopt()
{
parent::work_on_getopt();

$opts = $this->_opts;

if (isset($opts['c'])) {
$this->_configFile = $opts["c"];
} else {
echo "You must provide a valid configuration file.";
exit();
}
}
// more...
}
```

## New `getopt` features

Examples:
* 'user|username|u=s' this means '--user' or '--username' or '-u' are synonyms, and the option requires a string parameter.
* 'p=i' this means '-p' requires an integer parameter. No synonyms.
* 'verbose|v-i' this means '--verbose' or '-v' are synonyms, and they take an optional integer parameter.
* 'help|h' this means '--help' or '-h' are synonyms, and they take no parameter.

## Conclusions

All previously procedures with `getopt()` are preserved and works correctly.

The Getopt class is a derivation of Zend_Console_Getopt refactored for this project.

I hope that it's useful.

Bye
Walter