home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Web Server / PHP.EXE / pear / PHPDoc / core / PhpdocArgvHandler.php < prev    next >
Encoding:
PHP Script  |  2001-02-18  |  5.5 KB  |  161 lines

  1. <?php
  2. /**
  3. * Handles command line arguments.
  4. *
  5. * Be careful the source has not been tested yet, it's probably very buggy.
  6. * Any help and comments are welcome...
  7. *
  8. * @author   Ulf Wendel <ulf@redsys.de>
  9. * @version  $Id: PhpdocArgvHandler.php,v 1.3 2001/02/18 15:29:29 uw Exp $
  10. */
  11. class PhpdocArgvHandler extends PhpdocObject {
  12.     
  13.     /**
  14.     * Message explaining the usage of phpdoc on the command line.
  15.     * 
  16.     * Actually it's not the message itself but an array containing
  17.     * the instructions. The array is indexed by the command line option e.g. "-h".
  18.     * The array values hold a short message describing the  usage of the option.
  19.     * 
  20.     * @var        array
  21.     * @access    private
  22.     */
  23.     var $COMMANDS = array(
  24.                             "-f filename [, filename]"  => "name of files to parse",
  25.                             "-d directory"              => "name of a directory to parse",
  26.                             "-p path"                   => "path of the files",
  27.                             "-t target"                 => "path where to save the generated files, default is the current path",
  28.                             "-h"                        => "show this help message"
  29.                         );
  30.  
  31.     
  32.     /**
  33.     * Handle the command line values
  34.     * 
  35.     * handleArgv() looks for command line values and 
  36.     * interprets them. If there're unknown command it prints
  37.     * a message and calls die()
  38.     */    
  39.     function handleArgv() {
  40.         global $argv, $argc;
  41.         
  42.         // the first argv is the name of the script,
  43.         // so there must be at least another one
  44.         if ($argc < 2) {
  45.             $error  = "\n\nCould not understand your request.\n\n";
  46.             $error .= $this->getArgvHelpMessage();
  47.             print $error;
  48.             die();
  49.         }
  50.         
  51.         $commands = 0;
  52.         $errors = array();
  53.         
  54.         reset($argv);
  55.         
  56.         // skip the fist, it's the name of the php script
  57.         next($argv);
  58.         
  59.         while (list($k, $arg) = each($argv)) {
  60.             // valid command?
  61.             if ("-" != substr($arg, 0, 1))
  62.                 continue;
  63.             
  64.             $cmd         = substr($arg, 1, 2);                
  65.             $value     = trim(substr($arg, 3));
  66.             
  67.             // all command line options except -h require values
  68.             if (""==$value && "h"!=$cmd) {
  69.                 $errors[] = array( 
  70.                                                         "msg"     => sprintf("-%s: no value found", trim($cmd)),
  71.                                                         "type"    => "argv"
  72.                                                     );
  73.                 // skip this command
  74.                 continue;
  75.             }
  76.             
  77.             switch ($cmd) {
  78.                 case "f ":
  79.                     $files = explode(",", substr($arg, 3));
  80.                     $this->setFiles($files);
  81.                     $commands++;
  82.                     break;
  83.                 
  84.                 case "d ":
  85.                     $this->setDirectory($value);
  86.                     $commands++;
  87.                     break;
  88.                     
  89.                 case "p ":
  90.                     $this->setPath($value);
  91.                     $commands++;
  92.                     break;
  93.                     
  94.                 case "t ":
  95.                     $this->setTarget($value);
  96.                     $commands++;
  97.                     break;
  98.                 
  99.                 case "h ":
  100.                     $commands++;
  101.                     break;
  102.                     
  103.                 default:
  104.                     $errors[]="unknown command: '$arg'";
  105.                     break;
  106.             }
  107.             
  108.         }
  109.         
  110.         // are there enough informations to start work?
  111.         $errors = $this->checkStatus($errors);
  112.         
  113.         // check for errors and die() if neccessary
  114.         if (count($errors)>0) {
  115.             $error = "\n\nCould not understand your request.\n\n";
  116.             reset($errors);
  117.             while (list($k, $data)=each($errors)) 
  118.                 $error.=$data["msg"]."\n";
  119.             
  120.             $error.= $this->getArgvHelpMessage();
  121.             print $error;
  122.             die();
  123.         }
  124.                 
  125.         // no errors, but no recognized commands? die() if neccessary
  126.         if (0==$commands) {
  127.             $error = "\n\nCould not understand your request.\n\n";
  128.             $error.= $this->getArgvHelpMessage();
  129.             print $error;
  130.             die();
  131.         }
  132.         
  133.         // YEAH everything is fine, we can start working!
  134.         $this->parse();        
  135.     } // end func handleArgv
  136.     
  137.     /**
  138.     * Returns the current help message of phpdoc
  139.     * 
  140.     * The message is not HTML formated, it could be shown 
  141.     * on the command line. 
  142.     *
  143.     * @access    private
  144.     * @return    string    $help_msg    Some instructions on available command line options
  145.     * @see        handleArgv(), $COMMANDS
  146.     */    
  147.     function getArgvHelpMessage() {
  148.     
  149.         $help_msg = "";
  150.         
  151.         // generate the message from the COMMAND array
  152.         reset($this->COMMANDS);
  153.         while (list($param, $explanation)=each($this->COMMANDS)) 
  154.             $help_msg.= sprintf("%-28s%s\n", $param, $explanation);
  155.         
  156.         $help_msg.="\nFurter information can be found in the documentation.\n";
  157.         return $help_msg;        
  158.     } // end func getArgvHelpMessage
  159.     
  160. } // end class PhpdocArgvHandler
  161. ?>