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.7 2008/12/22 15:08:12 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.6
  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   '';
  164.         $parent $this->parser->parent;
  165.         while ($parent{
  166.             $name .= $parent->name . ' ';
  167.             if (count($parent->options> 0{
  168.                 $name .= '[' 
  169.                     . strtolower($this->parser->message_provider->get('OPTION_WORD',
  170.                           array('plural' => 's'))) 
  171.                     . '] ';
  172.             }
  173.             $parent $parent->parent;
  174.         }
  175.         $name .= $this->parser->name;
  176.         return $this->wrap($name);
  177.     }
  178.  
  179.     // }}}
  180.     // description() {{{
  181.  
  182.     /**
  183.      * Returns the command line description message.
  184.      *
  185.      * @return string The description message
  186.      */
  187.     protected function description()
  188.     {
  189.         return $this->wrap($this->parser->description);
  190.     }
  191.  
  192.     // }}}
  193.     // usageLine() {{{
  194.  
  195.     /**
  196.      * Returns the command line usage message
  197.      *
  198.      * @return string the usage message
  199.      */
  200.     protected function usageLine()
  201.     {
  202.         $usage $this->parser->message_provider->get('USAGE_WORD'":\n";
  203.         $ret   $usage '  ' $this->name();
  204.         if (count($this->parser->options> 0{
  205.             $ret .= ' [' 
  206.                 . strtolower($this->parser->message_provider->get('OPTION_WORD'))
  207.                 . ']';
  208.         }
  209.         if (count($this->parser->args> 0{
  210.             foreach ($this->parser->args as $name=>$arg{
  211.                 $ret .= ' <' $arg->help_name . ($arg->multiple?'...':'''>';
  212.             }
  213.         }
  214.         return $this->columnWrap($ret2);
  215.     }
  216.  
  217.     // }}}
  218.     // commandUsageLine() {{{
  219.  
  220.     /**
  221.      * Returns the command line usage message for subcommands.
  222.      *
  223.      * @return string The usage line
  224.      */
  225.     protected function commandUsageLine()
  226.     {
  227.         if (count($this->parser->commands== 0{
  228.             return '';
  229.         }
  230.         $ret '  ' $this->name();
  231.         if (count($this->parser->options> 0{
  232.             $ret .= ' [' 
  233.                 . strtolower($this->parser->message_provider->get('OPTION_WORD'))
  234.                 . ']';
  235.         }
  236.         //XXX
  237.         $ret .= " <command> [options] [args]";
  238.         return $this->columnWrap($ret2);
  239.     }
  240.  
  241.     // }}}
  242.     // argumentList() {{{
  243.  
  244.     /**
  245.      * Render the arguments list that will be displayed to the user, you can
  246.      * override this method if you want to change the look of the list.
  247.      *
  248.      * @return string The formatted argument list
  249.      */
  250.     protected function argumentList()
  251.     {
  252.         $col  = 0;
  253.         $args = array();
  254.         foreach ($this->parser->args as $arg{
  255.             $argstr '  ' $arg->toString();
  256.             $args[= array($argstr$arg->description);
  257.             $ln     strlen($argstr);
  258.             if ($col $ln{
  259.                 $col $ln;
  260.             }
  261.         }
  262.         $ret $this->parser->message_provider->get('ARGUMENT_WORD'":";
  263.         foreach ($args as $arg{
  264.             $text str_pad($arg[0]$col'  ' $arg[1];
  265.             $ret .= "\n" $this->columnWrap($text$col+2);
  266.         }
  267.         return $ret;
  268.     }
  269.  
  270.     // }}}
  271.     // optionList() {{{
  272.  
  273.     /**
  274.      * Render the options list that will be displayed to the user, you can
  275.      * override this method if you want to change the look of the list.
  276.      *
  277.      * @return string The formatted option list
  278.      */
  279.     protected function optionList()
  280.     {
  281.         $col     = 0;
  282.         $options = array();
  283.         foreach ($this->parser->options as $option{
  284.             $delim    $this->options_on_different_lines ? "\n" ', ';
  285.             $optstr   $option->toString($delim);
  286.             $lines    explode("\n"$optstr);
  287.             $lines[0'  ' $lines[0];
  288.             if (count($lines> 1{
  289.                 $lines[1'  ' $lines[1];
  290.                 $ln       strlen($lines[1]);
  291.             else {
  292.                 $ln strlen($lines[0]);
  293.             }
  294.             $options[= array($lines$option->description);
  295.             if ($col $ln{
  296.                 $col $ln;
  297.             }
  298.         }
  299.         $ret $this->parser->message_provider->get('OPTION_WORD'":";
  300.         foreach ($options as $option{
  301.             if (count($option[0]> 1{
  302.                 $text str_pad($option[0][1]$col'  ' $option[1];
  303.                 $pre  $option[0][0"\n";
  304.             else {
  305.                 $text str_pad($option[0][0]$col'  ' $option[1];
  306.                 $pre  '';
  307.             }
  308.             $ret .= "\n" $pre $this->columnWrap($text$col+2);
  309.         }
  310.         return $ret;
  311.     }
  312.  
  313.     // }}}
  314.     // commandList() {{{
  315.  
  316.     /**
  317.      * Render the command list that will be displayed to the user, you can
  318.      * override this method if you want to change the look of the list.
  319.      *
  320.      * @return string The formatted subcommand list
  321.      */
  322.     protected function commandList()
  323.     {
  324.  
  325.         $commands = array();
  326.         $col      = 0;
  327.         foreach ($this->parser->commands as $cmdname=>$command{
  328.             $cmdname    '  ' $cmdname;
  329.             $commands[= array($cmdname$command->description);
  330.             $ln         strlen($cmdname);
  331.             if ($col $ln{
  332.                 $col $ln;
  333.             }
  334.         }
  335.         $ret $this->parser->message_provider->get('COMMAND_WORD'":";
  336.         foreach ($commands as $command{
  337.             $text str_pad($command[0]$col'  ' $command[1];
  338.             $ret .= "\n" $this->columnWrap($text$col+2);
  339.         }
  340.         return $ret;
  341.     }
  342.  
  343.     // }}}
  344.     // wrap() {{{
  345.  
  346.     /**
  347.      * Wraps the text passed to the method.
  348.      *
  349.      * @param string $text The text to wrap
  350.      * @param int    $lw   The column width (defaults to line_width property)
  351.      *
  352.      * @return string The wrapped text
  353.      */
  354.     protected function wrap($text$lw=null)
  355.     {
  356.         if ($this->line_width > 0{
  357.             if ($lw === null{
  358.                 $lw $this->line_width;
  359.             }
  360.             return wordwrap($text$lw"\n"false);
  361.         }
  362.         return $text;
  363.     }
  364.  
  365.     // }}}
  366.     // columnWrap() {{{
  367.  
  368.     /**
  369.      * Wraps the text passed to the method at the specified width.
  370.      *
  371.      * @param string $text The text to wrap
  372.      * @param int    $cw   The wrap width
  373.      *
  374.      * @return string The wrapped text
  375.      */
  376.     protected function columnWrap($text$cw)
  377.     {
  378.         $tokens explode("\n"$this->wrap($text));
  379.         $ret    $tokens[0];
  380.         $chunks $this->wrap(trim(substr($textstrlen($ret)))
  381.             $this->line_width - $cw);
  382.         $tokens explode("\n"$chunks);
  383.         foreach ($tokens as $token{
  384.             if (!empty($token)) {
  385.                 $ret .= "\n" str_repeat(' '$cw$token;
  386.             }
  387.         }
  388.         return $ret;
  389.     }
  390.  
  391.     // }}}
  392. }

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