Console_Getargs
[ class tree: Console_Getargs ] [ index: Console_Getargs ] [ all elements ]

Source for file Getargs.php

Documentation is available at Getargs.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2004 The PHP Group                                     |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Bertrand Mansion <bmansion@mamasam.com>                      |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Getargs.php,v 1.2 2004/09/20 09:27:33 mansion Exp $
  20.  
  21. require_once 'PEAR.php';
  22.  
  23. /**#@+
  24.  * Error Constants
  25.  */
  26. /**
  27.  * Wrong configuration
  28.  *
  29.  * This error will be TRIGGERed when a configuration error is found,
  30.  * it will also issue a WARNING.
  31.  */
  32. define('CONSOLE_GETARGS_ERROR_CONFIG'-1);
  33.  
  34. /**
  35.  * User made an error
  36.  *
  37.  * This error will be RETURNed when a bad parameter
  38.  * is found in the command line, for example an unknown parameter
  39.  * or a parameter with an invalid number of options.
  40.  */
  41. define('CONSOLE_GETARGS_ERROR_USER'-2);
  42.  
  43. /**
  44.  * Help text wanted
  45.  *
  46.  * This error will be RETURNed when the user asked to
  47.  * see the help by using <kbd>-h</kbd> or <kbd>--help</kbd> in the command line, you can then print
  48.  * the help ascii art text by using the {@link Console_Getargs::getHelp()} method
  49.  */
  50. define('CONSOLE_GETARGS_HELP'-3);
  51. /**#@-*/
  52.  
  53. /**
  54.  * Command-line arguments parsing class
  55.  * 
  56.  * This implementation was freely inspired by a python module called
  57.  * getargs by Vinod Vijayarajan and a perl CPAN module called
  58.  * Getopt::Simple by Ron Savage
  59.  *
  60.  * This class implements a Command Line Parser that your cli applications
  61.  * can use to parse command line arguments found in $_SERVER['argv'].
  62.  * It gives more flexibility and error checking than Console_Getopt. It also
  63.  * performs some arguments validation and is capable to return a formatted
  64.  * help text to the user, based on the configuration it is given.
  65.  * 
  66.  * The class provides the following capabilities:
  67.  * - Each command line option can take an arbitrary number of arguments.
  68.  * - Makes the distinction between switches (options without arguments)
  69.  *   and options that require arguments.
  70.  * - Recognizes 'single-argument-options' and 'default-if-set' options.
  71.  * - Switches and options with arguments can be interleaved in the command
  72.  *   line.
  73.  * - You can specify the maximum and minimum number of arguments an option
  74.  *   can take. Use -1 if you don't want to specify an upper bound.
  75.  * - Specify the default arguments to an option
  76.  * - Short options can be more than one letter in length.
  77.  * - A given option may be invoked by multiple names (aliases).
  78.  * - Understands by default the --help, -h options
  79.  * - Can return a formatted help text
  80.  * - Arguments may be specified using the '=' syntax also.
  81.  * 
  82.  * @todo Implement the parsing of comma delimited arguments
  83.  * @author Bertrand Mansion <bmansion@mamasam.com>
  84.  * @copyright 2004
  85.  * @license http://www.php.net/license/3_0.txt PHP License 3.0
  86.  * @version @VER@
  87.  * @package  Console_Getargs
  88.  */
  89. {
  90.     /**
  91.      * Factory creates a new {@link Console_Getargs_Options} object
  92.      *
  93.      * This method will return a new {@link Console_Getargs_Options}
  94.      * built using the given configuration options. If the configuration
  95.      * or the command line options contain errors, the returned object will
  96.      * in fact be a PEAR_Error explaining the cause of the error.
  97.      *
  98.      * Factory expects an array as parameter.
  99.      * The format for this array is:
  100.      * <pre>
  101.      * array(
  102.      *  longname => array('short'   => Short option name,
  103.      *                    'max'     => Maximum arguments for option,
  104.      *                    'min'     => Minimum arguments for option,
  105.      *                    'default' => Default option argument,
  106.      *                    'desc'    => Option description)
  107.      * )
  108.      * </pre>
  109.      * 
  110.      * If an option can be invoked by more than one name, they have to be defined
  111.      * by using | as a separator. For example: name1|name2
  112.      * This works both in long and short names.
  113.      *
  114.      * max/min are the most/least number of arguments an option accepts.
  115.      *
  116.      * The 'defaults' field is optional and is used to specify default
  117.      * arguments to an option. These will be assigned to the option if
  118.      * it is *not* used in the command line.
  119.      * Default arguments can be:
  120.      * - a single value for options that require a single argument,
  121.      * - an array of values for options with more than one possible arguments.
  122.      * Default argument(s) are mandatory for 'default-if-set' options.
  123.      *
  124.      * If max is 0 (option is just a switch), min is ignored.
  125.      * If max is -1, then the option can have an unlimited number of arguments
  126.      * greater or equal to min.
  127.      * 
  128.      * If max == min == 1, the option is treated as a single argument option.
  129.      * 
  130.      * If max >= 1 and min == 0, the option is treated as a
  131.      * 'default-if-set' option. This implies that it will get the default argument
  132.      * only if the option is used in the command line without any value.
  133.      * (Note: defaults *must* be specified for 'default-if-set' options)
  134.      *
  135.      * If the option is not in the command line, the defaults are
  136.      * *not* applied. If an argument for the option is specified on the command
  137.      * line, then the given argument is assigned to the option.
  138.      * Thus:
  139.      * - a --debug in the command line would cause debug = 'default argument'
  140.      * - a --debug 2 in the command line would result in debug = 2
  141.      *  if not used in the command line, debug will not be defined.
  142.      * 
  143.      * Example 1.
  144.      * <code>
  145.      * require_once 'Console_Getargs.php';
  146.      *
  147.      * $args =& Console_Getargs::factory($config);
  148.      * 
  149.      * if (PEAR::isError($args)) {
  150.      *  if ($args->getCode() === CONSOLE_GETARGS_ERROR_USER) {
  151.      *    echo Console_Getargs::getHelp($config, null, $args->getMessage())."\n";
  152.      *  } else if ($args->getCode() === CONSOLE_GETARGS_HELP) {
  153.      *    echo Console_Getargs::getHelp($config)."\n";
  154.      *  }
  155.      *  exit;
  156.      * }
  157.      * 
  158.      * echo 'Verbose: '.$args->getValue('verbose')."\n";
  159.      * if ($args->isDefined('bs')) {
  160.      *  echo 'Block-size: '.(is_array($args->getValue('bs')) ? implode(', ', $args->getValue('bs'))."\n" : $args->getValue('bs')."\n");
  161.      * } else {
  162.      *  echo "Block-size: undefined\n";
  163.      * }
  164.      * echo 'Files: '.($args->isDefined('file') ? implode(', ', $args->getValue('file'))."\n" : "undefined\n");
  165.      * if ($args->isDefined('n')) {
  166.      *  echo 'Nodes: '.(is_array($args->getValue('n')) ? implode(', ', $args->getValue('n'))."\n" : $args->getValue('n')."\n");
  167.      * } else {
  168.      *  echo "Nodes: undefined\n";
  169.      * }
  170.      * echo 'Log: '.$args->getValue('log')."\n";
  171.      * echo 'Debug: '.($args->isDefined('d') ? "YES\n" : "NO\n");
  172.      * 
  173.      * </code>
  174.      * 
  175.      * @param array associative array with keys being the options long name
  176.      * @access public
  177.      * @return object|PEAR_Error a newly created Console_Getargs_Options object
  178.      *                             or a PEAR_Error object on error
  179.      */
  180.     function &factory($config = array())
  181.     {
  182.         $obj =new Console_Getargs_Options();
  183.  
  184.         $err $obj->init($config);
  185.         if ($err !== true{
  186.             return $err;
  187.         }
  188.  
  189.         $err $obj->buildMaps();
  190.         if ($err !== true{
  191.             return $err;
  192.         }
  193.  
  194.         $err $obj->parseArgs();
  195.         if ($err !== true{
  196.             return $err;
  197.         }
  198.  
  199.         $err $obj->setDefaults();
  200.         if ($err !== true{
  201.             return $err;
  202.         }
  203.  
  204.         return $obj;
  205.     }
  206.  
  207.     /**
  208.      * Returns an ascii art version of the help
  209.      *
  210.      * This method uses the given configuration and parameters
  211.      * to create and format an help text for the options you defined
  212.      * in your config parameter. You can supply a header and a footer
  213.      * as well as the maximum length of a line. If you supplied
  214.      * descriptions for your options, they will be used as well.
  215.      *
  216.      * By default, it returns something like this:
  217.      * <pre>
  218.      * Usage: myscript.php [options]
  219.      * 
  220.      * -f --files values(2)          Set the source and destination image files.
  221.      * -w --width=&lt;value&gt;            Set the new width of the image.
  222.      * -d --debug                    Switch to debug mode.
  223.      * --formats values(1-3)         Set the image destination format. (jpegbig,
  224.      *                               jpegsmall)
  225.      * -fi --filters values(1-...)   Set the filters to be applied to the image upon
  226.      *                               conversion. The filters will be used in the order
  227.      *                               they are set.
  228.      * -v --verbose (optional)value  Set the verbose level. (3)
  229.      * </pre>
  230.      *
  231.      * @access public
  232.      * @param  array  your args configuration
  233.      * @param  string the header for the help. If it is left null,
  234.      *                 a default header will be used, starting by Usage:
  235.      * @param  string the footer for the help. This could be used
  236.      *                 to supply a description of the error the user made
  237.      * @param  int    help lines max length
  238.      * @return string the formatted help text
  239.      */
  240.     function getHelp($config$helpHeader = null$helpFooter ''$maxlength = 78)
  241.     {
  242.         $help '';
  243.         if (!isset($helpHeader)) {
  244.             $helpHeader 'Usage: '.basename($_SERVER['SCRIPT_NAME'])." [options]\n\n";
  245.         }
  246.         $i = 0;
  247.         foreach ($config as $long => $def{
  248.             
  249.             $shortArr = array();
  250.             if (isset($def['short'])) {
  251.                 $shortArr explode('|'$def['short']);
  252.             }
  253.             $longArr explode('|'$long);
  254.             $col1[$i!empty($shortArr'-'.$shortArr[0].' ' '';
  255.             $col1[$i.= '--'.$longArr[0];
  256.             $max $def['max'];
  257.             $min = isset($def['min']$def['min'$max;
  258.  
  259.             if ($max === 1 && $min === 1{
  260.                 $col1[$i.= '=<value>';
  261.             else if ($max > 1{
  262.                 if ($min === $max{
  263.                     $col1[$i.= ' values('.$max.')';
  264.                 else if ($min === 0{
  265.                     $col1[$i.= ' values(optional)';
  266.                 else {
  267.                     $col1[$i.= ' values('.$min.'-'.$max.')';
  268.                 }
  269.             else if ($max === 1 && $min === 0{
  270.                 $col1[$i.= ' (optional)value';
  271.             else if ($max === -1{
  272.                 if ($min > 0{
  273.                     $col1[$i.= ' values('.$min.'-...)';
  274.                 else {
  275.                     $col1[$i.= ' (optional)values';
  276.                 }
  277.             }
  278.  
  279.             if (isset($def['desc'])) {
  280.                 $col2[$i$def['desc'];
  281.             else {
  282.                 $col2[$i'';
  283.             }
  284.             if (isset($def['default'])) {
  285.                 if (is_array($def['default'])) {
  286.                     $col2[$i.= ' ('.implode(', '$def['default']).')';
  287.                 else {
  288.                     $col2[$i.= ' ('.$def['default'].')';
  289.                 }
  290.             }
  291.             $i++;
  292.         }
  293.         $arglen = 0;
  294.         foreach ($col1 as $txt{
  295.             $length strlen($txt);
  296.             if ($length $arglen{
  297.                 $arglen $length;
  298.             }
  299.         }
  300.         $desclen $maxlength $arglen;
  301.         $padding str_repeat(' '$arglen);
  302.         foreach ($col1 as $k => $txt{
  303.             if (strlen($col2[$k]$desclen{
  304.                 $desc wordwrap($col2[$k]$desclen"\n  ".$padding);
  305.             else {
  306.                 $desc $col2[$k];
  307.             }
  308.             $help .= str_pad($txt$arglen).'  '.$desc."\n";
  309.         }
  310.         return $helpHeader.$help.$helpFooter;
  311.     }
  312. // end class Console_Getargs
  313.  
  314. /**
  315.  * This class implements a wrapper to the command line options and arguments.
  316.  *
  317.  * @author Bertrand Mansion <bmansion@mamasam.com>
  318.  * @package  Console_Getargs
  319.  */
  320. {
  321.  
  322.     /**
  323.      * Lookup to match short options name with long ones
  324.      * @var array 
  325.      * @access private
  326.      */
  327.     var $_shortLong = array();
  328.  
  329.     /**
  330.      * Lookup to match alias options name with long ones
  331.      * @var array 
  332.      * @access private
  333.      */
  334.     var $_aliasLong = array();
  335.  
  336.     /**
  337.      * Arguments set for the options
  338.      * @var array 
  339.      * @access private
  340.      */
  341.     var $_longLong = array();
  342.  
  343.     /**
  344.      * Configuration set at initialization time
  345.      * @var array 
  346.      * @access private
  347.      */
  348.     var $_config = array();
  349.  
  350.     /**
  351.      * A read/write copy of argv
  352.      * @var array 
  353.      * @access private
  354.      */
  355.     var $args = array();
  356.  
  357.     /**
  358.      * Initializes the Console_Getargs_Options object
  359.      * @param array configuration options
  360.      * @access private
  361.      * @throws CONSOLE_GETARGS_ERROR_CONFIG
  362.      * @return true|PEAR_Error
  363.      */
  364.     function init($config)
  365.     {
  366.         if (!isset($_SERVER['argv']|| !is_array($_SERVER['argv'])) {
  367.             return PEAR::raiseError("Could not read argv"CONSOLE_GETARGS_ERROR_CONFIG,
  368.                                     PEAR_ERROR_TRIGGERE_USER_WARNING'Console_Getargs_Options::init()');
  369.         }
  370.         $this->args $_SERVER['argv'];
  371.  
  372.         if (isset($this->args[0]{0}&& $this->args[0]{0!= '-'{
  373.             array_shift($this->args);
  374.         }
  375.         $this->_config $config;
  376.         return true;
  377.     }
  378.  
  379.     /**
  380.      * Makes the lookup arrays for alias and short name mapping with long names
  381.      * @access private
  382.      * @throws CONSOLE_GETARGS_ERROR_CONFIG
  383.      * @return true|PEAR_Error
  384.      */
  385.     function buildMaps()
  386.     {
  387.         foreach($this->_config as $long => $def{
  388.  
  389.             $longArr explode('|'$long);
  390.             $longname $longArr[0];
  391.  
  392.             if (count($longArr> 1{
  393.                 array_shift($longArr);
  394.                 foreach($longArr as $alias{
  395.                     if (isset($this->_aliasLong[$alias])) {
  396.                         return PEAR::raiseError('Duplicate alias for long option '.$aliasCONSOLE_GETARGS_ERROR_CONFIG,
  397.                                     PEAR_ERROR_TRIGGERE_USER_WARNING'Console_Getargs_Options::buildMaps()');
  398.  
  399.                     }
  400.                     $this->_aliasLong[$alias$longname;
  401.                 }
  402.                 $this->_config[$longname$def;
  403.                 unset($this->_config[$long]);
  404.             }
  405.  
  406.             if (!empty($def['short'])) {
  407.                 // Short names
  408.                 $shortArr explode('|'$def['short']);
  409.                 $short $shortArr[0];
  410.                 if (count($shortArr> 1{
  411.                     array_shift($shortArr);
  412.                     foreach ($shortArr as $alias{
  413.                         if (isset($this->_shortLong[$alias])) {
  414.                             return PEAR::raiseError('Duplicate alias for short option '.$aliasCONSOLE_GETARGS_ERROR_CONFIG,
  415.                                     PEAR_ERROR_TRIGGERE_USER_WARNING'Console_Getargs_Options::buildMaps()');
  416.                         }
  417.                         $this->_shortLong[$alias$longname;
  418.                     }
  419.                 }
  420.                 $this->_shortLong[$short$longname;
  421.             }
  422.         }
  423.         return true;
  424.     }
  425.  
  426.     /**
  427.      * Parses the given options/arguments one by one
  428.      * @access private
  429.      * @throws CONSOLE_GETARGS_HELP
  430.      * @throws CONSOLE_GETARGS_ERROR_USER
  431.      * @return true|PEAR_Error
  432.      */
  433.     function parseArgs()
  434.     {
  435.         for ($i = 0$count count($this->args)$i $count$i++{
  436.  
  437.             $arg $this->args[$i];
  438.  
  439.             if ($arg === '--'{
  440.                 // '--' alone breaks the loop
  441.                 break;
  442.             }
  443.             if ($arg === '--help' || $arg === '-h'{
  444.                 return PEAR::raiseError(nullCONSOLE_GETARGS_HELPPEAR_ERROR_RETURN);
  445.             }
  446.             if (strlen($arg> 1 && $arg{1== '-'{
  447.                 $err $this->parseArg(substr($arg2)true$i);
  448.             else if (strlen($arg> 1 && $arg{0== '-'{
  449.                 $err $this->parseArg(substr($arg1)false$i);
  450.             else {
  451.                 $err = PEAR::raiseError('Unknown argument '.$arg,
  452.                                      CONSOLE_GETARGS_ERROR_USERPEAR_ERROR_RETURN,
  453.                                      null'Console_Getargs_Options::parseArgs()');
  454.             }
  455.             if ($err !== true{
  456.                 return $err;
  457.             }
  458.         }
  459.         return true;
  460.     }
  461.  
  462.     /**
  463.      * Parses one option/argument
  464.      * @access private
  465.      * @throws CONSOLE_GETARGS_ERROR_USER
  466.      * @return true|PEAR_Error
  467.      */
  468.     function parseArg($arg$isLong&$pos)
  469.     {
  470.         $opt '';
  471.         for ($i = 0; $i strlen($arg)$i++{
  472.             $opt .= $arg{$i};
  473.             if ($isLong === false && isset($this->_shortLong[$opt])) {
  474.                 $cmp $opt;
  475.                 $long $this->_shortLong[$opt];
  476.             else if ($isLong === true && isset($this->_config[$opt])) {
  477.                 $long $cmp $opt;
  478.             }
  479.             if ($arg{$i=== '='{
  480.                 break;
  481.             }
  482.         }
  483.  
  484.         if (isset($long)) {
  485.             if (strlen($argstrlen($cmp)) {
  486.                 $arg substr($argstrlen($cmp));
  487.                 if ($arg{0=== '='{
  488.                     $arg substr($arg1);
  489.                 }
  490.             else {
  491.                 $arg '';
  492.             }
  493.             return $this->setValue($long$arg$pos);
  494.         }
  495.         return PEAR::raiseError('Unknown argument '.$opt,
  496.                                 CONSOLE_GETARGS_ERROR_USERPEAR_ERROR_RETURN,
  497.                                 null'Console_Getargs_Options::parseArg()');
  498.     }
  499.  
  500.     /**
  501.      * Set the option arguments
  502.      * @access private
  503.      * @throws CONSOLE_GETARGS_ERROR_CONFIG
  504.      * @throws CONSOLE_GETARGS_ERROR_USER
  505.      * @return true|PEAR_Error
  506.      */
  507.     function setValue($optname$value&$pos)
  508.     {
  509.         if (!isset($this->_config[$optname]['max'])) {
  510.             return PEAR::raiseError('No max parameter set for '.$optname,
  511.                                      CONSOLE_GETARGS_ERROR_CONFIGPEAR_ERROR_TRIGGER,
  512.                                      E_USER_WARNING'Console_Getargs_Options::setValue()');
  513.         }
  514.  
  515.         $max $this->_config[$optname]['max'];
  516.         $min = isset($this->_config[$optname]['min']$this->_config[$optname]['min']$max;
  517.  
  518.         if ($value !== ''{
  519.             // Argument is like -v5
  520.             if ($min == 1 && $max > 0{
  521.                 $this->updateValue($optname$value);
  522.                 return true;
  523.             }
  524.             if ($max === 0{
  525.                 return PEAR::raiseError('Argument '.$optname.' does not take any value',
  526.                                      CONSOLE_GETARGS_ERROR_USERPEAR_ERROR_RETURN,
  527.                                      null'Console_Getargs_Options::setValue()');
  528.             }
  529.             return PEAR::raiseError('Argument '.$optname.' expects more than one value',
  530.                                      CONSOLE_GETARGS_ERROR_USERPEAR_ERROR_RETURN,
  531.                                      null'Console_Getargs_Options::setValue()');
  532.         }
  533.  
  534.         if ($min === 1 && $max === 1{
  535.             // Argument requires 1 value
  536.             if (isset($this->args[$pos+1]&& $this->isValue($this->args[$pos+1])) {
  537.                 $this->updateValue($optname$this->args[$pos+1]);
  538.                 $pos++;
  539.                 return true;
  540.             }
  541.             return PEAR::raiseError('Argument '.$optname.' expects one value',
  542.                              CONSOLE_GETARGS_ERROR_USERPEAR_ERROR_RETURN,
  543.                              null'Console_Getargs_Options::setValue()');
  544.  
  545.         else if ($max === 0{
  546.             // Argument is a switch
  547.             if (isset($this->args[$pos+1]&& $this->isValue($this->args[$pos+1])) {
  548.                 return PEAR::raiseError('Argument '.$optname.' does not take any value',
  549.                                  CONSOLE_GETARGS_ERROR_USERPEAR_ERROR_RETURN,
  550.                                  null'Console_Getargs_Options::setValue()');
  551.             }
  552.             $this->updateValue($optnametrue);
  553.             return true;
  554.  
  555.         else if ($max >= 1 && $min === 0{
  556.             // Argument has a default-if-set value
  557.             if (!isset($this->_config[$optname]['default'])) {
  558.                 return PEAR::raiseError('No default value defined for '.$optname,
  559.                                  CONSOLE_GETARGS_ERROR_CONFIGPEAR_ERROR_TRIGGER,
  560.                                  E_USER_WARNING'Console_Getargs_Options::setValue()');
  561.             }
  562.             if (is_array($this->_config[$optname]['default'])) {
  563.                 return PEAR::raiseError('Default value for '.$optname.' must be scalar',
  564.                                  CONSOLE_GETARGS_ERROR_CONFIGPEAR_ERROR_TRIGGER,
  565.                                  E_USER_WARNING'Console_Getargs_Options::setValue()');
  566.             }
  567.             if (isset($this->args[$pos+1]&& $this->isValue($this->args[$pos+1])) {
  568.                 $this->updateValue($optname$this->args[$pos+1]);
  569.                 $pos++;
  570.                 return true;
  571.             }
  572.             $this->updateValue($optname$this->_config[$optname]['default']);
  573.             return true;
  574.         }
  575.  
  576.         // Argument takes one or more values
  577.         $added = 0;
  578.         for ($i $pos + 1; $i <= count($this->args)$i++{
  579.             if (isset($this->args[$i]&& $this->isValue($this->args[$i])) {
  580.                 $this->updateValue($optname$this->args[$i]);
  581.                 $added++;
  582.                 $pos++;
  583.                 continue;
  584.             }
  585.             if ($min $added{
  586.                 return PEAR::raiseError('Argument '.$optname.' expects at least '.$min.(($min > 1' values' ' value'),
  587.                          CONSOLE_GETARGS_ERROR_USERPEAR_ERROR_RETURN,
  588.                          null'Console_Getargs_Options::setValue()');
  589.             else if ($max !== -1 && $added $max{
  590.                 return PEAR::raiseError('Argument '.$optname.' expects maximum '.$max.' values',
  591.                          CONSOLE_GETARGS_ERROR_USERPEAR_ERROR_RETURN,
  592.                          null'Console_Getargs_Options::setValue()');
  593.             }
  594.             break;
  595.         }
  596.         return true;
  597.     }
  598.  
  599.     /**
  600.      * Checks whether the given parameter is an argument or an option
  601.      * @access private
  602.      * @return boolean 
  603.      */
  604.     function isValue($arg)
  605.     {
  606.         if (strlen($arg> 1 && $arg{1== '-' ||
  607.             strlen($arg> 1 && $arg{0== '-'{
  608.             return false;
  609.         }
  610.         return true;
  611.     }
  612.  
  613.     /**
  614.      * Adds the argument to the option
  615.      *
  616.      * If the argument for the option is already set,
  617.      * the option arguments will be changed to an array
  618.      * @access private
  619.      * @return void 
  620.      */
  621.     function updateValue($optname$value)
  622.     {
  623.         if (isset($this->_longLong[$optname])) {
  624.             if (is_array($this->_longLong[$optname])) {
  625.                 $this->_longLong[$optname][$value;
  626.             else {
  627.                 $prevValue $this->_longLong[$optname];
  628.                 $this->_longLong[$optname= array($prevValue);
  629.                 $this->_longLong[$optname][$value;
  630.             }
  631.         else {
  632.             $this->_longLong[$optname$value;
  633.         }
  634.     }
  635.  
  636.     /**
  637.      * Sets the option default arguments when necessary
  638.      * @access private
  639.      * @return true 
  640.      */
  641.     function setDefaults()
  642.     {
  643.         foreach ($this->_config as $longname => $def{
  644.             if (isset($def['default']&& $def['min'!== 0 && !isset($this->_longLong[$longname])) {
  645.                 $this->_longLong[$longname$def['default'];
  646.             }
  647.         }
  648.         return true;
  649.     }
  650.  
  651.     /**
  652.      * Checks whether the given option is defined
  653.      *
  654.      * An option will be defined if an argument was assigned to it using
  655.      * the command line options. You can use the short, the long or
  656.      * an alias name as parameter.
  657.      *
  658.      * @access public
  659.      * @param  string the name of the option to be checked
  660.      * @return boolean true if the option is defined
  661.      */
  662.     function isDefined($optname)
  663.     {
  664.         $longname $this->getLongName($optname);
  665.         if (isset($this->_longLong[$longname])) {
  666.             return true;
  667.         }
  668.         return false;
  669.     }
  670.  
  671.     /**
  672.      * Returns the long version of the given parameter
  673.      *
  674.      * If the given name is not found, it will return the name that
  675.      * was given, without further ensuring that the option
  676.      * actually exists
  677.      *
  678.      * @access private
  679.      * @param  string the name of the option
  680.      * @return string long version of the option name
  681.      */
  682.     function getLongName($optname)
  683.     {
  684.         if (isset($this->_shortLong[$optname])) {
  685.             $longname $this->_shortLong[$optname];
  686.         else if (isset($this->_aliasLong[$optname])) {
  687.             $longname $this->_aliasLong[$optname];
  688.         else {
  689.             $longname $optname;
  690.         }
  691.         return $longname;
  692.     }
  693.  
  694.     /**
  695.      * Returns the argument of the given option
  696.      *
  697.      * You can use the short, alias or long version of the option name.
  698.      * This method will try to find the argument(s) of the given option name.
  699.      * If it is not found it will return null. If the arg has more than
  700.      * one argument, an array of arguments will be returned.
  701.      *
  702.      * @access public
  703.      * @param  string the name of the option
  704.      * @return array|string|nullargument(s) associated with the option
  705.      */
  706.     function getValue($optname)
  707.     {
  708.         if ($this->isDefined($optname)) {
  709.             $longname $this->getLongName($optname);
  710.             return $this->_longLong[$longname];
  711.         }
  712.         return null;
  713.     }
  714. // end class Console_Getargs_Options
  715. ?>

Documentation generated on Mon, 11 Mar 2019 13:55:30 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.