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: Default.php,v 1.6 2008/10/09 10:44:54 izi Exp $
  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.0.5
  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.      * An instance of Console_CommandLine.
  55.      *
  56.      * @var Console_CommandLine $parser The parser
  57.      */
  58.     public $parser = false;
  59.  
  60.     // }}}
  61.     // __construct() {{{
  62.  
  63.     /**
  64.      * Constructor.
  65.      *
  66.      * @param object $parser A Console_CommandLine instance
  67.      *
  68.      * @return void 
  69.      */
  70.     public function __construct($parser = false
  71.     {
  72.         $this->parser = $parser;
  73.     }
  74.  
  75.     // }}}
  76.     // usage() {{{
  77.  
  78.     /**
  79.      * Returns the full usage message.
  80.      *
  81.      * @return string The usage message
  82.      */
  83.     public function usage()
  84.     {
  85.         $ret '';
  86.         if (!empty($this->parser->description)) 
  87.             $ret .= $this->description("\n\n";
  88.         }
  89.         $ret .= $this->usageLine("\n";
  90.         if (count($this->parser->commands> 0{
  91.             $ret .= $this->commandUsageLine("\n";
  92.         }
  93.         if (count($this->parser->options> 0{
  94.             $ret .= "\n" $this->optionList("\n";
  95.         }
  96.         if (count($this->parser->args> 0{
  97.             $ret .= "\n" $this->argumentList("\n";
  98.         }
  99.         if (count($this->parser->commands> 0{
  100.             $ret .= "\n" $this->commandList("\n";
  101.         }
  102.         $ret .= "\n";
  103.         return $ret;
  104.     }
  105.     // }}}
  106.     // error() {{{
  107.  
  108.     /**
  109.      * Returns a formatted error message.
  110.      *
  111.      * @param string $error The error message to format
  112.      *
  113.      * @return string The error string
  114.      */
  115.     public function error($error)
  116.     {
  117.         $ret 'Error: ' $error "\n";
  118.         if ($this->parser->add_help_option{
  119.             $name $this->name();
  120.             $ret .= $this->wrap($this->parser->message_provider->get('PROG_HELP_LINE',
  121.                 array('progname' => $name))) "\n";
  122.             if (count($this->parser->commands> 0{
  123.                 $ret .= $this->wrap($this->parser->message_provider->get('COMMAND_HELP_LINE',
  124.                     array('progname' => $name))) "\n";
  125.             }
  126.         }
  127.         return $ret;
  128.     }
  129.  
  130.     // }}}
  131.     // version() {{{
  132.  
  133.     /**
  134.      * Returns the program version string.
  135.      *
  136.      * @return string The version string
  137.      */
  138.     public function version()
  139.     {
  140.         return $this->parser->message_provider->get('PROG_VERSION_LINE'array(
  141.             'progname' => $this->name(),
  142.             'version'  => $this->parser->version
  143.         )) "\n";
  144.     }
  145.  
  146.     // }}}
  147.     // name() {{{
  148.  
  149.     /**
  150.      * Returns the full name of the program or the sub command
  151.      *
  152.      * @return string The name of the program
  153.      */
  154.     protected function name()
  155.     {
  156.         $name   '';
  157.         $parent $this->parser->parent;
  158.         while ($parent{
  159.             $name .= $parent->name . ' ';
  160.             if (count($parent->options> 0{
  161.                 $name .= '[' 
  162.                     . strtolower($this->parser->message_provider->get('OPTION_WORD',
  163.                           array('plural' => 's'))) 
  164.                     . '] ';
  165.             }
  166.             $parent $parent->parent;
  167.         }
  168.         $name .= $this->parser->name;
  169.         return $this->wrap($name);
  170.     }
  171.  
  172.     // }}}
  173.     // description() {{{
  174.  
  175.     /**
  176.      * Returns the command line description message.
  177.      *
  178.      * @return string The description message
  179.      */
  180.     protected function description()
  181.     {
  182.         return $this->wrap($this->parser->description);
  183.     }
  184.  
  185.     // }}}
  186.     // usageLine() {{{
  187.  
  188.     /**
  189.      * Returns the command line usage message
  190.      *
  191.      * @return string the usage message
  192.      */
  193.     protected function usageLine()
  194.     {
  195.         $usage $this->parser->message_provider->get('USAGE_WORD'":\n";
  196.         $ret   $usage '  ' $this->name();
  197.         if (count($this->parser->options> 0{
  198.             $ret .= ' [' 
  199.                 . strtolower($this->parser->message_provider->get('OPTION_WORD'))
  200.                 . ']';
  201.         }
  202.         if (count($this->parser->args> 0{
  203.             foreach ($this->parser->args as $name=>$arg{
  204.                 $ret .= ' <' $arg->help_name . ($arg->multiple?'...':'''>';
  205.             }
  206.         }
  207.         return $this->columnWrap($ret2);
  208.     }
  209.  
  210.     // }}}
  211.     // commandUsageLine() {{{
  212.  
  213.     /**
  214.      * Returns the command line usage message for subcommands.
  215.      *
  216.      * @return string The usage line
  217.      */
  218.     protected function commandUsageLine()
  219.     {
  220.         if (count($this->parser->commands== 0{
  221.             return '';
  222.         }
  223.         $ret '  ' $this->name();
  224.         if (count($this->parser->options> 0{
  225.             $ret .= ' [' 
  226.                 . strtolower($this->parser->message_provider->get('OPTION_WORD'))
  227.                 . ']';
  228.         }
  229.         //XXX
  230.         $ret .= " <command> [options] [args]";
  231.         return $this->columnWrap($ret2);
  232.     }
  233.  
  234.     // }}}
  235.     // argumentList() {{{
  236.  
  237.     /**
  238.      * Render the arguments list that will be displayed to the user, you can
  239.      * override this method if you want to change the look of the list.
  240.      *
  241.      * @return string The formatted argument list
  242.      */
  243.     protected function argumentList()
  244.     {
  245.         $col  = 0;
  246.         $args = array();
  247.         foreach ($this->parser->args as $arg{
  248.             $argstr '  ' $arg->toString();
  249.             $args[= array($argstr$arg->description);
  250.             $ln     strlen($argstr);
  251.             if ($col $ln{
  252.                 $col $ln;
  253.             }
  254.         }
  255.         $ret $this->parser->message_provider->get('ARGUMENT_WORD'":";
  256.         foreach ($args as $arg{
  257.             $text str_pad($arg[0]$col'  ' $arg[1];
  258.             $ret .= "\n" $this->columnWrap($text$col+2);
  259.         }
  260.         return $ret;
  261.     }
  262.  
  263.     // }}}
  264.     // optionList() {{{
  265.  
  266.     /**
  267.      * Render the options 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 option list
  271.      */
  272.     protected function optionList()
  273.     {
  274.         $col     = 0;
  275.         $options = array();
  276.         foreach ($this->parser->options as $option{
  277.             $optstr    '  ' $option->toString();
  278.             $options[= array($optstr$option->description);
  279.             $ln        strlen($optstr);
  280.             if ($col $ln{
  281.                 $col $ln;
  282.             }
  283.         }
  284.         $ret $this->parser->message_provider->get('OPTION_WORD'":";
  285.         foreach ($options as $option{
  286.             $text str_pad($option[0]$col'  ' $option[1];
  287.             $ret .= "\n" $this->columnWrap($text$col+2);
  288.         }
  289.         return $ret;
  290.     }
  291.  
  292.     // }}}
  293.     // commandList() {{{
  294.  
  295.     /**
  296.      * Render the command 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 subcommand list
  300.      */
  301.     protected function commandList()
  302.     {
  303.  
  304.         $commands = array();
  305.         $col      = 0;
  306.         foreach ($this->parser->commands as $cmdname=>$command{
  307.             $cmdname    '  ' $cmdname;
  308.             $commands[= array($cmdname$command->description);
  309.             $ln         strlen($cmdname);
  310.             if ($col $ln{
  311.                 $col $ln;
  312.             }
  313.         }
  314.         $ret $this->parser->message_provider->get('COMMAND_WORD'":";
  315.         foreach ($commands as $command{
  316.             $text str_pad($command[0]$col'  ' $command[1];
  317.             $ret .= "\n" $this->columnWrap($text$col+2);
  318.         }
  319.         return $ret;
  320.     }
  321.  
  322.     // }}}
  323.     // wrap() {{{
  324.  
  325.     /**
  326.      * Wraps the text passed to the method.
  327.      *
  328.      * @param string $text The text to wrap
  329.      * @param int    $lw   The column width (defaults to line_width property)
  330.      *
  331.      * @return string The wrapped text
  332.      */
  333.     protected function wrap($text$lw=null)
  334.     {
  335.         if ($this->line_width > 0{
  336.             if ($lw === null{
  337.                 $lw $this->line_width;
  338.             }
  339.             return wordwrap($text$lw"\n"false);
  340.         }
  341.         return $text;
  342.     }
  343.  
  344.     // }}}
  345.     // columnWrap() {{{
  346.  
  347.     /**
  348.      * Wraps the text passed to the method at the specified width.
  349.      *
  350.      * @param string $text The text to wrap
  351.      * @param int    $cw   The wrap width
  352.      *
  353.      * @return string The wrapped text
  354.      */
  355.     protected function columnWrap($text$cw)
  356.     {
  357.         $tokens explode("\n"$this->wrap($text));
  358.         $ret    $tokens[0];
  359.         $chunks $this->wrap(trim(substr($textstrlen($ret)))
  360.             $this->line_width - $cw);
  361.         $tokens explode("\n"$chunks);
  362.         foreach ($tokens as $token{
  363.             if (!empty($token)) {
  364.                 $ret .= "\n" str_repeat(' '$cw$token;
  365.             }
  366.         }
  367.         return $ret;
  368.     }
  369.  
  370.     // }}}
  371. }

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