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 282213 2009-06-16 08:15:06Z izi $
  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.1.3
  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.                 $ret .= ' <' $arg->help_name . ($arg->multiple?'...':'''>';
  211.             }
  212.         }
  213.         return $this->columnWrap($ret2);
  214.     }
  215.  
  216.     // }}}
  217.     // commandUsageLine() {{{
  218.  
  219.     /**
  220.      * Returns the command line usage message for subcommands.
  221.      *
  222.      * @return string The usage line
  223.      */
  224.     protected function commandUsageLine()
  225.     {
  226.         if (count($this->parser->commands== 0{
  227.             return '';
  228.         }
  229.         $ret '  ' $this->name();
  230.         if (count($this->parser->options> 0{
  231.             $ret .= ' [' 
  232.                 . strtolower($this->parser->message_provider->get('OPTION_WORD'))
  233.                 . ']';
  234.         }
  235.         $ret       .= " <command>";
  236.         $hasArgs    = false;
  237.         $hasOptions = false;
  238.         foreach ($this->parser->commands as $command{
  239.             if (!$hasArgs && count($command->args> 0{
  240.                 $hasArgs = true;
  241.             }
  242.             if (!$hasOptions && ($command->add_help_option || 
  243.                 $command->add_version_option || count($command->options> 0)) {
  244.                 $hasOptions = true;
  245.             }
  246.         }
  247.         if ($hasOptions{
  248.             $ret .= ' [options]';
  249.         }
  250.         if ($hasArgs{
  251.             $ret .= ' [args]';
  252.         }
  253.         return $this->columnWrap($ret2);
  254.     }
  255.  
  256.     // }}}
  257.     // argumentList() {{{
  258.  
  259.     /**
  260.      * Render the arguments list that will be displayed to the user, you can
  261.      * override this method if you want to change the look of the list.
  262.      *
  263.      * @return string The formatted argument list
  264.      */
  265.     protected function argumentList()
  266.     {
  267.         $col  = 0;
  268.         $args = array();
  269.         foreach ($this->parser->args as $arg{
  270.             $argstr '  ' $arg->toString();
  271.             $args[= array($argstr$arg->description);
  272.             $ln     strlen($argstr);
  273.             if ($col $ln{
  274.                 $col $ln;
  275.             }
  276.         }
  277.         $ret $this->parser->message_provider->get('ARGUMENT_WORD'":";
  278.         foreach ($args as $arg{
  279.             $text str_pad($arg[0]$col'  ' $arg[1];
  280.             $ret .= "\n" $this->columnWrap($text$col+2);
  281.         }
  282.         return $ret;
  283.     }
  284.  
  285.     // }}}
  286.     // optionList() {{{
  287.  
  288.     /**
  289.      * Render the options list that will be displayed to the user, you can
  290.      * override this method if you want to change the look of the list.
  291.      *
  292.      * @return string The formatted option list
  293.      */
  294.     protected function optionList()
  295.     {
  296.         $col     = 0;
  297.         $options = array();
  298.         foreach ($this->parser->options as $option{
  299.             $delim    $this->options_on_different_lines ? "\n" ', ';
  300.             $optstr   $option->toString($delim);
  301.             $lines    explode("\n"$optstr);
  302.             $lines[0'  ' $lines[0];
  303.             if (count($lines> 1{
  304.                 $lines[1'  ' $lines[1];
  305.                 $ln       strlen($lines[1]);
  306.             else {
  307.                 $ln strlen($lines[0]);
  308.             }
  309.             $options[= array($lines$option->description);
  310.             if ($col $ln{
  311.                 $col $ln;
  312.             }
  313.         }
  314.         $ret $this->parser->message_provider->get('OPTION_WORD'":";
  315.         foreach ($options as $option{
  316.             if (count($option[0]> 1{
  317.                 $text str_pad($option[0][1]$col'  ' $option[1];
  318.                 $pre  $option[0][0"\n";
  319.             else {
  320.                 $text str_pad($option[0][0]$col'  ' $option[1];
  321.                 $pre  '';
  322.             }
  323.             $ret .= "\n" $pre $this->columnWrap($text$col+2);
  324.         }
  325.         return $ret;
  326.     }
  327.  
  328.     // }}}
  329.     // commandList() {{{
  330.  
  331.     /**
  332.      * Render the command list that will be displayed to the user, you can
  333.      * override this method if you want to change the look of the list.
  334.      *
  335.      * @return string The formatted subcommand list
  336.      */
  337.     protected function commandList()
  338.     {
  339.  
  340.         $commands = array();
  341.         $col      = 0;
  342.         foreach ($this->parser->commands as $cmdname=>$command{
  343.             $cmdname    '  ' $cmdname;
  344.             $commands[= array($cmdname$command->description$command->aliases);
  345.             $ln         strlen($cmdname);
  346.             if ($col $ln{
  347.                 $col $ln;
  348.             }
  349.         }
  350.         $ret $this->parser->message_provider->get('COMMAND_WORD'":";
  351.         foreach ($commands as $command{
  352.             $text str_pad($command[0]$col'  ' $command[1];
  353.             if ($aliasesCount count($command[2])) {
  354.                 $pad '';
  355.                 $text .= ' (';
  356.                 $text .= $aliasesCount > 1 ? 'aliases: ' 'alias: ';
  357.                 foreach ($command[2as $alias{
  358.                     $text .= $pad $alias;
  359.                     $pad   ', ';
  360.                 }
  361.                 $text .= ')';
  362.             }
  363.             $ret .= "\n" $this->columnWrap($text$col+2);
  364.         }
  365.         return $ret;
  366.     }
  367.  
  368.     // }}}
  369.     // wrap() {{{
  370.  
  371.     /**
  372.      * Wraps the text passed to the method.
  373.      *
  374.      * @param string $text The text to wrap
  375.      * @param int    $lw   The column width (defaults to line_width property)
  376.      *
  377.      * @return string The wrapped text
  378.      */
  379.     protected function wrap($text$lw=null)
  380.     {
  381.         if ($this->line_width > 0{
  382.             if ($lw === null{
  383.                 $lw $this->line_width;
  384.             }
  385.             return wordwrap($text$lw"\n"false);
  386.         }
  387.         return $text;
  388.     }
  389.  
  390.     // }}}
  391.     // columnWrap() {{{
  392.  
  393.     /**
  394.      * Wraps the text passed to the method at the specified width.
  395.      *
  396.      * @param string $text The text to wrap
  397.      * @param int    $cw   The wrap width
  398.      *
  399.      * @return string The wrapped text
  400.      */
  401.     protected function columnWrap($text$cw)
  402.     {
  403.         $tokens explode("\n"$this->wrap($text));
  404.         $ret    $tokens[0];
  405.         $chunks $this->wrap(trim(substr($textstrlen($ret)))
  406.             $this->line_width - $cw);
  407.         $tokens explode("\n"$chunks);
  408.         foreach ($tokens as $token{
  409.             if (!empty($token)) {
  410.                 $ret .= "\n" str_repeat(' '$cw$token;
  411.             }
  412.         }
  413.         return $ret;
  414.     }
  415.  
  416.     // }}}
  417. }

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