fix image text
[pear] / HTML / FlexyFramework / Cli.php
index 6250de6..3d6c4f6 100644 (file)
@@ -25,48 +25,13 @@ class HTML_FlexyFramework_Cli
     
       
     static $cli_opts = array(
+        
+        // this is a flag argument
         'pman-nodatabase' => array(
             'desc' => 'Turn off database',
-            //'default' => 0,
-            //'short' => 'v',
-            'min' => 0,
-            'max' => 1,
-        ),
-        
-        'source' => array(
-            'desc' => 'Source directory for json files.',
-            'short' => 'f',
-            'default' => '',
-            'min' => 1,
-            'max' => 1,
-        ),
-         'no-cache' => array(
-            'desc' => 'Do not cache.',
-            'short' => 'n',
-            'default' => 0,
-            'min' => 1,
-            'max' => 1,
-        ),
-        'list-order' => array(
-            'desc' => 'List Order.',
-            'short' => 'l',
-            'default' => 0,
-            'min' => 1,
-            'max' => 1,
-        ),
-         'table' => array(
-            'desc' => 'Only a specific table, eg. -t Netsuite_SalesOrder',
-            'short' => 't',
-            
-            'max' => 1,
-        ),
-         'year' => array(
-            'desc' => 'Only a specific year, eg. -y 2009',
-            'short' => 'y',
-            'default' => 2009,
-            'min' => 1,
-            'max' => 1,
-        ),
+            'max' => 0,
+        )
+         
     );
     
     
@@ -78,7 +43,7 @@ class HTML_FlexyFramework_Cli
     var $ff; // the Framework instance.
     
     
-    function HTML_FlexyFramework_Cli($ff)
+    function __construct($ff)
     {
         $this->ff = $ff;
     }
@@ -113,6 +78,12 @@ Available commands:
         $pr = $this->ff->project;
         
         $this->cliHelpSearch($p,$pr);
+        if (!empty($this->ff->projectExtends)) {
+            foreach($this->ff->projectExtends as $pr) {
+                $this->cliHelpSearch($p,$pr);
+            }
+        }
+        
         echo "\n\n";
         exit;
     }
@@ -218,17 +189,58 @@ Available commands:
     {
     
     // cli static $classname::$cli_opts
-       
+    
         try {
-            $cls = new ReflectionClass($classname);        
-            $val = $cls->getStaticPropertyValue('cli_opts');
-        } catch (Exception $e) {
-            return;
+            // look up the parent tree for core opts.
+            $val = array();
+            //var_dump($classname);
+            $cls = new ReflectionClass($classname);
+            if (method_exists($classname, 'cli_opts')) {
+                $val = $classname::cli_opts();
+            } else {
+                $ar = $cls->getStaticProperties();
+                 if (isset($ar['cli_opts'])) {
+                    //echo "getting cli opts?\n";
+                    $val = $cls->getStaticPropertyValue('cli_opts');
+                }
+            }
+             
+            $val = is_array($val) ? $val : array();
+            while ($cls = $cls->getParentClass()) {
+                //var_dump($cls->name);
+                 
+                try {
+                    $vadd  = array();
+                    if (method_exists($cls->name, 'cli_opts')) {
+                        $cn = $cls->name;
+                        $vadd = $cn::cli_opts();
+                    } else {
+                        $ar = $cls->getStaticProperties();
+                        if (isset($ar['cli_opts'])) {
+                            $vadd = $cls->getStaticPropertyValue('cli_opts');
+                        }
+                         
+                    }
+                    $val = array_merge($val, is_array($vadd) ? $vadd : array()  );
+                } catch (ReflectionException $e) {
+                    continue;
+                }
+            }
+            
+            
+            
+        } catch (ReflectionException $e) {
+            //print_r($e);
+            echo "cliParse:Warning:  {$e->getMessage()}\n";
+            exit;
         }
         if (empty($val)) {
             return false;
         }
         
+        $val = array_merge(self::$cli_opts, $val);
+        
+        
         require_once 'Console/Getargs.php';
         $ar = $_SERVER['argv'];
         $call = array(array_shift($ar)); // remove index.php
@@ -237,6 +249,7 @@ Available commands:
         
         $newargs = Console_Getargs::factory($val, $ar);
         
+  
         if (!is_a($newargs, 'PEAR_Error')) {
             return $newargs->getValues();
         }
@@ -273,7 +286,7 @@ Available commands:
      *  --pman-nodatabase=1 on the command line.
      *
      *  
-     *
+     * @returns   array() - args, false - nothing matched / invalid, true = help! 
      *
      */
     
@@ -281,15 +294,60 @@ Available commands:
     {
         require_once 'Console/Getargs.php';
         $ar = $_SERVER['argv'];
-        //var_dump($ar);
-        $val = $this->cli_opts;
+        $call = array(array_shift($ar)); // remove index.php
+        $has_class = false;
+        if (isset($ar[0]) && $ar[0][0] != '-') {
+            $call[] = array_shift($ar); // remove our class...
+            $has_class = true;
+        }  
+        $val = self::$cli_opts;
         
         $newargs = Console_Getargs::factory($val, $ar);
         
+        // we need to read our 'special arguments' here - otherwise other arguments, cause getargs to fail
+        switch (true) {
+            case in_array('--pman-nodatabase', $ar):
+                echo "Turning off database\n";
+                $this->ff->nodatabase= true;
+                    
+                break;
+             
+        }
+        
+        
+        
         if (!is_a($newargs, 'PEAR_Error')) {
-            return $newargs->getValues();
+            return false;
         }
+        list($optional, $required, $params) = Console_Getargs::getOptionalRequired($val);
+    
+        $helpHeader = 'Usage: php ' . implode (' ', $call) . ' '. 
+            $optional . ' ' . $required . ' ' . $params . "\n\n";           
+   
+        if ($newargs->getCode() === CONSOLE_GETARGS_ERROR_USER) {
+            // since we do not handle all the arguemnts here...
+            // skip errors if we find unknown arguments.
+            if (preg_match('/^Unknown argument/', $newargs->getMessage())) {
+                return false;
+            }
+            
+            // User put illegal values on the command line.
+            echo Console_Getargs::getHelp($val,
+                    $helpHeader, "\n\n".$newargs->getMessage(), 78, 4)."\n\n";
+            exit;
+        }
+        if ($newargs->getCode() === CONSOLE_GETARGS_HELP) {
+            if (!$has_class) {
+                
+                echo Console_Getargs::getHelp($val,
+                        $helpHeader, NULL, 78, 4)."\n\n";
+                exit;
+            }
+            return true;// help is handled later in the flow?
+        }
+        
         return false;
+     
         
     }