A little something I threw together today in support of a CLI script I wrote to parse some httpd logs. This part is just for dealing with the command line args. One shortcoming is that it requires that if you have both a -x and --xname parameter, they must be for the same attribute; but sticking to that convention keeps it pretty clean/simple. Right now I have the possible arg names hard-coded in the constructor. To generalize it, I'll probably change it to accept some sort of input array.
/**
* Class CLIArgs
* Handle command line args
*/
class CLIArgs
{
private $args = array();
/**
* CLIArgs constructor
* @todo generalize by having options be an input param
*/
public function __construct()
{
$this->args = getopt('d:t:h', array('dir:', 'time:', 'help'));
}
/**
* @param string $name long name of parameter (e.g. 'help'
* @return null|mixed null if arg not found in request
*/
public function getArg($name)
{
if(isset($this->args[$name])) {
return $this->args[$name];
}
$letter = substr($name, 0, 1);
if(isset($this->args[$letter])) {
return $this->args[$letter];
}
return null;
}
}