.gitignore000064400000000036144761607260006550 0ustar00/vendor/ /tests/ composer.lockcomposer.json000064400000000677144761607260007315 0ustar00{ "name": "boru/dhcli", "type": "library", "autoload": { "psr-4": { "boru\\dhcli\\": "src/" } }, "authors": [ { "name": "Daniel Hayes", "email": "dhayes@boruapps.com" } ], "require": { "boru/dhutils": "*" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] } instructions-composer.txt000064400000000311144761607260011706 0ustar00{ "require": { "boru/dhcli": "dev-master" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] }src/ClassCommandInterface.php000064400000000372144761607260012250 0ustar00"; protected $maxlenLong = 0; protected $maxlenShort = 0; protected $valPlaceHolderLen = 0; protected $dhargs; /** * all args from $dhargs */ protected $argsAll = []; /** @var Args */ private $args; /** * unused args from $dhargs */ protected $argsUnused = []; protected $callback; /** * Shortcut to create a new CLI Command * @param mixed $command The command name, used to execute * @param mixed $description Description of the command, used in help * @param \boru\dhcli\Option[] $options array of \boru\dhcli\Option objects * @param array $extraConf additional configuration items, if any. (commonOptions,valPlaceHolder,callback) * @return static */ public static function create($command,$description,$options=[],$extraConf=[]) { $arr = ["command"=>$command,"description"=>$description]; if(!empty($options)) { $arr["opts"]=$options; } if(!empty($extraConf)) { foreach($extraConf as $k=>$v) { $arr[$k]=$v; } } return new static($arr); } public function __construct($options=[]) { $this->command = dhGlobal::getMultiVal($options,["name"=>false,"command"=>false],true); $this->description = dhGlobal::getVal($options,"description",""); $this->valPlaceHolder = dhGlobal::getVal($options,"valPlaceHolder",""); $this->valPlaceHolderLen = strlen($this->valPlaceHolder); $opts = dhGlobal::getMultiVal($options,["opts"=>false,"opt"=>false,"options"=>false,"option"=>false,"commonOptions"=>false,"commonOpts"=>false]); foreach($opts as $optVal) { if($optVal !== false) { if(is_array($optVal)) { $this->options($optVal); } elseif(is_array($optVal) || is_object($optVal)) { $this->option($optVal); } } } $this->callback = dhGlobal::getVal($options,"callback",null); $default = dhGlobal::getVal($options,"default",false); if($default !== false) { $this->isDefaultCommand = true; } } public function name() { return $this->command; } public function addNewOpt($array) { $opt = new Option($array); if($opt->getName() != "") { $this->addOpt($opt); } return $this; } public function options(array $opts) { foreach($opts as $opt) { if(is_array($opt)) { $this->addNewOpt($opt); } elseif(is_object($opt)) { $this->option($opt); } } return $this; } public function option($opt) { if(is_array($opt)) { $this->addNewOpt($opt); } elseif(is_object($opt)) { return $this->addOpt($opt); } } public function addOpt(Option $opt) { $this->options[$opt->getName()] = $opt; if($opt->isRequired()) { $this->optsRequired[$opt->getName()] = $opt->getName(); } else { $this->optsOptional[$opt->getName()] = $opt->getName(); } if($opt->isLong()) { $len = strlen($opt->getLong()); if($opt->needsValue()) { $len+=$this->valPlaceHolderLen+1; } $this->maxlenLong = max($this->maxlenLong,$len+2); } if($opt->isShort()) { $len = strlen($opt->getShort()); if($opt->needsValue()) { $len+=$this->valPlaceHolderLen+2; } else { $this->optsFlags[$opt->getName()] = $opt->getName(); } $this->maxlenShort = max($this->maxlenShort,$len+1); } return $this; } public function isValid() { foreach($this->options as $name=>$opt) { if($opt->isRequired()) { if(!$opt->hasValue()) { $this->errors[$name] = "required"; } } if($opt->needsValue() && !empty($opt->getInvalidOptions())) { $this->errors[$name] = "invalid value"; } elseif($opt->needsValue() && $opt->getValue() === false) { $this->errors[$name] = "missing value"; } } return empty($this->errors); } public function getOptionValues() { if(!$this->isValid()) { return false; } if(is_null($this->optionValues) || empty($this->optionValues)) { foreach($this->options as $name=>$opt) { if($opt->hasValue()) { $this->optionValues[$name] = $opt->getValue(); } } } return $this->optionValues; } public function getErrors() { return empty($this->errors) ? false : $this->errors; } public function getErrorOutput($syntaxPrefix="") { $this->syntax($syntaxPrefix); dhGlobal::outLine(""); if(($errors = $this->getErrors()) !== false) { foreach($errors as $opt=>$err) { $opt = $this->options[$opt]; if($err == "required") { $info = ""; if($opt->isShort()) { $info .= $opt->getInputShort()." "; } if($opt->isLong()) { $info .= $opt->getInputLong(); } dhGlobal::outLine("Missing required option:",$info); } elseif($err == "missing value") { $info = ""; if($opt->isShort()) { $info .= $opt->getInputShort()." "; } if($opt->isLong()) { $info .= $opt->getInputLong(); } dhGlobal::outLine("Missing value for non-required option:",$info); } elseif($err == "invalid value") { $invalidOptions = $opt->getInvalidOptions(); $selArg = $opt->getParsedArg(); if(is_array($selArg)) { $selArg = $selArg[0]; } foreach($invalidOptions as $invalidOpt) { dhGlobal::outLine("$selArg: invalid value of \"".$invalidOpt["value"]."\" -- ".$invalidOpt["reason"]); } } } } } public function help($syntaxPrefix="") { dhGlobal::outLine($this->name()); $this->syntax("Usage: ".$syntaxPrefix," "); dhGlobal::outLine(" {$this->description}"); if(!empty($this->optsRequired)) { dhGlobal::outLine(" * Required Options:"); foreach($this->optsRequired as $optName) { $this->_printOpt($this->options[$optName]); } } if(!empty($this->optsOptional)) { dhGlobal::outLine(" * Optional Options:"); foreach($this->optsOptional as $optName) { $this->_printOpt($this->options[$optName]); } } } public function syntax($syntaxPrefix="",$indent="") { dhGlobal::outLine($indent.$syntaxPrefix."{$this->command} ".$this->getSyntaxArgs()); } public function getSyntaxArgs() { $syntaxArgs = []; if(!empty($this->optsRequired)) { foreach($this->optsRequired as $optName) { if($this->options[$optName]->isPositional()) { $syntaxArgs[] = "<$optName>"; } elseif($this->options[$optName]->isShort()) { $syntaxArgs[] = "-".$this->options[$optName]->getShort().$this->valPlaceHolder; } else { $syntaxArgs[] = "--".$this->options[$optName]->getLong()."=".$this->valPlaceHolder; } } } if(!empty($this->optsOptional)) { $syntaxArgs[] = "[optionals]"; } return implode(" ",$syntaxArgs); } public function _printOpt(Option $opt) { $val = $opt->needsValue(); $short = str_pad("",$this->maxlenShort," ",STR_PAD_RIGHT); $long = str_pad("",$this->maxlenLong," ",STR_PAD_RIGHT); $hasShort = $opt->isShort(); $hasLong = $opt->isLong(); if($opt->isPositional()) { if($opt->isRequired()) { $long = str_pad("<".$opt->getLong().">",$this->maxlenLong," ",STR_PAD_RIGHT); } else { $long = str_pad("[".$opt->getLong()."]",$this->maxlenLong," ",STR_PAD_RIGHT); } } elseif($val) { if($opt->isShort()) { $short = $opt->getShort().' '.$this->valPlaceHolder; if($hasLong) { $short.=","; } $short = str_pad("-".$short,$this->maxlenShort," ",STR_PAD_RIGHT); } if($opt->isLong()) { $long = str_pad("--".$opt->getLong()."=".$this->valPlaceHolder,$this->maxlenLong," ",STR_PAD_RIGHT); } } else { if($opt->isShort()) { $short = $opt->getShort(); if($hasLong) { $short.=","; } $short = str_pad("-".$short,$this->maxlenShort," ",STR_PAD_RIGHT); } if($opt->isLong()) { $long = str_pad("--".$opt->getLong(),$this->maxlenLong," ",STR_PAD_RIGHT); } } //$name = $opt->getName(); $desc = $opt->getDescription(); dhGlobal::outLine(" ",$short," ",$long," "," -- ",$desc); $subOpts = []; if(!empty($opt->getValue())) { $subOpts[] = "(Default: ".$opt->getValue().")"; } if(!empty($opt->getOptions())) { $subOpts[] = "[".implode("|",$opt->getOptions())."]"; } if(!empty($subOpts)) { $s = str_pad(" ",$this->maxlenShort," "); $l = str_pad(" ",$this->maxlenLong," "); dhGlobal::outLine(" ",$s," ",$l," "," -- ",implode(" ",$subOpts)); } } public function process($args) { if(!is_object($args)) { if(is_array($args) || is_string($args)) { $this->args = new Args($args); } else { throw new \Exception("Invalid args for ".__CLASS__."->process()"); } } else { $this->args = $args; } $this->dhargs = $args; $this->setArgs($args->getArgs(true),true); $positionalOptions = []; foreach($this->options as $name=>$opt) { if($opt->isPositional()) { $positionalOptions[] = $opt; } else { $this->parseOptionArgs($this->options[$name]); } } $this->setArgs($args->getArgs()); if(!empty($positionalOptions)) { foreach($positionalOptions as $k=>$opt) { $this->parsePositionalArgs($k,$opt); } } if(!$this->isValid()) { $this->setStatus(false); return false; } foreach($this->options as $name=>$opt) { if($opt->hasValue()) { $this->optionValues[$name] = $opt->getValue(); } } $this->setStatus(true); if(!is_null($this->callback)) { $var = $this->callback; $callbackResult = $var($this->getResult()); if(!is_null($callbackResult) && $callbackResult !== false && $callbackResult !== true) { if(is_array($callbackResult)) { foreach($callbackResult as $line) { dhGlobal::outLine($line); } } else { dhGlobal::outLine($$callbackResult); } } $this->callbackUsed = true; return true; } //returned because there was no callback return null; } public function getResult() { $result = [ "commandName"=>$this->name(), "command"=>&$this, "options"=>$this->getOptionValues(), "args"=>[ "all"=>$this->getArgs(true), "unused"=>$this->getArgs(false), ] ]; return $result; } /** * Get the value of status */ public function getStatus() { return $this->status; } /** * Set the value of status * * @return self */ public function setStatus($status) { $this->status = $status; return $this; } public function setArgs($args,$all=false) { if(!$all) { $this->argsUnused = $args; } else { $this->argsAll = $args; } } public function getArgs($all=false) { if(!$all) { return $this->argsUnused; } else { return $this->argsAll; } } public function getOption($key,$default=false) { return isset($this->optionValues[$key]) ? $this->optionValues[$key] : $default; } public function parsePositionalArgs($pos,Option &$opt) { if(isset($this->argsUnused[$pos])) { $opt->setValue($this->argsUnused[$pos]); unset($this->argsUnused[$pos]); } } public function parseOptionArgs(Option &$opt) { $optName = $opt->getName(); //reset the cursor $this->args->cursor=0; //loop through all the args and process any options we have defined while(($arg = $this->args->next()) !== false) { if($this->isOptArg($arg,$opt)) { $this->args->markUsed(); if($this->args->isLongArg($arg)) { $opt->setParsedArg("--".$opt->getLong()); //$this->args->debug("isLongArg $arg"); if($opt->needsValue()) { if(($value = $this->args->getArgValue($arg)) !== false) { //$this->debug("arg found, setting value to $value"); $opt->setValue($value); } } else { //$this->debug("$arg found"); $opt->setValue(true); } } elseif($this->args->isShortArg($arg)) { $opt->setParsedArg("-".$opt->getShort()); if($opt->needsValue()) { if(($value = $this->args->getArgValue($arg)) !== false) { //$this->debug("arg found, setting value to $value"); $opt->setValue($value); } } else { //$this->debug("$arg found"); $opt->setValue(true); } } } } return $this; } public function isOptArg($arg,$opt) { if(($long = $opt->getLong()) !== false) { $o = substr($arg,2); $p = strpos($o, '='); if($p) { dhGlobal::info("isLong"); return $long == substr($o, 0, $p); } } if(($short = $opt->getShort()) !== false && substr($arg,0,1) == "-") { $o = substr($arg,1); if(!$opt->needsValue()) { //if it doesn't need a value //eg -s if($short == $o) { return true; } //if we don't need a value, we may be a flag that can be grouped //eg -sTmv if(strlen($o)>1) { for($i=0;$iargs->getArgValue($arg)) { return false; } if($short == $o[0]) { return true; } return false; } } if($this->args->isLongArg($arg) || $this->args->isShortArg($arg)) { } return false; } }src/Option.php000064400000026127144761607260007341 0ustar00$name,"description"=>$description]; $opts["required"] = $required; $opts["positional"]=true; if(!empty($extraConfig)) { foreach($extraConfig as $k=>$v) { $opts[$k] = $v; } } return new static($opts); } /** * Returns a new Option configured for flags (no value) * @param string $description description of the option * @param string $short short arg * @param string $long long arg name * @param array $extraConfig additional options (default,valueFormat,options) * @return static */ public static function flag($short,$long,$description,$extraConfig=[]) { $opts = ["short"=>$short,"long"=>$long,"description"=>$description]; $opts["needsValue"] = false; $opts["required"] = false; $opts["multiple"] = false; if(!empty($extraConfig)) { foreach($extraConfig as $k=>$v) { $opts[$k] = $v; } } return new static($opts); } /** * Returns a new Option configured as a single value option * @param string $description description of the option * @param string $short short arg * @param string $long long arg name * @param bool $required * @param array $extraConfig additional options (default,valueFormat,options) * @return static */ public static function option($short,$long,$description,$required=true,$extraConfig=[]) { $opts = ["short"=>$short,"long"=>$long,"description"=>$description]; $opts["needsValue"] = true; $opts["required"] = $required; $opts["multiple"] = false; if(!empty($extraConfig)) { foreach($extraConfig as $k=>$v) { $opts[$k] = $v; } } return new static($opts); } /** * Returns a new Option configured as a multi-value option * @param string $description description of the option * @param string $short short arg * @param string $long long arg name * @param bool $required * @param array $extraConfig additional options (default,valueFormat,options) * @return static */ public static function multiOption($short,$long,$description,$required=true,$extraConfig=[]) { $opts = ["short"=>$short,"long"=>$long,"description"=>$description]; $opts["needsValue"] = true; $opts["required"] = $required; $opts["multiple"] = true; if(!empty($extraConfig)) { foreach($extraConfig as $k=>$v) { $opts[$k] = $v; } } return new static($opts); } public function __construct($arrayOptions=[]) { if(!empty($arrayOptions) && is_array($arrayOptions)) { if(isset($arrayOptions["name"])) { $this->name = $arrayOptions["name"]; } if(isset($arrayOptions["short"])) { $this->setShort($arrayOptions["short"]); } if(isset($arrayOptions["long"])) { $this->setLong($arrayOptions["long"]); } if(isset($arrayOptions["description"])) { $this->setDescription($arrayOptions["description"]); } if(isset($arrayOptions["required"])) { $this->setRequired($arrayOptions["required"]); } if(isset($arrayOptions["multiple"])) { $this->setMultiple($arrayOptions["multiple"]); } if(isset($arrayOptions["needsValue"])) { $this->setNeedsValue($arrayOptions["needsValue"]); } if(isset($arrayOptions["options"])) { $this->setOptions($arrayOptions["options"]); } if(isset($arrayOptions["default"])) { $this->setValue($arrayOptions["default"]); } if(isset($arrayOptions["valueFormat"])) { $this->setValueFormat($arrayOptions["valueFormat"]); } if(isset($arrayOptions["positional"])) { $this->setIsPositional($arrayOptions["positional"]); } } } public function getInputShort() { if(!$this->isShort()) { return false; } if($this->needsValue()) { return "-".$this->getShort().""; } else { return "-".$this->getShort(); } } public function getInputLong() { if(!$this->isLong()) { return false; } if($this->needsValue()) { return "--".$this->getLong()."="; } else { return "--".$this->getLong(); } } //Boolean checks public function isShort() { if($this->isPositional) { return false; } if(!empty($this->short) && !is_null($this->short)) { return true; } return false; } public function isLong() { if($this->isPositional) { return false; } if(!empty($this->long) && !is_null($this->long)) { return true; } return false; } public function isPositional() { return $this->isPositional; } public function isRequired() { return $this->required ? true : false; } public function isMultiple() { return $this->multiple ? true : false; } public function needsValue() { return $this->needsValue ? true : false; } public function hasValue() { return !is_null($this->value); } public function isValidOption($value) { if(empty($this->options)) { if(empty($this->valueFormat)) { return true; } if(preg_match($this->valueFormat,$value) === 1) { return true; } else { $this->invalidOptions[] = array("value"=>$value,"reason"=>"invalid format, must match {$this->valueFormat} ".preg_match($this->valueFormat,$value)); return false; } } if(in_array($value,$this->options)) { return true; } else { $this->invalidOptions[] = array("value"=>$value,"reason"=>"must be one of [".implode(', ',$this->options)."])"); return false; } return in_array($value,$this->options); } //Getters and Setters public function getName() { if(empty($this->name)) { if(($long = $this->getLong()) !== false) { return $long; } return $this->getShort(); } return $this->name; } public function getShort() { return !empty($this->short) ? $this->short : false; } public function setShort($short) { $this->short = $short; return $this; } public function getLong() { return !empty($this->long) ? $this->long : false; } public function setLong($long) { $this->long = $long; return $this; } public function getDescription() { return $this->description; } public function setDescription($description) { $this->description=$description; return $this; } public function setRequired($req) { $this->required = $req; return $this; } public function setNeedsValue($nv) { $this->needsValue = $nv; return $this; } public function getValue() { return $this->value; } public function setValue($value) { if($this->isValidOption($value)) { if($this->isMultiple()) { if(!is_array($this->value)) { if(!is_null($this->value)) { $this->value = [$this->value,$value]; } else { $this->value = $value; } } else { $this->value[] = $value; } } else { $this->value = $value; } return $this; } //throw here? return false; } public function getOptions() { return $this->options; } public function setOptions($options) { $this->options = $options; return $this; } /** * Get the value of multiple */ public function getMultiple() { return $this->multiple; } /** * Set the value of multiple * * @return self */ public function setMultiple($multiple) { $this->multiple = $multiple; return $this; } /** * Get the value of valueFormat */ public function getValueFormat() { return $this->valueFormat; } /** * Set the value of valueFormat * * @return self */ public function setValueFormat($valueFormat) { $this->valueFormat = $valueFormat; return $this; } /** * Get the value of invalidOptions */ public function getInvalidOptions() { return $this->invalidOptions; } /** * Set the value of invalidOptions * * @return self */ public function setInvalidOptions($invalidOptions) { $this->invalidOptions = $invalidOptions; return $this; } /** * Get the value of parsedArg */ public function getParsedArg() { return $this->parsedArg; } /** * Set the value of parsedArg * * @return self */ public function setParsedArg($parsedArg) { if(!is_null($this->parsedArg)) { if(!is_array($this->parsedArg)) { $this->parsedArg = [$this->parsedArg]; } $this->parsedArg[] = $parsedArg; } else { $this->parsedArg = $parsedArg; } return $this; } /** * Get the value of isPositional * @return mixed */ public function getIsPositional() { return $this->isPositional; } /** * Set the value of isPositional * @param mixed $isPositional * @return self */ public function setIsPositional($isPositional) { $this->isPositional = $isPositional; return $this; } }src/dhCLI.php000064400000024652144761607260007015 0ustar00name = $this->getOption($options,"name",null); $this->description = $this->getOption($options,"description",null); $this->prescript = $this->getOption($options,"script",null); $prefix = $this->getOption($options,"prefix",false); dhGlobal::out()->setPrefix($prefix); if(($prefixFormat = $this->getOption($options,"prefix",false))!==false) { dhGlobal::out()->setPrefixFormat($prefixFormat); } if(!is_null($cmdObj)) { $this->cmdObj = &$cmdObj; } } public function classCommand($className) { if(class_exists($className)) { $this->command(Command::create($className::commandName(),$className::commandDescription(),$className::commandOptions(),['callback'=>function($result) use ($className) { $instance = new $className($result); }])); } } public function command(Command $command) { $this->commands[$command->name()] = $command; if($command->isDefaultCommand) { $this->setDefaultCommand($this->commands[$command->name()]); } } public function createOptFromArray($arrayOptions=[]) { $opt = new Option($arrayOptions); return $opt; } public function addCommandOpt($command,$opts) { if(is_object($opts)) { $this->commands[$command]->addOpt($opts); } elseif(is_array($opts)) { foreach($opts as $opt) { if(is_object($opt)) { $this->commands[$command]->addOpt($opt); } } } } public function addCommandOptFromArray($command,$arrayOptions=[]) { if(is_array($arrayOptions)) { $opt = $this->createOptFromArray($arrayOptions); if($opt->name != "") { $this->addCommandOpt($command,$opt); } } } public function commandStandard($command="",$description="",$opts="") { $this->commands[$command] = new Command(["command"=>$command,"description"=>$description]); if(!empty($opts)) { $this->addCommandOpt($command,$opts); } return $this->commands[$command]; } public function process($input,$separator=" ") { $args = $input; if(!is_array($input)) { $args = explode($separator,$input); } //print_r($args); if(empty($args) && $this->defaultCommand !== false) { $this->command = $this->defaultCommand; } else { if(empty($args) || $args[0] == "-h" || $args[0] == "--help" || $args[0] == "help") { $this->printBanner(); $cmdFilter = ""; if(isset($args[1])) { $cmdFilter = $args[1]; } $this->help($cmdFilter); $this->setStatus(-1); return -1; } if(!isset($this->commands[$args[0]])) { $this->printBanner(); dhGlobal::outLine("Unknown command: {$args[0]}"); $this->setStatus(-1); return -1; } $this->command = $this->commands[$args[0]]; array_shift($args); } $dhargs = new Args($args); $worked = $this->command->process($dhargs); if($this->command->getStatus() === true) { if(!is_null($this->cmdObj)) { if(method_exists($this->cmdObj,"commandRun")) { $this->cmdObj->commandRun($this->command->getResult()); $worked = true; } } $this->setStatus(1); if(is_null($worked)) { dhGlobal::info($this->command->name(),"processed but had no execution"); } return 1; } $this->setStatus(0); $this->printErrors(); return 0; } public function help($commandFilter="") { dhGlobal::outLine("Syntax: {$this->prescript} [options]"); dhGlobal::outLine(""); if(!empty($commandFilter) && isset($this->commands[$commandFilter])) { $this->commands[$commandFilter]->help($this->prescript); } else { dhGlobal::outLine("Available commands:"); $first=true; foreach($this->commands as $cmdName=>$cmd) { dhGlobal::outLine(" ",$cmdName,"-",$cmd->description); } dhGlobal::outLine(""); dhGlobal::outLine("use 'help ' for more information"); } } public function getCommandHelp($command="") { if(!empty($command)) { if(!isset($this->commands[$command])) { return false; } } } public function getResult() { if($this->getStatus()>=1) { $arr = []; $arr["command"] = $this->command->command; $arr["options"] = $this->command->getOptionValues(); $arr["args"]["all"] = $this->command->getArgs(true); $arr["args"]["unused"] = $this->command->getArgs(false); $arr["callbackUsed"] = $this->command->callbackUsed; return $arr; } return false; } public function printErrors() { if($this->getStatus() === -1) { return -1; } elseif($this->getStatus() === 0) { $this->printBanner(); $this->command->getErrorOutput($this->prescript); return 1; } return 0; } public function printName() { dhGlobal::outLine($this->name); } public function printDesc() { dhGlobal::outLine($this->description); } public function printBanner() { dhGlobal::outLine(""); $this->printName(); $this->printDesc(); dhGlobal::outLine(""); } public function resetOutput() { //$this->dhoutput = dhGlobal::out(); } /** * Get the value of command */ public function getCommand() { return $this->command; } /** * Set the value of command * * @return self */ public function setCommand($command) { $this->command = $command; return $this; } /** * Get status of the process execution */ public function getStatus() { return $this->status; } /** * Set status of the process execution * * @return self */ public function setStatus($status) { $this->status = $status; return $this; } /** * Get the value of defaultCommand */ public function getDefaultCommand() { return $this->defaultCommand; } /** * Set the value of defaultCommand * * @return self */ public function setDefaultCommand(&$defaultCommand) { $this->defaultCommand = &$defaultCommand; return $this; } public static function init($name,$description=null,$classCommands=[],$args=null,$script=null) { if(is_null($args)) { global $argv; $args = $argv; } if(is_null($script)) { $sn = array_shift($args); $script = "php -f $sn "; } if(is_null($description)) { $description = "No description set"; } static::$dhcli = new self(["name"=>$name,"description"=>$description,"script"=>$script]); foreach($classCommands as $className) { static::$dhcli->classCommand($className); } return static::$dhcli->process($args); } }test/init.php000064400000000073144761607260007214 0ustar00"dhCLI Test 1.0", "description"=>"A testing script to process commands", "script"=>"php -f test.php " ],$testObj); $dhcli->command( dhCLI::makeCommand([ "command"=>"run", "description"=>"a good description goes here", "opts"=>[ dhCLI::makeOption([ "short"=>"b", "long"=>"book", "needsValue"=>true, "required"=>true, "multiple"=>false, "valueFormat"=>"/^[0-9]+$/", "description"=>"The playbook.yml file to execute" ]), dhCLI::makeOption([ "short"=>"t", "long"=>"target", "needsValue"=>true, "required"=>true, "description"=>"The server(s) or groups to run the playbook on" ]), ], "commonOpts" => [ dhCLI::makeOption([ "short"=>"n", "long"=>"noparse", "description"=>"Don't parse the log.. returns ansible's direct output" ]) ], "callback"=>function($result) { dhGlobal::debug("callback for run"); }, ]) ); $dhcli->command( dhCLI::makeCommand([ "command"=>"list", "description"=>"list playbooks", "callback"=>function($result) { dhGlobal::debug("callback for list"); } ]) ); //echo $dhcli->help(); $status = $dhcli->process($argv); if($status <= 0) { //error, we printed errors since it's CLI } else { //success, we did something already.. } /* $argstring = "-s -t abc --thisisfun"; $args = new dhcli_args($argstring); $args->setDebug(true); $args->parse($opt); if($opt->hasValue()) { echo "We are set\n"; echo $opt->getValue()."\n"; } */