Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Bench
  • CommandLineBuilder
  • Pagination
  • Registry
  • Strings
  • Token
  • UserAgent

Exceptions

  • ExceptionHandler
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: namespace Pry\Util;
 13: /**
 14:  * Classe CommandLineBuilder. 
 15:  * Permet de créer simplement des lignes de commandes
 16:  * 
 17:  * @category Pry
 18:  * @package Util
 19:  * @version 1.0.0
 20:  * @author Olivier ROGER <oroger.fr>
 21:  *
 22:  */
 23: 
 24: define('SPACE',' ');
 25: class CommandLineBuilder
 26: {
 27: 
 28:     /**
 29:      * Nom de la commande à utiliser
 30:      * @var string 
 31:      */
 32:     protected $command;
 33:     
 34:     /**
 35:      * Liste des paramètres
 36:      * @var array 
 37:      */
 38:     protected $params;
 39:     
 40:     /**
 41:      * Liste des options et de leur valeur
 42:      * @var array 
 43:      */
 44:     protected $options;
 45:     
 46:     /**
 47:      * Liste des options longue et de leur valeur
 48:      * @var array 
 49:      */
 50:     protected $longOptions;
 51:     
 52:     /**
 53:      * Caractère à placer devant les options.
 54:      * Par défaut : -
 55:      * @var string 
 56:      */
 57:     protected $optionChar = '-';
 58:     
 59:     /**
 60:      * Caractère à placer devant les options longues.
 61:      * Par défaut : --
 62:      * @var string 
 63:      */
 64:     protected $longOptionChar = '--';
 65:     
 66:     public function __construct()
 67:     {
 68:         $this->clear();
 69:     }
 70:     
 71:     /**
 72:      * Réinitialise la ligne de commande
 73:      */
 74:     public function clear()
 75:     {
 76:         $this->command      = '';
 77:         $this->params       = array();
 78:         $this->options      = array();
 79:         $this->longOptions  = array();
 80:     }
 81:     
 82:     /**
 83:      * Défini le préfixe des options
 84:      * @param string $char
 85:      */
 86:     public function setOptionChar($char)
 87:     {
 88:         $this->optionChar = $char;
 89:     }
 90:     
 91:     /**
 92:      * Défini le préfixe des options longues
 93:      * @param string $longChar
 94:      */
 95:     public function setLongOptionChar($longChar)
 96:     {
 97:         $this->longOptionChar = $longChar;
 98:     }
 99:     
100:     /**
101:      * Défini la commande à utiliser
102:      * @param string $cmd
103:      */
104:     public function setCommand($cmd)
105:     {
106:         $this->command = $cmd;
107:     }
108:     
109:     /**
110:      * Ajoute un nouveau paramètre à la commande
111:      * @param string $param
112:      * @throws InvalidArgumentException si le paramètre existe déjà
113:      */
114:     public function addParameter($param)
115:     {
116:         if( in_array($param, $this->params) )
117:             throw new \InvalidArgumentException('This argument already exist');
118:                 
119:         $this->params[] = $param; 
120:     }
121:     
122:     /**
123:      * Ajoute une option et sa valeur éventuelle
124:      * @param string $name nom de l'option
125:      * @param string $value (facultatif) Valeur de l'option
126:      */
127:     public function addOption($name,$value='')
128:     {
129:         if(!empty($name))
130:             $this->options[$name] = $value;
131:     }
132:     
133:     /**
134:      * Ajoute une option longue et sa valeur éventuelle
135:      * @param string $name Nom de l'option longue
136:      * @param string $value Nom de la valeur
137:      */
138:     public function addLongOption($name,$value='')
139:     {
140:         if(!empty($name))
141:             $this->longOptions[$name] = $value;
142:     }
143:     
144:     /**
145:      * Construit la ligne de commande et la retourne
146:      * @return string Ligne de commande
147:      */
148:     public function get()
149:     {
150:         $command = $this->command;
151:         
152:         foreach($this->params as $param)
153:             $command .= SPACE.$param;
154:         
155:         foreach ($this->options as $option => $value)
156:         {
157:             $command .= SPACE.$this->optionChar.$option;
158:             if(!empty($value))
159:                 $command .= SPACE.$value;
160:         }
161:         
162:         foreach($this->longOptions as $option => $value)
163:         {
164:             $command .= SPACE.$this->longOptionChar.$option;
165:             if(!empty($value))
166:                 $command .= '='.$value;
167:         }
168:         
169:         return $command;
170:     }
171:     
172:     public function __toString()
173:     {
174:         return $this->get();
175:     }
176: }
Pry API documentation generated by ApiGen 2.8.0