From 0ae7435fcbd356b437a0302da1a31a500fe7047d Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 5 Jun 2013 13:34:48 -0500
Subject: [PATCH] Address issues from the cli framework

Include all the modules for `php manage.php --help` invocation. Also, pass
the parsed arguments and options to the Module::run() implementation.
---
 setup/cli/manage.php               |  9 +++++----
 setup/cli/modules/class.module.php | 21 ++++++++++++++++-----
 setup/cli/modules/unpack.php       |  2 +-
 3 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/setup/cli/manage.php b/setup/cli/manage.php
index c73831829..e11bfafeb 100755
--- a/setup/cli/manage.php
+++ b/setup/cli/manage.php
@@ -20,12 +20,12 @@ class Manager extends Module {
             include_once $script;
 
         global $registered_modules;
-        $this->epilog = 
+        $this->epilog =
             "Currently available modules follow. Use 'manage.php <module>
             --help' for usage regarding each respective module:";
 
         parent::showHelp();
-        
+
         echo "\n";
         foreach ($registered_modules as $name=>$mod)
             echo str_pad($name, 20) . $mod->prologue . "\n";
@@ -43,9 +43,10 @@ class Manager extends Module {
                 if ($val == $action)
                     unset($argv[$idx]);
 
-            include_once dirname(__file__) . '/modules/' . $action . '.php';
+            foreach (glob(dirname(__file__).'/modules/*.php') as $script)
+                include_once $script;
             $module = Module::getInstance($action);
-            $module->run();
+            $module->_run();
         }
     }
 }
diff --git a/setup/cli/modules/class.module.php b/setup/cli/modules/class.module.php
index de30f1632..501e6534e 100644
--- a/setup/cli/modules/class.module.php
+++ b/setup/cli/modules/class.module.php
@@ -1,7 +1,7 @@
 <?php
 
 class Option {
-    
+
     var $default = false;
 
     function Option() {
@@ -23,6 +23,8 @@ class Option {
             : null;
         $this->metavar = (isset($options['metavar'])) ? $options['metavar']
             : 'var';
+        $this->nargs = (isset($options['nargs'])) ? $options['nargs']
+            : 1;
     }
 
     function hasArg() {
@@ -87,6 +89,9 @@ class Module {
     var $usage = '$script [options] $args [arguments]';
     var $autohelp = true;
 
+    var $_options;
+    var $_args;
+
     function Module() {
         call_user_func_array(array($this, '__construct'), func_get_args());
     }
@@ -143,9 +148,8 @@ class Module {
 
     function getArgument($name, $default=false) {
         $this->parseOptions();
-        foreach (array_keys($this->arguments) as $idx=>$arg)
-            if ($arg == $name && isset($this->_args[$idx]))
-                return $this->_args[$idx];
+        if (isset($this->_args[$name]))
+            return $this->_args[$name];
         return $default;
     }
 
@@ -160,6 +164,8 @@ class Module {
         foreach (array_keys($this->arguments) as $idx=>$name)
             if (!isset($this->_args[$idx]))
                 $this->optionError($name . " is a required argument");
+            else
+                $this->_args[$name] = &$this->_args[$idx];
 
         if ($this->autohelp && $this->getOption('help')) {
             $this->showHelp();
@@ -173,7 +179,12 @@ class Module {
         die();
     }
 
-    /* abstract */ function run() {
+    function _run() {
+        $this->parseOptions();
+        return $this->run($this->_args, $this->_options);
+    }
+
+    /* abstract */ function run($args, $options) {
     }
 
     /* static */ function register($action, $class) {
diff --git a/setup/cli/modules/unpack.php b/setup/cli/modules/unpack.php
index 58fc70e4f..fca8f0ac2 100644
--- a/setup/cli/modules/unpack.php
+++ b/setup/cli/modules/unpack.php
@@ -114,7 +114,7 @@ class Unpacker extends Module {
         return INCLUDE_DIR;
     }
 
-    function run() {
+    function run($args, $options) {
         $this->destination = $this->getArgument('install-path');
         if (!is_dir($this->destination))
             if (!mkdir($this->destination, 0751, true))
-- 
GitLab