A little something I threw together today in support of a CLI script I wrote to parse some httpd logs. This part is just for dealing with the command line args. One shortcoming is that it requires that if you have both a -x and --xname parameter, they must be for the same attribute; but sticking to that convention keeps it pretty clean/simple. Right now I have the possible arg names hard-coded in the constructor. To generalize it, I'll probably change it to accept some sort of input array.

/**
 * Class CLIArgs
 * Handle command line args
 */
class CLIArgs
{
    private $args = array();

/**
 * CLIArgs constructor
 * @todo   generalize by having options be an input param
 */
public function __construct()
{
    $this->args = getopt('d:t:h', array('dir:', 'time:', 'help'));
}

/**
 * @param string $name long name of parameter (e.g. 'help'
 * @return null|mixed  null if arg not found in request
 */
public function getArg($name)
{
    if(isset($this->args[$name])) {
        return $this->args[$name];
    }
    $letter = substr($name, 0, 1);
    if(isset($this->args[$letter])) {
        return $this->args[$letter];
    }
    return null;
}
}

    With a second array you could get around that shortcoming: one to map cli args to index numbers and one to map index numbers to arg values. Assuming the constructor did its job right, you'd only need to see if [font=monospace]$this->args[$name][/font] isset and if it is, return [font=monospace]$this->values[$this->args[$name]][/font].

      Generalized things and (I think) made it work with options that can be only short or long form, and if both do not have to have the same first letter. Even stuck in on GitHub: https://github.com/nogdog/CLIArgs

      <?php
      /**
       * @author Charles Reace
       */
      
      /**
       * Class CLIArgs
       * Handle command line args
       */
      class CLIArgs
      {
          private $args = array();
          private $opts = array();
      
      /**
       * CLIArgs constructor.
       * @param array $opts ['name' => ['short' => 'v', 'long' => 'var', 'param' => 'foo', 'help' => 'help text']
       */
      public function __construct(Array $opts)
      {
          $this->opts = $opts;
          $this->getOpts();
      }
      
      /**
       * @param string $name long name of parameter (e.g. 'help'
       * @return null|mixed  null if arg not found in request
       */
      public function getArg($name)
      {
          foreach(array('short', 'long') as $type) {
              if (isset($this->args[$this->opts['name'][$type]])) {
                  return $this->args[$this->opts['name'][$type]];
              }
          }
          return null;
      }
      
      /**
       * Get all arg data
       * @return array
       */
      public function getArgs()
      {
          return $this->args;
      }
      
      /**
       * get some plain text for use in usage statement
       * @return string
       * @param string $indent
       */
      public function helpText($indent = '    ')
      {
          $text = '';
          foreach($this->opts as $name => $data) {
              if(!empty($data['short'])) {
                  $text .= $indent.'-'.$data['short'];
                  if(!empty($data['param'])) {
                      $text.= ' '.$data['param'];
                  }
                  $text .= PHP_EOL;
              }
              if(!empty($data['long'])) {
                  $text .= $indent.'--'.$data['long'];
                  if(!empty($data['param'])) {
                      $text .= '='.$data['param'];
                  }
                  $text .= PHP_EOL;
              }
              $text .= $indent.$indent.$data['help'].PHP_EOL;
          }
          return $text;
      }
      
      private function getOpts()
      {
          $optString = '';
          $optArray  = array();
          foreach($this->opts as $key => $data) {
      
              if(!empty($data['short'])) {
                  $optString .= $data['short'];
                  if(!empty($data['param'])) {
                      $optString .= ':';
                  }
              }
              if(!empty($data['long'])) {
                  $str = $data['long'];
                  if(!empty($data['param'])) {
                      $str .= ':';
                  }
                  $optArray[] = $str;
              }
          }
          $this->args = getopt($optString, $optArray);
          return !empty($this->opts);
      }
      }
      
        Write a Reply...