Skip to content
Snippets Groups Projects
Commit d87ddfd9 authored by Jared Hancock's avatar Jared Hancock
Browse files

Several fixes for the CLI manager and module class

parent 562591ac
Branches
Tags
No related merge requests found
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
require_once "modules/class.module.php"; require_once "modules/class.module.php";
if (!function_exists('noop')) { function noop() {} }
session_set_save_handler('noop','noop','noop','noop','noop','noop');
class Manager extends Module { class Manager extends Module {
var $prologue = var $prologue =
"Manage one or more osTicket installations"; "Manage one or more osTicket installations";
...@@ -13,7 +16,7 @@ class Manager extends Module { ...@@ -13,7 +16,7 @@ class Manager extends Module {
var $usage = '$script action [options] [arguments]'; var $usage = '$script action [options] [arguments]';
var $autohelp = true; var $autohelp = false;
function showHelp() { function showHelp() {
foreach (glob(dirname(__file__).'/modules/*.php') as $script) foreach (glob(dirname(__file__).'/modules/*.php') as $script)
...@@ -31,12 +34,12 @@ class Manager extends Module { ...@@ -31,12 +34,12 @@ class Manager extends Module {
echo str_pad($name, 20) . $mod->prologue . "\n"; echo str_pad($name, 20) . $mod->prologue . "\n";
} }
function run() { function run($args, $options) {
if ($this->getOption('help') && !$this->getArgument('action')) if ($options['help'] && !$args['action'])
$this->showHelp(); $this->showHelp();
else { else {
$action = $this->getArgument('action'); $action = $args['action'];
global $argv; global $argv;
foreach ($argv as $idx=>$val) foreach ($argv as $idx=>$val)
...@@ -45,8 +48,11 @@ class Manager extends Module { ...@@ -45,8 +48,11 @@ class Manager extends Module {
foreach (glob(dirname(__file__).'/modules/*.php') as $script) foreach (glob(dirname(__file__).'/modules/*.php') as $script)
include_once $script; include_once $script;
$module = Module::getInstance($action); if (($module = Module::getInstance($action)))
$module->_run(); return $module->_run($args['action']);
$this->stderr->write("Unknown action given\n");
$this->showHelp();
} }
} }
} }
...@@ -56,6 +62,6 @@ if (php_sapi_name() != "cli") ...@@ -56,6 +62,6 @@ if (php_sapi_name() != "cli")
$manager = new Manager(); $manager = new Manager();
$manager->parseOptions(); $manager->parseOptions();
$manager->run(); $manager->_run(basename(__file__));
?> ?>
...@@ -39,6 +39,8 @@ class Option { ...@@ -39,6 +39,8 @@ class Option {
$value = null; $value = null;
elseif ($value) elseif ($value)
$nargs = 1; $nargs = 1;
if ($this->type == 'int')
$value = (int)$value;
switch ($this->action) { switch ($this->action) {
case 'store_true': case 'store_true':
$value = true; $value = true;
...@@ -49,10 +51,17 @@ class Option { ...@@ -49,10 +51,17 @@ class Option {
case 'store_const': case 'store_const':
$value = $this->const; $value = $this->const;
break; break;
case 'append':
if (!isset($destination[$this->dest]))
$destination[$this->dest] = array($value);
else {
$T = &$destination[$this->dest];
$T[] = $value;
$value = $T;
}
break;
case 'store': case 'store':
default: default:
if ($this->type == 'int')
$value = (int)$value;
break; break;
} }
$destination[$this->dest] = $value; $destination[$this->dest] = $value;
...@@ -71,7 +80,7 @@ class Option { ...@@ -71,7 +80,7 @@ class Option {
else else
$switches = sprintf(" %s, %s", $short[0], $long[0]); $switches = sprintf(" %s, %s", $short[0], $long[0]);
$help = preg_replace('/\s+/', ' ', $this->help); $help = preg_replace('/\s+/', ' ', $this->help);
if (strlen($switches) > 24) if (strlen($switches) > 23)
$help = "\n" . str_repeat(" ", 24) . $help; $help = "\n" . str_repeat(" ", 24) . $help;
else else
$switches = str_pad($switches, 24); $switches = str_pad($switches, 24);
...@@ -103,6 +112,7 @@ class Module { ...@@ -103,6 +112,7 @@ class Module {
var $epilog = ""; var $epilog = "";
var $usage = '$script [options] $args [arguments]'; var $usage = '$script [options] $args [arguments]';
var $autohelp = true; var $autohelp = true;
var $module_name;
var $stdout; var $stdout;
var $stderr; var $stderr;
...@@ -128,11 +138,13 @@ class Module { ...@@ -128,11 +138,13 @@ class Module {
if ($this->prologue) if ($this->prologue)
echo $this->prologue . "\n\n"; echo $this->prologue . "\n\n";
echo "Usage:\n";
global $argv; global $argv;
$manager = @$argv[0];
echo "Usage:\n";
echo " " . str_replace( echo " " . str_replace(
array('$script', '$args'), array('$script', '$args'),
array($argv[0], implode(' ', array_keys($this->arguments))), array($manager ." ". $this->module_name, implode(' ', array_keys($this->arguments))),
$this->usage) . "\n"; $this->usage) . "\n";
ksort($this->options); ksort($this->options);
...@@ -205,15 +217,18 @@ class Module { ...@@ -205,15 +217,18 @@ class Module {
die(); die();
} }
function _run() { function _run($module_name) {
$this->module_name = $module_name;
$this->parseOptions(); $this->parseOptions();
return $this->run($this->_args, $this->_options); return $this->run($this->_args, $this->_options);
} }
/* abstract */ function run($args, $options) { /* abstract */
function run($args, $options) {
} }
/* static */ function register($action, $class) { /* static */
function register($action, $class) {
global $registered_modules; global $registered_modules;
$registered_modules[$action] = new $class(); $registered_modules[$action] = new $class();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment