This class is a first step to a comprehensive, and yet handy little collection of helper-functions, which can ease your way along while using php for shell-scripts.
Whereas most of you probably use php as a scripting-language for websites, it´s also a very good alternative to other shell-scripting languages like perl or python. In fact, I find it a lot more useful than regular shell-scripts.
This little class can be used both as a static collection of shell-methods, but you can also create instances of it.
So far, it automatically parses any userparams coming from the commandline, and stores them in a publicly available array named self::$arguments.
More features are to be added soon
<?
/* ----------------------------------------------------- */
/* coding: timor kodal [pulsar-network] */
/* copyright by: (c) 2010 timor kodal */
/* ----------------------------------------------------- */
/* contact: [http://pulsar.cc | timor [at] kodal.de */
/* ----------------------------------------------------- */
/* to see this and other scripts in action, please visit */
/* http://pulsar.cc */
/* ----------------------------------------------------- */
/* you are free to use my scripts */
/* according to the creative commons licence cc-nc-by */
/* ----------------------------------------------------- */
/* more information: http://creativecommons.org */
/* ----------------------------------------------------- */
class shell {
public $arguments;
public $defaults;
public $aliases;
function __construct (Array $args = NULL,Array $aliases = NULL,Array $defaults = NULL) {
$this->aliases = $aliases;
$this->defaults = $defaults;
$this->arguments = self::parseArguments($args,$aliases,$defaults);
}
public function getArgs(Array $defaults = NULL) {
$args = $this->arguments;
if ($defaults) {
foreach ($defaults as $k=>$v) {
if (!isset($args[$k]))
$args[$k]=$v;
if (!isset($args[$this->aliases[$k]]) && $this->aliases[$k])
$args[$this->aliases[$k]]=$v;
}
}
return $args;
}
public static function parseArguments (Array $args = NULL, Array $aliases = NULL,Array $defaults = NULL) {
global $argv;
if (!$aliases)
$aliases = Array ("u"=>"user");
if (!$args)
$args=$argv;
$arguments = Array();
$count = 0;
// -u=1
// -u test
// --user=1
if (is_array($defaults)) {
foreach ($defaults as $k=>$v) {
$arguments[$k]=$v;
if ($aliases[$k])
$arguments[$aliases[$k]]=$v;
}
}
foreach ($args as $k=>$v) {
$pattern = "/\-{1,2}([a-z]+)=(.*)/";
preg_match($pattern,$v,$matches);
if ($matches[0]) {
$arguments[$matches[1]]=$matches[2];
if ($aliases[$matches[1]])
$arguments[$aliases[$matches[1]]]=$matches[2];
} else
$arguments[]=$v;
}
return $arguments;
}
}
?>
download class.shell.php
Example:
<?
require_once("class.shell.php");
$aliases = Array("u"=>"uid","d"=>"default");
$shell = new shell(NULL,$aliases,Array("u"=>"root"));
print_r($shell->getArgs(Array("d"=>"yes")));
// php5 example.shell.php -user=teamore
// output:
// Array
// (
// [u] => root
// [uid] => root
// [0] => initprocmail.php
// [user] => teamore
// [d] => yes
// [default] => yes
// )
?>


Write a Comment