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

Source for file Default.php

Documentation is available at Default.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * This file is part of the PEAR Console_CommandLine package.
  7.  *
  8.  * PHP version 5
  9.  *
  10.  * LICENSE: This source file is subject to the MIT license that is available
  11.  * through the world-wide-web at the following URI:
  12.  * http://opensource.org/licenses/mit-license.php
  13.  *
  14.  * @category  Console
  15.  * @package   Console_CommandLine
  16.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  17.  * @copyright 2007 David JEAN LOUIS
  18.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  19.  * @version   CVS: $Id$
  20.  * @link      http://pear.php.net/package/Console_CommandLine
  21.  * @since     File available since release 0.1.0
  22.  * @filesource
  23.  */
  24.  
  25. /**
  26.  * The renderer interface.
  27.  */
  28. require_once 'Console/CommandLine/Renderer.php';
  29.  
  30. /**
  31.  * Console_CommandLine default renderer.
  32.  *
  33.  * @category  Console
  34.  * @package   Console_CommandLine
  35.  * @author    David JEAN LOUIS <izimobil@gmail.com>
  36.  * @copyright 2007 David JEAN LOUIS
  37.  * @license   http://opensource.org/licenses/mit-license.php MIT License
  38.  * @version   Release: 1.2.0
  39.  * @link      http://pear.php.net/package/Console_CommandLine
  40.  * @since     Class available since release 0.1.0
  41.  */
  42. class Console_CommandLine_Renderer_Default implements Console_CommandLine_Renderer
  43. {
  44.     // Properties {{{
  45.  
  46.     /**
  47.      * Integer that define the max width of the help text.
  48.      *
  49.      * @var integer $line_width Line width
  50.      */
  51.     public $line_width = 75;
  52.  
  53.     /**
  54.      * Integer that define the max width of the help text.
  55.      *
  56.      * @var integer $line_width Line width
  57.      */
  58.     public $options_on_different_lines = false;
  59.  
  60.     /**
  61.      * An instance of Console_CommandLine.
  62.      *
  63.      * @var Console_CommandLine $parser The parser
  64.      */
  65.     public $parser = false;
  66.  
  67.     // }}}
  68.     // __construct() {{{
  69.  
  70.     /**
  71.      * Constructor.
  72.      *
  73.      * @param object $parser A Console_CommandLine instance
  74.      *
  75.      * @return void 
  76.      */
  77.     public function __construct($parser = false
  78.     {
  79.         $this->parser = $parser;
  80.     }
  81.  
  82.     // }}}
  83.     // usage() {{{
  84.  
  85.     /**
  86.      * Returns the full usage message.
  87.      *
  88.      * @return string The usage message
  89.      */
  90.     public function usage()
  91.     {
  92.         $ret '';
  93.         if (!empty($this->parser->description)) 
  94.             $ret .= $this->description("\n\n";
  95.         }
  96.         $ret .= $this->usageLine("\n";
  97.         if (count($this->parser->commands> 0{
  98.             $ret .= $this->commandUsageLine("\n";
  99.         }
  100.         if (count($this->parser->options> 0{
  101.             $ret .= "\n" $this->optionList("\n";
  102.         }
  103.         if (count($this->parser->args> 0{
  104.             $ret .= "\n" $this->argumentList("\n";
  105.         }
  106.         if (count($this->parser->commands> 0{
  107.             $ret .= "\n" $this->commandList("\n";
  108.         }
  109.         $ret .= "\n";
  110.         return $ret;
  111.     }
  112.     // }}}
  113.     // error() {{{
  114.  
  115.     /**
  116.      * Returns a formatted error message.
  117.      *
  118.      * @param string $error The error message to format
  119.      *
  120.      * @return string The error string
  121.      */
  122.     public function error($error)
  123.     {
  124.         $ret 'Error: ' $error "\n";
  125.         if ($this->parser->add_help_option{
  126.             $name $this->name();
  127.             $ret .= $this->wrap($this->parser->message_provider->get('PROG_HELP_LINE',
  128.                 array('progname' => $name))) "\n";
  129.             if (count($this->parser->commands> 0{
  130.                 $ret .= $this->wrap($this->parser->message_provider->get('COMMAND_HELP_LINE',
  131.                     array('progname' => $name))) "\n";
  132.             }
  133.         }
  134.         return $ret;
  135.     }
  136.  
  137.     // }}}
  138.     // version() {{{
  139.  
  140.     /**
  141.      * Returns the program version string.
  142.      *
  143.      * @return string The version string
  144.      */
  145.     public function version()
  146.     {
  147.         return $this->parser->message_provider->get('PROG_VERSION_LINE'array(
  148.             'progname' => $this->name(),
  149.             'version'  => $this->parser->version
  150.         )) "\n";
  151.     }
  152.  
  153.     // }}}
  154.     // name() {{{
  155.  
  156.     /**
  157.      * Returns the full name of the program or the sub command
  158.      *
  159.      * @return string The name of the program
  160.      */
  161.     protected function name()
  162.     {
  163.         $name   $this->parser->name;
  164.         $parent $this->parser->parent;
  165.         while ($parent{
  166.             if (count($parent->options> 0{
  167.                 $name '[' 
  168.                     . strtolower($this->parser->message_provider->get('OPTION_WORD',
  169.                           array('plural' => 's'))) 
  170.                     . '] ' $name;
  171.             }
  172.             $name $parent->name . ' ' $name;
  173.             $parent $parent->parent;
  174.         }
  175.         return $this->wrap($name);
  176.     }
  177.  
  178.     // }}}
  179.     // description() {{{
  180.  
  181.     /**
  182.      * Returns the command line description message.
  183.      *
  184.      * @return string The description message
  185.      */
  186.     protected function description()
  187.     {
  188.         return $this->wrap($this->parser->description);
  189.     }
  190.  
  191.     // }}}
  192.     // usageLine() {{{
  193.  
  194.     /**
  195.      * Returns the command line usage message
  196.      *
  197.      * @return string the usage message
  198.      */
  199.     protected function usageLine()
  200.     {
  201.         $usage $this->parser->message_provider->get('USAGE_WORD'":\n";
  202.         $ret   $usage '  ' $this->name();
  203.         if (count($this->parser->options> 0{
  204.             $ret .= ' [' 
  205.                 . strtolower($this->parser->message_provider->get('OPTION_WORD'))
  206.                 . ']';
  207.         }
  208.         if (count($this->parser->args> 0{
  209.             foreach ($this->parser->args as $name=>$arg{
  210.                 $arg_str $arg->help_name;
  211.                 if ($arg->multiple{
  212.                     $arg_str .= '1 ' $arg->help_name . '2 ...';
  213.                 }
  214.                 if ($arg->optional{
  215.                     $arg_str '[' $arg_str ']';
  216.                 }
  217.                 $ret .= ' ' $arg_str;
  218.             }
  219.         }
  220.         return $this->columnWrap($ret2);
  221.     }
  222.  
  223.     // }}}
  224.     // commandUsageLine() {{{
  225.  
  226.     /**
  227.      * Returns the command line usage message for subcommands.
  228.      *
  229.      * @return string The usage line
  230.      */
  231.     protected function commandUsageLine()
  232.     {
  233.         if (count($this->parser->commands== 0{
  234.             return '';
  235.         }
  236.         $ret '  ' $this->name();
  237.         if (count($this->parser->options> 0{
  238.             $ret .= ' [' 
  239.                 . strtolower($this->parser->message_provider->get('OPTION_WORD'))
  240.                 . ']';
  241.         }
  242.         $ret       .= " <command>";
  243.         $hasArgs    = false;
  244.         $hasOptions = false;
  245.         foreach ($this->parser->commands as $command{
  246.             if (!$hasArgs && count($command->args> 0{
  247.                 $hasArgs = true;
  248.             }
  249.             if (!$hasOptions && ($command->add_help_option || 
  250.                 $command->add_version_option || count($command->options> 0)) {
  251.                 $hasOptions = true;
  252.             }
  253.         }
  254.         if ($hasOptions{
  255.             $ret .= ' [options]';
  256.         }
  257.         if ($hasArgs{
  258.             $ret .= ' [args]';
  259.         }
  260.         return $this->columnWrap($ret2);
  261.     }
  262.  
  263.     // }}}
  264.     // argumentList() {{{
  265.  
  266.     /**
  267.      * Render the arguments list that will be displayed to the user, you can
  268.      * override this method if you want to change the look of the list.
  269.      *
  270.      * @return string The formatted argument list
  271.      */
  272.     protected function argumentList()
  273.     {
  274.         $col  = 0;
  275.         $args = array();
  276.         foreach ($this->parser->args as $arg{
  277.             $argstr '  ' $arg->toString();
  278.             $args[= array($argstr$arg->description);
  279.             $ln     strlen($argstr);
  280.             if ($col $ln{
  281.                 $col $ln;
  282.             }
  283.         }
  284.         $ret $this->parser->message_provider->get('ARGUMENT_WORD'":";
  285.         foreach ($args as $arg{
  286.             $text str_pad($arg[0]$col'  ' $arg[1];
  287.             $ret .= "\n" $this->columnWrap($text$col+2);
  288.         }
  289.         return $ret;
  290.     }
  291.  
  292.     // }}}
  293.     // optionList() {{{
  294.  
  295.     /**
  296.      * Render the options list that will be displayed to the user, you can
  297.      * override this method if you want to change the look of the list.
  298.      *
  299.      * @return string The formatted option list
  300.      */
  301.     protected function optionList()
  302.     {
  303.         $col     = 0;
  304.         $options = array();
  305.         foreach ($this->parser->options as $option{
  306.             $delim    $this->options_on_different_lines ? "\n" ', ';
  307.             $optstr   $option->toString($delim);
  308.             $lines    explode("\n"$optstr);
  309.             $lines[0'  ' $lines[0];
  310.             if (count($lines> 1{
  311.                 $lines[1'  ' $lines[1];
  312.                 $ln       strlen($lines[1]);
  313.             else {
  314.                 $ln strlen($lines[0]);
  315.             }
  316.             $options[= array($lines$option->description);
  317.             if ($col $ln{
  318.                 $col $ln;
  319.             }
  320.         }
  321.         $ret $this->parser->message_provider->get('OPTION_WORD'":";
  322.         foreach ($options as $option{
  323.             if (count($option[0]> 1{
  324.                 $text str_pad($option[0][1]$col'  ' $option[1];
  325.                 $pre  $option[0][0"\n";
  326.             else {
  327.                 $text str_pad($option[0][0]$col'  ' $option[1];
  328.                 $pre  '';
  329.             }
  330.             $ret .= "\n" $pre $this->columnWrap($text$col+2);
  331.         }
  332.         return $ret;
  333.     }
  334.  
  335.     // }}}
  336.     // commandList() {{{
  337.  
  338.     /**
  339.      * Render the command list that will be displayed to the user, you can
  340.      * override this method if you want to change the look of the list.
  341.      *
  342.      * @return string The formatted subcommand list
  343.      */
  344.     protected function commandList()
  345.     {
  346.  
  347.         $commands = array();
  348.         $col      = 0;
  349.         foreach ($this->parser->commands as $cmdname=>$command{
  350.             $cmdname    '  ' $cmdname;
  351.             $commands[= array($cmdname$command->description$command->aliases);
  352.             $ln         strlen($cmdname);
  353.             if ($col $ln{
  354.                 $col $ln;
  355.             }
  356.         }
  357.         $ret $this->parser->message_provider->get('COMMAND_WORD'":";
  358.         foreach ($commands as $command{
  359.             $text str_pad($command[0]$col'  ' $command[1];
  360.             if ($aliasesCount count($command[2])) {
  361.                 $pad '';
  362.                 $text .= ' (';
  363.                 $text .= $aliasesCount > 1 ? 'aliases: ' 'alias: ';
  364.                 foreach ($command[2as $alias{
  365.                     $text .= $pad $alias;
  366.                     $pad   ', ';
  367.                 }
  368.                 $text .= ')';
  369.             }
  370.             $ret .= "\n" $this->columnWrap($text$col+2);
  371.         }
  372.         return $ret;
  373.     }
  374.  
  375.     // }}}
  376.     // wrap() {{{
  377.  
  378.     /**
  379.      * Wraps the text passed to the method.
  380.      *
  381.      * @param string $text The text to wrap
  382.      * @param int    $lw   The column width (defaults to line_width property)
  383.      *
  384.      * @return string The wrapped text
  385.      */
  386.     protected function wrap($text$lw=null)
  387.     {
  388.         if ($this->line_width > 0{
  389.             if ($lw === null{
  390.                 $lw $this->line_width;
  391.             }
  392.             return wordwrap($text$lw"\n"false);
  393.         }
  394.         return $text;
  395.     }
  396.  
  397.     // }}}
  398.     // columnWrap() {{{
  399.  
  400.     /**
  401.      * Wraps the text passed to the method at the specified width.
  402.      *
  403.      * @param string $text The text to wrap
  404.      * @param int    $cw   The wrap width
  405.      *
  406.      * @return string The wrapped text
  407.      */
  408.     protected function columnWrap($text$cw)
  409.     {
  410.         $tokens explode("\n"$this->wrap($text));
  411.         $ret    $tokens[0];
  412.         $chunks $this->wrap(trim(substr($textstrlen($ret)))
  413.             $this->line_width - $cw);
  414.         $tokens explode("\n"$chunks);
  415.         foreach ($tokens as $token{
  416.             if (!empty($token)) {
  417.                 $ret .= "\n" str_repeat(' '$cw$token;
  418.             }
  419.         }
  420.         return $ret;
  421.     }
  422.  
  423.     // }}}
  424. }

Documentation generated on Mon, 11 Mar 2019 15:51:31 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.