├── .gitignore ├── .travis.yml ├── Console ├── CommandLine.php └── CommandLine │ ├── Action.php │ ├── Action │ ├── Callback.php │ ├── Counter.php │ ├── Help.php │ ├── List.php │ ├── Password.php │ ├── StoreArray.php │ ├── StoreFalse.php │ ├── StoreFloat.php │ ├── StoreInt.php │ ├── StoreString.php │ ├── StoreTrue.php │ └── Version.php │ ├── Argument.php │ ├── Command.php │ ├── CustomMessageProvider.php │ ├── Element.php │ ├── Exception.php │ ├── MessageProvider.php │ ├── MessageProvider │ └── Default.php │ ├── Option.php │ ├── Outputter.php │ ├── Outputter │ └── Default.php │ ├── Renderer.php │ ├── Renderer │ └── Default.php │ ├── Result.php │ └── XmlParser.php ├── README.rst ├── composer.json ├── data └── xmlschema.rng ├── docs └── examples │ ├── ex1.php │ ├── ex2.php │ ├── ex2.xml │ ├── ex3.php │ ├── ex4.php │ ├── ex4.xml │ ├── ex5.php │ └── ex5.xml ├── phpunit.xml └── tests ├── AllTests.php ├── console_commandline_accept.phpt ├── console_commandline_addargument.phpt ├── console_commandline_addargument_2.phpt ├── console_commandline_addcommand.phpt ├── console_commandline_addcommand_2.phpt ├── console_commandline_addcommand_3.phpt ├── console_commandline_addoption.phpt ├── console_commandline_addoption_errors_1.phpt ├── console_commandline_addoption_errors_2.phpt ├── console_commandline_addoption_errors_3.phpt ├── console_commandline_addoption_errors_4.phpt ├── console_commandline_addoption_errors_5.phpt ├── console_commandline_addoption_errors_6.phpt ├── console_commandline_addoption_errors_7.phpt ├── console_commandline_bug18682.phpt ├── console_commandline_fromxmlfile.phpt ├── console_commandline_fromxmlfile_1.phpt ├── console_commandline_fromxmlfile_2.phpt ├── console_commandline_fromxmlfile_error.phpt ├── console_commandline_fromxmlstring.phpt ├── console_commandline_options_defaults.phpt ├── console_commandline_parse_1.phpt ├── console_commandline_parse_10.phpt ├── console_commandline_parse_11.phpt ├── console_commandline_parse_12.phpt ├── console_commandline_parse_13.phpt ├── console_commandline_parse_14.phpt ├── console_commandline_parse_15.phpt ├── console_commandline_parse_16.phpt ├── console_commandline_parse_17.phpt ├── console_commandline_parse_18.phpt ├── console_commandline_parse_19.phpt ├── console_commandline_parse_2.phpt ├── console_commandline_parse_20.phpt ├── console_commandline_parse_21.phpt ├── console_commandline_parse_22.phpt ├── console_commandline_parse_23.phpt ├── console_commandline_parse_24.phpt ├── console_commandline_parse_25.phpt ├── console_commandline_parse_26.phpt ├── console_commandline_parse_27.phpt ├── console_commandline_parse_28.phpt ├── console_commandline_parse_29.phpt ├── console_commandline_parse_3.phpt ├── console_commandline_parse_4.phpt ├── console_commandline_parse_5.phpt ├── console_commandline_parse_6.phpt ├── console_commandline_parse_7.phpt ├── console_commandline_parse_8.phpt ├── console_commandline_parse_9.phpt ├── console_commandline_webrequest_1.phpt ├── console_commandline_webrequest_2.phpt ├── console_commandline_webrequest_3.phpt ├── test.xml └── tests.inc.php /.gitignore: -------------------------------------------------------------------------------- 1 | /.phpunit.result.cache 2 | vendor 3 | composer.lock 4 | /README.html 5 | /dist 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | sudo: false 3 | install: 4 | - pear upgrade -f pear-1.10.5 5 | - pear install package.xml 6 | php: 7 | - 5.6 8 | - 7.0 9 | - 7.1 10 | - 7.2 11 | - 7.3 12 | - 7.4 13 | matrix: 14 | include: 15 | - php: 5.4 16 | dist: trusty 17 | - php: 5.5 18 | dist: trusty 19 | script: 20 | - pear run-tests -qd tests 21 | -------------------------------------------------------------------------------- /Console/CommandLine/Action.php: -------------------------------------------------------------------------------- 1 | 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 | * Class that represent an option action. 27 | * 28 | * @category Console 29 | * @package Console_CommandLine 30 | * @author David JEAN LOUIS 31 | * @copyright 2007 David JEAN LOUIS 32 | * @license http://opensource.org/licenses/mit-license.php MIT License 33 | * @version Release: @package_version@ 34 | * @link http://pear.php.net/package/Console_CommandLine 35 | * @since Class available since release 0.1.0 36 | */ 37 | abstract class Console_CommandLine_Action 38 | { 39 | // Properties {{{ 40 | 41 | /** 42 | * A reference to the result instance. 43 | * 44 | * @var Console_CommandLine_Result $result The result instance 45 | */ 46 | protected $result; 47 | 48 | /** 49 | * A reference to the option instance. 50 | * 51 | * @var Console_CommandLine_Option $option The action option 52 | */ 53 | protected $option; 54 | 55 | /** 56 | * A reference to the parser instance. 57 | * 58 | * @var Console_CommandLine $parser The parser 59 | */ 60 | protected $parser; 61 | 62 | // }}} 63 | // __construct() {{{ 64 | 65 | /** 66 | * Constructor 67 | * 68 | * @param Console_CommandLine_Result $result The result instance 69 | * @param Console_CommandLine_Option $option The action option 70 | * @param Console_CommandLine $parser The current parser 71 | * 72 | * @return void 73 | */ 74 | public function __construct($result, $option, $parser) 75 | { 76 | $this->result = $result; 77 | $this->option = $option; 78 | $this->parser = $parser; 79 | } 80 | 81 | // }}} 82 | // getResult() {{{ 83 | 84 | /** 85 | * Convenience method to retrieve the value of result->options[name]. 86 | * 87 | * @return mixed The result value or null 88 | */ 89 | public function getResult() 90 | { 91 | if (isset($this->result->options[$this->option->name])) { 92 | return $this->result->options[$this->option->name]; 93 | } 94 | return null; 95 | } 96 | 97 | // }}} 98 | // format() {{{ 99 | 100 | /** 101 | * Allow a value to be pre-formatted prior to being used in a choices test. 102 | * Setting $value to the new format will keep the formatting. 103 | * 104 | * @param mixed &$value The value to format 105 | * 106 | * @return mixed The formatted value 107 | */ 108 | public function format(&$value) 109 | { 110 | return $value; 111 | } 112 | 113 | // }}} 114 | // setResult() {{{ 115 | 116 | /** 117 | * Convenience method to assign the result->options[name] value. 118 | * 119 | * @param mixed $result The result value 120 | * 121 | * @return void 122 | */ 123 | public function setResult($result) 124 | { 125 | $this->result->options[$this->option->name] = $result; 126 | } 127 | 128 | // }}} 129 | // execute() {{{ 130 | 131 | /** 132 | * Executes the action with the value entered by the user. 133 | * All children actions must implement this method. 134 | * 135 | * @param mixed $value The option value 136 | * @param array $params An optional array of parameters 137 | * 138 | * @return string 139 | */ 140 | abstract public function execute($value = false, $params = array()); 141 | // }}} 142 | } 143 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/Callback.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the Callback action. 32 | * 33 | * The result option array entry value is set to the return value of the 34 | * callback defined in the option. 35 | * 36 | * There are two steps to defining a callback option: 37 | * - define the option itself using the callback action 38 | * - write the callback; this is a function (or method) that takes five 39 | * arguments, as described below. 40 | * 41 | * All callbacks are called as follows: 42 | * 43 | * callable_func( 44 | * $value, // the value of the option 45 | * $option_instance, // the option instance 46 | * $result_instance, // the result instance 47 | * $parser_instance, // the parser instance 48 | * $params // an array of params as specified in the option 49 | * ); 50 | * 51 | * and *must* return the option value. 52 | * 53 | * @category Console 54 | * @package Console_CommandLine 55 | * @author David JEAN LOUIS 56 | * @copyright 2007 David JEAN LOUIS 57 | * @license http://opensource.org/licenses/mit-license.php MIT License 58 | * @version Release: @package_version@ 59 | * @link http://pear.php.net/package/Console_CommandLine 60 | * @since Class available since release 0.1.0 61 | */ 62 | class Console_CommandLine_Action_Callback extends Console_CommandLine_Action 63 | { 64 | // execute() {{{ 65 | 66 | /** 67 | * Executes the action with the value entered by the user. 68 | * 69 | * @param mixed $value The value of the option 70 | * @param array $params An optional array of parameters 71 | * 72 | * @return string 73 | */ 74 | public function execute($value = false, $params = array()) 75 | { 76 | $this->setResult(call_user_func($this->option->callback, $value, 77 | $this->option, $this->result, $this->parser, $params)); 78 | } 79 | // }}} 80 | } 81 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/Counter.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the Version action. 32 | * 33 | * The execute methode add 1 to the value of the result option array entry. 34 | * The value is incremented each time the option is found, for example 35 | * with an option defined like that: 36 | * 37 | * 38 | * $parser->addOption( 39 | * 'verbose', 40 | * array( 41 | * 'short_name' => '-v', 42 | * 'action' => 'Counter' 43 | * ) 44 | * ); 45 | * 46 | * If the user type: 47 | * 48 | * $ script.php -v -v -v 49 | * 50 | * or: 51 | * 52 | * $ script.php -vvv 53 | * 54 | * the verbose variable will be set to to 3. 55 | * 56 | * @category Console 57 | * @package Console_CommandLine 58 | * @author David JEAN LOUIS 59 | * @copyright 2007 David JEAN LOUIS 60 | * @license http://opensource.org/licenses/mit-license.php MIT License 61 | * @version Release: @package_version@ 62 | * @link http://pear.php.net/package/Console_CommandLine 63 | * @since Class available since release 0.1.0 64 | */ 65 | class Console_CommandLine_Action_Counter extends Console_CommandLine_Action 66 | { 67 | // execute() {{{ 68 | 69 | /** 70 | * Executes the action with the value entered by the user. 71 | * 72 | * @param mixed $value The option value 73 | * @param array $params An optional array of parameters 74 | * 75 | * @return string 76 | */ 77 | public function execute($value = false, $params = array()) 78 | { 79 | $result = $this->getResult(); 80 | if ($result === null) { 81 | $result = 0; 82 | } 83 | $this->setResult(++$result); 84 | } 85 | // }}} 86 | } 87 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/Help.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the Help action, a special action that displays the 32 | * help message, telling the user how to use the program. 33 | * 34 | * @category Console 35 | * @package Console_CommandLine 36 | * @author David JEAN LOUIS 37 | * @copyright 2007 David JEAN LOUIS 38 | * @license http://opensource.org/licenses/mit-license.php MIT License 39 | * @version Release: @package_version@ 40 | * @link http://pear.php.net/package/Console_CommandLine 41 | * @since Class available since release 0.1.0 42 | */ 43 | class Console_CommandLine_Action_Help extends Console_CommandLine_Action 44 | { 45 | // execute() {{{ 46 | 47 | /** 48 | * Executes the action with the value entered by the user. 49 | * 50 | * @param mixed $value The option value 51 | * @param array $params An optional array of parameters 52 | * 53 | * @return string 54 | */ 55 | public function execute($value = false, $params = array()) 56 | { 57 | return $this->parser->displayUsage(); 58 | } 59 | // }}} 60 | } 61 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/List.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the List action, a special action that simply output an 32 | * array as a list. 33 | * 34 | * @category Console 35 | * @package Console_CommandLine 36 | * @author David JEAN LOUIS 37 | * @copyright 2007 David JEAN LOUIS 38 | * @license http://opensource.org/licenses/mit-license.php MIT License 39 | * @version Release: @package_version@ 40 | * @link http://pear.php.net/package/Console_CommandLine 41 | * @since Class available since release 0.1.0 42 | */ 43 | class Console_CommandLine_Action_List extends Console_CommandLine_Action 44 | { 45 | // execute() {{{ 46 | 47 | /** 48 | * Executes the action with the value entered by the user. 49 | * Possible parameters are: 50 | * - message: an alternative message to display instead of the default 51 | * message, 52 | * - delimiter: an alternative delimiter instead of the comma, 53 | * - post: a string to append after the message (default is the new line 54 | * char). 55 | * 56 | * @param mixed $value The option value 57 | * @param array $params An optional array of parameters 58 | * 59 | * @return string 60 | */ 61 | public function execute($value = false, $params = array()) 62 | { 63 | $list = isset($params['list']) ? $params['list'] : array(); 64 | $msg = isset($params['message']) 65 | ? $params['message'] 66 | : $this->parser->message_provider->get('LIST_DISPLAYED_MESSAGE'); 67 | $del = isset($params['delimiter']) ? $params['delimiter'] : ', '; 68 | $post = isset($params['post']) ? $params['post'] : "\n"; 69 | $this->parser->outputter->stdout($msg . implode($del, $list) . $post); 70 | exit(0); 71 | } 72 | // }}} 73 | } 74 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/Password.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the Password action, a special action that allow the 32 | * user to specify the password on the commandline or to be prompted for 33 | * entering it. 34 | * 35 | * @category Console 36 | * @package Console_CommandLine 37 | * @author David JEAN LOUIS 38 | * @copyright 2007 David JEAN LOUIS 39 | * @license http://opensource.org/licenses/mit-license.php MIT License 40 | * @version Release: @package_version@ 41 | * @link http://pear.php.net/package/Console_CommandLine 42 | * @since Class available since release 0.1.0 43 | */ 44 | class Console_CommandLine_Action_Password extends Console_CommandLine_Action 45 | { 46 | // execute() {{{ 47 | 48 | /** 49 | * Executes the action with the value entered by the user. 50 | * 51 | * @param mixed $value The option value 52 | * @param array $params An array of optional parameters 53 | * 54 | * @return string 55 | */ 56 | public function execute($value = false, $params = array()) 57 | { 58 | $this->setResult(empty($value) ? $this->_promptPassword() : $value); 59 | } 60 | // }}} 61 | // _promptPassword() {{{ 62 | 63 | /** 64 | * Prompts the password to the user without echoing it. 65 | * 66 | * @return string 67 | * @todo not echo-ing the password does not work on windows is there a way 68 | * to make this work ? 69 | */ 70 | private function _promptPassword() 71 | { 72 | if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { 73 | fwrite(STDOUT, 74 | $this->parser->message_provider->get('PASSWORD_PROMPT_ECHO')); 75 | @flock(STDIN, LOCK_EX); 76 | $passwd = fgets(STDIN); 77 | @flock(STDIN, LOCK_UN); 78 | } else { 79 | fwrite(STDOUT, $this->parser->message_provider->get('PASSWORD_PROMPT')); 80 | // disable echoing 81 | system('stty -echo'); 82 | @flock(STDIN, LOCK_EX); 83 | $passwd = fgets(STDIN); 84 | @flock(STDIN, LOCK_UN); 85 | system('stty echo'); 86 | } 87 | return trim($passwd); 88 | } 89 | // }}} 90 | } 91 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/StoreArray.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the StoreArray action. 32 | * 33 | * The execute method appends the value of the option entered by the user to 34 | * the result option array entry. 35 | * 36 | * @category Console 37 | * @package Console_CommandLine 38 | * @author David JEAN LOUIS 39 | * @copyright 2007 David JEAN LOUIS 40 | * @license http://opensource.org/licenses/mit-license.php MIT License 41 | * @version Release: @package_version@ 42 | * @link http://pear.php.net/package/Console_CommandLine 43 | * @since Class available since release 0.1.0 44 | */ 45 | class Console_CommandLine_Action_StoreArray extends Console_CommandLine_Action 46 | { 47 | // Protected properties {{{ 48 | 49 | /** 50 | * Force a clean result when first called, overriding any defaults assigned. 51 | * 52 | * @var object $firstPass First time this action has been called. 53 | */ 54 | protected $firstPass = true; 55 | 56 | // }}} 57 | // execute() {{{ 58 | 59 | /** 60 | * Executes the action with the value entered by the user. 61 | * 62 | * @param mixed $value The option value 63 | * @param array $params An optional array of parameters 64 | * 65 | * @return string 66 | */ 67 | public function execute($value = false, $params = array()) 68 | { 69 | $result = $this->getResult(); 70 | if (null === $result || $this->firstPass) { 71 | $result = array(); 72 | $this->firstPass = false; 73 | } 74 | $result[] = $value; 75 | $this->setResult($result); 76 | } 77 | // }}} 78 | } 79 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/StoreFalse.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the StoreFalse action. 32 | * 33 | * The execute method store the boolean 'false' in the corrsponding result 34 | * option array entry (the value is true if the option is not present in the 35 | * command line entered by the user). 36 | * 37 | * @category Console 38 | * @package Console_CommandLine 39 | * @author David JEAN LOUIS 40 | * @copyright 2007 David JEAN LOUIS 41 | * @license http://opensource.org/licenses/mit-license.php MIT License 42 | * @version Release: @package_version@ 43 | * @link http://pear.php.net/package/Console_CommandLine 44 | * @since Class available since release 0.1.0 45 | */ 46 | class Console_CommandLine_Action_StoreFalse extends Console_CommandLine_Action 47 | { 48 | // execute() {{{ 49 | 50 | /** 51 | * Executes the action with the value entered by the user. 52 | * 53 | * @param mixed $value The option value 54 | * @param array $params An array of optional parameters 55 | * 56 | * @return string 57 | */ 58 | public function execute($value = false, $params = array()) 59 | { 60 | $this->setResult(false); 61 | } 62 | 63 | // }}} 64 | } 65 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/StoreFloat.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the StoreFloat action. 32 | * 33 | * The execute method store the value of the option entered by the user as a 34 | * float in the result option array entry, if the value passed is not a float 35 | * an Exception is raised. 36 | * 37 | * @category Console 38 | * @package Console_CommandLine 39 | * @author David JEAN LOUIS 40 | * @copyright 2007 David JEAN LOUIS 41 | * @license http://opensource.org/licenses/mit-license.php MIT License 42 | * @version Release: @package_version@ 43 | * @link http://pear.php.net/package/Console_CommandLine 44 | * @since Class available since release 0.1.0 45 | */ 46 | class Console_CommandLine_Action_StoreFloat extends Console_CommandLine_Action 47 | { 48 | // execute() {{{ 49 | 50 | /** 51 | * Executes the action with the value entered by the user. 52 | * 53 | * @param mixed $value The option value 54 | * @param array $params An array of optional parameters 55 | * 56 | * @return string 57 | * @throws Console_CommandLine_Exception 58 | */ 59 | public function execute($value = false, $params = array()) 60 | { 61 | if (!is_numeric($value)) { 62 | include_once 'Console/CommandLine/Exception.php'; 63 | throw Console_CommandLine_Exception::factory( 64 | 'OPTION_VALUE_TYPE_ERROR', 65 | array( 66 | 'name' => $this->option->name, 67 | 'type' => 'float', 68 | 'value' => $value 69 | ), 70 | $this->parser 71 | ); 72 | } 73 | $this->setResult((float)$value); 74 | } 75 | // }}} 76 | } 77 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/StoreInt.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the StoreInt action. 32 | * 33 | * The execute method store the value of the option entered by the user as an 34 | * integer in the result option array entry, if the value passed is not an 35 | * integer an Exception is raised. 36 | * 37 | * @category Console 38 | * @package Console_CommandLine 39 | * @author David JEAN LOUIS 40 | * @copyright 2007 David JEAN LOUIS 41 | * @license http://opensource.org/licenses/mit-license.php MIT License 42 | * @version Release: @package_version@ 43 | * @link http://pear.php.net/package/Console_CommandLine 44 | * @since Class available since release 0.1.0 45 | */ 46 | class Console_CommandLine_Action_StoreInt extends Console_CommandLine_Action 47 | { 48 | // execute() {{{ 49 | 50 | /** 51 | * Executes the action with the value entered by the user. 52 | * 53 | * @param mixed $value The option value 54 | * @param array $params An array of optional parameters 55 | * 56 | * @return string 57 | * @throws Console_CommandLine_Exception 58 | */ 59 | public function execute($value = false, $params = array()) 60 | { 61 | if (!is_numeric($value)) { 62 | include_once 'Console/CommandLine/Exception.php'; 63 | throw Console_CommandLine_Exception::factory( 64 | 'OPTION_VALUE_TYPE_ERROR', 65 | array( 66 | 'name' => $this->option->name, 67 | 'type' => 'int', 68 | 'value' => $value 69 | ), 70 | $this->parser 71 | ); 72 | } 73 | $this->setResult((int)$value); 74 | } 75 | // }}} 76 | } 77 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/StoreString.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the StoreString action. 32 | * 33 | * The execute method store the value of the option entered by the user as a 34 | * string in the result option array entry. 35 | * 36 | * @category Console 37 | * @package Console_CommandLine 38 | * @author David JEAN LOUIS 39 | * @copyright 2007 David JEAN LOUIS 40 | * @license http://opensource.org/licenses/mit-license.php MIT License 41 | * @version Release: @package_version@ 42 | * @link http://pear.php.net/package/Console_CommandLine 43 | * @since Class available since release 0.1.0 44 | */ 45 | class Console_CommandLine_Action_StoreString extends Console_CommandLine_Action 46 | { 47 | // execute() {{{ 48 | 49 | /** 50 | * Executes the action with the value entered by the user. 51 | * 52 | * @param mixed $value The option value 53 | * @param array $params An array of optional parameters 54 | * 55 | * @return string 56 | */ 57 | public function execute($value = false, $params = array()) 58 | { 59 | $this->setResult((string)$value); 60 | } 61 | // }}} 62 | } 63 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/StoreTrue.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the StoreTrue action. 32 | * 33 | * The execute method store the boolean 'true' in the corrsponding result 34 | * option array entry (the value is false if the option is not present in the 35 | * command line entered by the user). 36 | * 37 | * @category Console 38 | * @package Console_CommandLine 39 | * @author David JEAN LOUIS 40 | * @copyright 2007 David JEAN LOUIS 41 | * @license http://opensource.org/licenses/mit-license.php MIT License 42 | * @version Release: @package_version@ 43 | * @link http://pear.php.net/package/Console_CommandLine 44 | * @since Class available since release 0.1.0 45 | */ 46 | class Console_CommandLine_Action_StoreTrue extends Console_CommandLine_Action 47 | { 48 | // execute() {{{ 49 | 50 | /** 51 | * Executes the action with the value entered by the user. 52 | * 53 | * @param mixed $value The option value 54 | * @param array $params An array of optional parameters 55 | * 56 | * @return string 57 | */ 58 | public function execute($value = false, $params = array()) 59 | { 60 | $this->setResult(true); 61 | } 62 | // }}} 63 | } 64 | -------------------------------------------------------------------------------- /Console/CommandLine/Action/Version.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine/Action.php'; 29 | 30 | /** 31 | * Class that represent the Version action, a special action that displays the 32 | * version string of the program. 33 | * 34 | * @category Console 35 | * @package Console_CommandLine 36 | * @author David JEAN LOUIS 37 | * @copyright 2007 David JEAN LOUIS 38 | * @license http://opensource.org/licenses/mit-license.php MIT License 39 | * @version Release: @package_version@ 40 | * @link http://pear.php.net/package/Console_CommandLine 41 | * @since Class available since release 0.1.0 42 | */ 43 | class Console_CommandLine_Action_Version extends Console_CommandLine_Action 44 | { 45 | // execute() {{{ 46 | 47 | /** 48 | * Executes the action with the value entered by the user. 49 | * 50 | * @param mixed $value The option value 51 | * @param array $params An array of optional parameters 52 | * 53 | * @return string 54 | */ 55 | public function execute($value = false, $params = array()) 56 | { 57 | return $this->parser->displayVersion(); 58 | } 59 | // }}} 60 | } 61 | -------------------------------------------------------------------------------- /Console/CommandLine/Argument.php: -------------------------------------------------------------------------------- 1 | 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 | * Include base element class. 27 | */ 28 | require_once 'Console/CommandLine/Element.php'; 29 | 30 | /** 31 | * Class that represent a command line argument. 32 | * 33 | * @category Console 34 | * @package Console_CommandLine 35 | * @author David JEAN LOUIS 36 | * @copyright 2007 David JEAN LOUIS 37 | * @license http://opensource.org/licenses/mit-license.php MIT License 38 | * @version Release: @package_version@ 39 | * @link http://pear.php.net/package/Console_CommandLine 40 | * @since Class available since release 0.1.0 41 | */ 42 | class Console_CommandLine_Argument extends Console_CommandLine_Element 43 | { 44 | // Public properties {{{ 45 | 46 | /** 47 | * Setting this to true will tell the parser that the argument expects more 48 | * than one argument and that argument values should be stored in an array. 49 | * 50 | * @var boolean $multiple Whether the argument expects multiple values 51 | */ 52 | public $multiple = false; 53 | 54 | /** 55 | * Setting this to true will tell the parser that the argument is optional 56 | * and can be ommited. 57 | * Note that it is not a good practice to make arguments optional, it is 58 | * the role of the options to be optional, by essence. 59 | * 60 | * @var boolean $optional Whether the argument is optional or not. 61 | */ 62 | public $optional = false; 63 | 64 | /** 65 | * An array of possible values for the argument. 66 | * 67 | * @var array $choices Valid choices for the argument 68 | */ 69 | public $choices = array(); 70 | 71 | // }}} 72 | // validate() {{{ 73 | 74 | /** 75 | * Validates the argument instance. 76 | * 77 | * @return void 78 | * @throws Console_CommandLine_Exception 79 | * @todo use exceptions 80 | */ 81 | public function validate() 82 | { 83 | // check if the argument name is valid 84 | if (!preg_match('/^[a-zA-Z_\x7f-\xff]+[a-zA-Z0-9_\x7f-\xff]*$/', 85 | $this->name)) { 86 | Console_CommandLine::triggerError( 87 | 'argument_bad_name', 88 | E_USER_ERROR, 89 | array('{$name}' => $this->name) 90 | ); 91 | } 92 | if (!$this->optional && $this->default !== null) { 93 | Console_CommandLine::triggerError( 94 | 'argument_no_default', 95 | E_USER_ERROR 96 | ); 97 | } 98 | parent::validate(); 99 | } 100 | 101 | // }}} 102 | } 103 | -------------------------------------------------------------------------------- /Console/CommandLine/Command.php: -------------------------------------------------------------------------------- 1 | 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 | * File containing the parent class. 27 | */ 28 | require_once 'Console/CommandLine.php'; 29 | 30 | /** 31 | * Class that represent a command with option and arguments. 32 | * 33 | * This class exist just to clarify the interface but at the moment it is 34 | * strictly identical to Console_CommandLine class, it could change in the 35 | * future though. 36 | * 37 | * @category Console 38 | * @package Console_CommandLine 39 | * @author David JEAN LOUIS 40 | * @copyright 2007 David JEAN LOUIS 41 | * @license http://opensource.org/licenses/mit-license.php MIT License 42 | * @version Release: @package_version@ 43 | * @link http://pear.php.net/package/Console_CommandLine 44 | * @since Class available since release 0.1.0 45 | */ 46 | class Console_CommandLine_Command extends Console_CommandLine 47 | { 48 | // Public properties {{{ 49 | 50 | /** 51 | * An array of aliases for the subcommand. 52 | * 53 | * @var array $aliases Aliases for the subcommand. 54 | */ 55 | public $aliases = array(); 56 | 57 | // }}} 58 | // __construct() {{{ 59 | 60 | /** 61 | * Constructor. 62 | * 63 | * @param array $params An optional array of parameters 64 | * 65 | * @return void 66 | */ 67 | public function __construct($params = array()) 68 | { 69 | if (isset($params['aliases'])) { 70 | $this->aliases = $params['aliases']; 71 | } 72 | parent::__construct($params); 73 | } 74 | 75 | // }}} 76 | } 77 | -------------------------------------------------------------------------------- /Console/CommandLine/CustomMessageProvider.php: -------------------------------------------------------------------------------- 1 | 17 | * @author Michael Gauthier 18 | * @copyright 2007 David JEAN LOUIS, 2009 silverorange 19 | * @license http://opensource.org/licenses/mit-license.php MIT License 20 | * @version CVS: $Id$ 21 | * @link http://pear.php.net/package/Console_CommandLine 22 | * @since File available since release 1.1.0 23 | * @filesource 24 | */ 25 | 26 | /** 27 | * Common interfacefor message providers that allow overriding with custom 28 | * messages 29 | * 30 | * Message providers may optionally implement this interface. 31 | * 32 | * @category Console 33 | * @package Console_CommandLine 34 | * @author David JEAN LOUIS 35 | * @author Michael Gauthier 36 | * @copyright 2007 David JEAN LOUIS, 2009 silverorange 37 | * @license http://opensource.org/licenses/mit-license.php MIT License 38 | * @version Release: @package_version@ 39 | * @link http://pear.php.net/package/Console_CommandLine 40 | * @since Interface available since release 1.1.0 41 | */ 42 | interface Console_CommandLine_CustomMessageProvider 43 | { 44 | // getWithCustomMesssages() {{{ 45 | 46 | /** 47 | * Retrieves the given string identifier corresponding message. 48 | * 49 | * For a list of identifiers please see the provided default message 50 | * provider. 51 | * 52 | * @param string $code The string identifier of the message 53 | * @param array $vars An array of template variables 54 | * @param array $messages An optional array of messages to use. Array 55 | * indexes are message codes. 56 | * 57 | * @return string 58 | * @see Console_CommandLine_MessageProvider 59 | * @see Console_CommandLine_MessageProvider_Default 60 | */ 61 | public function getWithCustomMessages( 62 | $code, $vars = array(), $messages = array() 63 | ); 64 | 65 | // }}} 66 | } 67 | -------------------------------------------------------------------------------- /Console/CommandLine/Element.php: -------------------------------------------------------------------------------- 1 | 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 | * Class that represent a command line element (an option, or an argument). 27 | * 28 | * @category Console 29 | * @package Console_CommandLine 30 | * @author David JEAN LOUIS 31 | * @copyright 2007 David JEAN LOUIS 32 | * @license http://opensource.org/licenses/mit-license.php MIT License 33 | * @version Release: @package_version@ 34 | * @link http://pear.php.net/package/Console_CommandLine 35 | * @since Class available since release 0.1.0 36 | */ 37 | abstract class Console_CommandLine_Element 38 | { 39 | // Public properties {{{ 40 | 41 | /** 42 | * The element name. 43 | * 44 | * @var string $name Element name 45 | */ 46 | public $name; 47 | 48 | /** 49 | * The name of variable displayed in the usage message, if no set it 50 | * defaults to the "name" property. 51 | * 52 | * @var string $help_name Element "help" variable name 53 | */ 54 | public $help_name; 55 | 56 | /** 57 | * The element description. 58 | * 59 | * @var string $description Element description 60 | */ 61 | public $description; 62 | 63 | /** 64 | * The default value of the element if not provided on the command line. 65 | * 66 | * @var mixed $default Default value of the option. 67 | */ 68 | public $default; 69 | 70 | /** 71 | * Custom errors messages for this element 72 | * 73 | * This array is of the form: 74 | * 75 | * $messageText, 78 | * $messageName => $messageText, 79 | * ... 80 | * ); 81 | * ?> 82 | * 83 | * 84 | * If specified, these messages override the messages provided by the 85 | * default message provider. For example: 86 | * 87 | * 'The argument foo is required.', 90 | * ); 91 | * ?> 92 | * 93 | * 94 | * @var array 95 | * @see Console_CommandLine_MessageProvider_Default 96 | */ 97 | public $messages = array(); 98 | 99 | // }}} 100 | // __construct() {{{ 101 | 102 | /** 103 | * Constructor. 104 | * 105 | * @param string $name The name of the element 106 | * @param array $params An optional array of parameters 107 | * 108 | * @return void 109 | */ 110 | public function __construct($name = null, $params = array()) 111 | { 112 | $this->name = $name; 113 | foreach ($params as $attr => $value) { 114 | if (property_exists($this, $attr)) { 115 | $this->$attr = $value; 116 | } 117 | } 118 | } 119 | 120 | // }}} 121 | // toString() {{{ 122 | 123 | /** 124 | * Returns the string representation of the element. 125 | * 126 | * @return string The string representation of the element 127 | * @todo use __toString() instead 128 | */ 129 | public function toString() 130 | { 131 | return $this->help_name; 132 | } 133 | // }}} 134 | // validate() {{{ 135 | 136 | /** 137 | * Validates the element instance and set it's default values. 138 | * 139 | * @return void 140 | * @throws Console_CommandLine_Exception 141 | */ 142 | public function validate() 143 | { 144 | // if no help_name passed, default to name 145 | if ($this->help_name == null) { 146 | $this->help_name = $this->name; 147 | } 148 | } 149 | 150 | // }}} 151 | } 152 | -------------------------------------------------------------------------------- /Console/CommandLine/Exception.php: -------------------------------------------------------------------------------- 1 | 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 | * Include the PEAR_Exception class 27 | */ 28 | require_once 'PEAR/Exception.php'; 29 | 30 | /** 31 | * Interface for custom message provider. 32 | */ 33 | require_once 'Console/CommandLine/CustomMessageProvider.php'; 34 | 35 | /** 36 | * Class for exceptions raised by the Console_CommandLine package. 37 | * 38 | * @category Console 39 | * @package Console_CommandLine 40 | * @author David JEAN LOUIS 41 | * @copyright 2007 David JEAN LOUIS 42 | * @license http://opensource.org/licenses/mit-license.php MIT License 43 | * @version Release: @package_version@ 44 | * @link http://pear.php.net/package/Console_CommandLine 45 | * @since Class available since release 0.1.0 46 | */ 47 | class Console_CommandLine_Exception extends PEAR_Exception 48 | { 49 | // Codes constants {{{ 50 | 51 | /**#@+ 52 | * Exception code constants. 53 | */ 54 | const OPTION_VALUE_REQUIRED = 1; 55 | const OPTION_VALUE_UNEXPECTED = 2; 56 | const OPTION_VALUE_TYPE_ERROR = 3; 57 | const OPTION_UNKNOWN = 4; 58 | const ARGUMENT_REQUIRED = 5; 59 | const INVALID_SUBCOMMAND = 6; 60 | /**#@-*/ 61 | 62 | // }}} 63 | // factory() {{{ 64 | 65 | /** 66 | * Convenience method that builds the exception with the array of params by 67 | * calling the message provider class. 68 | * 69 | * @param string $code The string identifier of the 70 | * exception. 71 | * @param array $params Array of template vars/values 72 | * @param Console_CommandLine $parser An instance of the parser 73 | * @param array $messages An optional array of messages 74 | * passed to the message provider. 75 | * 76 | * @return object an instance of Console_CommandLine_Exception 77 | */ 78 | public static function factory( 79 | $code, $params, $parser, array $messages = array() 80 | ) { 81 | $provider = $parser->message_provider; 82 | if ($provider instanceof Console_CommandLine_CustomMessageProvider) { 83 | $msg = $provider->getWithCustomMessages( 84 | $code, 85 | $params, 86 | $messages 87 | ); 88 | } else { 89 | $msg = $provider->get($code, $params); 90 | } 91 | $const = 'Console_CommandLine_Exception::' . $code; 92 | $code = defined($const) ? constant($const) : 0; 93 | return new Console_CommandLine_Exception($msg, $code); 94 | } 95 | 96 | // }}} 97 | } 98 | -------------------------------------------------------------------------------- /Console/CommandLine/MessageProvider.php: -------------------------------------------------------------------------------- 1 | 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 | * Message providers common interface, all message providers must implement 27 | * this interface. 28 | * 29 | * @category Console 30 | * @package Console_CommandLine 31 | * @author David JEAN LOUIS 32 | * @copyright 2007 David JEAN LOUIS 33 | * @license http://opensource.org/licenses/mit-license.php MIT License 34 | * @version Release: @package_version@ 35 | * @link http://pear.php.net/package/Console_CommandLine 36 | * @since Class available since release 0.1.0 37 | */ 38 | interface Console_CommandLine_MessageProvider 39 | { 40 | // get() {{{ 41 | 42 | /** 43 | * Retrieves the given string identifier corresponding message. 44 | * For a list of identifiers please see the provided default message 45 | * provider. 46 | * 47 | * @param string $code The string identifier of the message 48 | * @param array $vars An array of template variables 49 | * 50 | * @return string 51 | * @see Console_CommandLine_MessageProvider_Default 52 | */ 53 | public function get($code, $vars=array()); 54 | 55 | // }}} 56 | } 57 | -------------------------------------------------------------------------------- /Console/CommandLine/MessageProvider/Default.php: -------------------------------------------------------------------------------- 1 | 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 message provider interface. 27 | */ 28 | require_once 'Console/CommandLine/MessageProvider.php'; 29 | 30 | /** 31 | * The custom message provider interface. 32 | */ 33 | require_once 'Console/CommandLine/CustomMessageProvider.php'; 34 | 35 | /** 36 | * Lightweight class that manages messages used by Console_CommandLine package, 37 | * allowing the developper to customize these messages, for example to 38 | * internationalize a command line frontend. 39 | * 40 | * @category Console 41 | * @package Console_CommandLine 42 | * @author David JEAN LOUIS 43 | * @copyright 2007 David JEAN LOUIS 44 | * @license http://opensource.org/licenses/mit-license.php MIT License 45 | * @version Release: @package_version@ 46 | * @link http://pear.php.net/package/Console_CommandLine 47 | * @since Class available since release 0.1.0 48 | */ 49 | class Console_CommandLine_MessageProvider_Default 50 | implements Console_CommandLine_MessageProvider, 51 | Console_CommandLine_CustomMessageProvider 52 | { 53 | // Properties {{{ 54 | 55 | /** 56 | * Associative array of messages 57 | * 58 | * @var array $messages 59 | */ 60 | protected $messages = array( 61 | 'OPTION_VALUE_REQUIRED' => 'Option "{$name}" requires a value.', 62 | 'OPTION_VALUE_UNEXPECTED' => 'Option "{$name}" does not expect a value (got "{$value}").', 63 | 'OPTION_VALUE_NOT_VALID' => 'Option "{$name}" must be one of the following: "{$choices}" (got "{$value}").', 64 | 'ARGUMENT_VALUE_NOT_VALID'=> 'Argument "{$name}" must be one of the following: "{$choices}" (got "{$value}").', 65 | 'OPTION_VALUE_TYPE_ERROR' => 'Option "{$name}" requires a value of type {$type} (got "{$value}").', 66 | 'OPTION_AMBIGUOUS' => 'Ambiguous option "{$name}", can be one of the following: {$matches}.', 67 | 'OPTION_UNKNOWN' => 'Unknown option "{$name}".', 68 | 'ARGUMENT_REQUIRED' => 'You must provide at least {$argnum} argument{$plural}.', 69 | 'PROG_HELP_LINE' => 'Type "{$progname} --help" to get help.', 70 | 'PROG_VERSION_LINE' => '{$progname} version {$version}.', 71 | 'COMMAND_HELP_LINE' => 'Type "{$progname} --help" to get help on specific command.', 72 | 'USAGE_WORD' => 'Usage', 73 | 'OPTION_WORD' => 'Options', 74 | 'ARGUMENT_WORD' => 'Arguments', 75 | 'COMMAND_WORD' => 'Commands', 76 | 'PASSWORD_PROMPT' => 'Password: ', 77 | 'PASSWORD_PROMPT_ECHO' => 'Password (warning: will echo): ', 78 | 'INVALID_CUSTOM_INSTANCE' => 'Instance does not implement the required interface', 79 | 'LIST_OPTION_MESSAGE' => 'lists valid choices for option {$name}', 80 | 'LIST_DISPLAYED_MESSAGE' => 'Valid choices are: ', 81 | 'INVALID_SUBCOMMAND' => 'Command "{$command}" is not valid.', 82 | 'SUBCOMMAND_REQUIRED' => 'Please enter one of the following command: {$commands}.', 83 | ); 84 | 85 | // }}} 86 | // get() {{{ 87 | 88 | /** 89 | * Retrieve the given string identifier corresponding message. 90 | * 91 | * @param string $code The string identifier of the message 92 | * @param array $vars An array of template variables 93 | * 94 | * @return string 95 | */ 96 | public function get($code, $vars = array()) 97 | { 98 | if (!isset($this->messages[$code])) { 99 | return 'UNKNOWN'; 100 | } 101 | return $this->replaceTemplateVars($this->messages[$code], $vars); 102 | } 103 | 104 | // }}} 105 | // getWithCustomMessages() {{{ 106 | 107 | /** 108 | * Retrieve the given string identifier corresponding message. 109 | * 110 | * @param string $code The string identifier of the message 111 | * @param array $vars An array of template variables 112 | * @param array $messages An optional array of messages to use. Array 113 | * indexes are message codes. 114 | * 115 | * @return string 116 | */ 117 | public function getWithCustomMessages( 118 | $code, $vars = array(), $messages = array() 119 | ) { 120 | // get message 121 | if (isset($messages[$code])) { 122 | $message = $messages[$code]; 123 | } elseif (isset($this->messages[$code])) { 124 | $message = $this->messages[$code]; 125 | } else { 126 | $message = 'UNKNOWN'; 127 | } 128 | return $this->replaceTemplateVars($message, $vars); 129 | } 130 | 131 | // }}} 132 | // replaceTemplateVars() {{{ 133 | 134 | /** 135 | * Replaces template vars in a message 136 | * 137 | * @param string $message The message 138 | * @param array $vars An array of template variables 139 | * 140 | * @return string 141 | */ 142 | protected function replaceTemplateVars($message, $vars = array()) 143 | { 144 | $tmpkeys = array_keys($vars); 145 | $keys = array(); 146 | foreach ($tmpkeys as $key) { 147 | $keys[] = '{$' . $key . '}'; 148 | } 149 | return str_replace($keys, array_values($vars), $message); 150 | } 151 | 152 | // }}} 153 | } 154 | -------------------------------------------------------------------------------- /Console/CommandLine/Option.php: -------------------------------------------------------------------------------- 1 | 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 | * Required by this class. 27 | */ 28 | require_once 'Console/CommandLine.php'; 29 | require_once 'Console/CommandLine/Element.php'; 30 | 31 | /** 32 | * Class that represent a commandline option. 33 | * 34 | * @category Console 35 | * @package Console_CommandLine 36 | * @author David JEAN LOUIS 37 | * @copyright 2007 David JEAN LOUIS 38 | * @license http://opensource.org/licenses/mit-license.php MIT License 39 | * @version Release: @package_version@ 40 | * @link http://pear.php.net/package/Console_CommandLine 41 | * @since Class available since release 0.1.0 42 | */ 43 | class Console_CommandLine_Option extends Console_CommandLine_Element 44 | { 45 | // Public properties {{{ 46 | 47 | /** 48 | * The option short name (ex: -v). 49 | * 50 | * @var string $short_name Short name of the option 51 | */ 52 | public $short_name; 53 | 54 | /** 55 | * The option long name (ex: --verbose). 56 | * 57 | * @var string $long_name Long name of the option 58 | */ 59 | public $long_name; 60 | 61 | /** 62 | * The option action, defaults to "StoreString". 63 | * 64 | * @var string $action Option action 65 | */ 66 | public $action = 'StoreString'; 67 | 68 | /** 69 | * An array of possible values for the option. If this array is not empty 70 | * and the value passed is not in the array an exception is raised. 71 | * This only make sense for actions that accept values of course. 72 | * 73 | * @var array $choices Valid choices for the option 74 | */ 75 | public $choices = array(); 76 | 77 | /** 78 | * The callback function (or method) to call for an action of type 79 | * Callback, this can be any callable supported by the php function 80 | * call_user_func. 81 | * 82 | * Example: 83 | * 84 | * 85 | * $parser->addOption('myoption', array( 86 | * 'short_name' => '-m', 87 | * 'long_name' => '--myoption', 88 | * 'action' => 'Callback', 89 | * 'callback' => 'myCallbackFunction' 90 | * )); 91 | * 92 | * 93 | * @var callable $callback The option callback 94 | */ 95 | public $callback; 96 | 97 | /** 98 | * An associative array of additional params to pass to the class 99 | * corresponding to the action, this array will also be passed to the 100 | * callback defined for an action of type Callback, Example: 101 | * 102 | * 103 | * // for a custom action 104 | * $parser->addOption('myoption', array( 105 | * 'short_name' => '-m', 106 | * 'long_name' => '--myoption', 107 | * 'action' => 'MyCustomAction', 108 | * 'action_params' => array('foo'=>true, 'bar'=>false) 109 | * )); 110 | * 111 | * // if the user type: 112 | * // $ -m spam 113 | * // in your MyCustomAction class the execute() method will be called 114 | * // with the value 'spam' as first parameter and 115 | * // array('foo'=>true, 'bar'=>false) as second parameter 116 | * 117 | * 118 | * @var array $action_params Additional parameters to pass to the action 119 | */ 120 | public $action_params = array(); 121 | 122 | /** 123 | * For options that expect an argument, this property tells the parser if 124 | * the option argument is optional and can be ommited. 125 | * 126 | * @var bool $argumentOptional Whether the option arg is optional or not 127 | */ 128 | public $argument_optional = false; 129 | 130 | /** 131 | * For options that uses the "choice" property only. 132 | * Adds a --list- option to the parser that displays the list of 133 | * choices for the option. 134 | * 135 | * @var bool $add_list_option Whether to add a list option or not 136 | */ 137 | public $add_list_option = false; 138 | 139 | // }}} 140 | // Private properties {{{ 141 | 142 | /** 143 | * When an action is called remember it to allow for multiple calls. 144 | * 145 | * @var object $action_instance Placeholder for action 146 | */ 147 | private $_action_instance = null; 148 | 149 | // }}} 150 | // __construct() {{{ 151 | 152 | /** 153 | * Constructor. 154 | * 155 | * @param string $name The name of the option 156 | * @param array $params An optional array of parameters 157 | * 158 | * @return void 159 | */ 160 | public function __construct($name = null, $params = array()) 161 | { 162 | parent::__construct($name, $params); 163 | if ($this->action == 'Password') { 164 | // special case for Password action, password can be passed to the 165 | // commandline or prompted by the parser 166 | $this->argument_optional = true; 167 | } 168 | } 169 | 170 | // }}} 171 | // toString() {{{ 172 | 173 | /** 174 | * Returns the string representation of the option. 175 | * 176 | * @param string $delim Delimiter to use between short and long option 177 | * 178 | * @return string The string representation of the option 179 | * @todo use __toString() instead 180 | */ 181 | public function toString($delim = ", ") 182 | { 183 | $ret = ''; 184 | $padding = ''; 185 | if ($this->short_name != null) { 186 | $ret .= $this->short_name; 187 | if ($this->expectsArgument()) { 188 | $ret .= ' ' . $this->help_name; 189 | } 190 | $padding = $delim; 191 | } 192 | if ($this->long_name != null) { 193 | $ret .= $padding . $this->long_name; 194 | if ($this->expectsArgument()) { 195 | $ret .= '=' . $this->help_name; 196 | } 197 | } 198 | return $ret; 199 | } 200 | 201 | // }}} 202 | // expectsArgument() {{{ 203 | 204 | /** 205 | * Returns true if the option requires one or more argument and false 206 | * otherwise. 207 | * 208 | * @return bool Whether the option expects an argument or not 209 | */ 210 | public function expectsArgument() 211 | { 212 | if ($this->action == 'StoreTrue' || $this->action == 'StoreFalse' || 213 | $this->action == 'Help' || $this->action == 'Version' || 214 | $this->action == 'Counter' || $this->action == 'List') { 215 | return false; 216 | } 217 | return true; 218 | } 219 | 220 | // }}} 221 | // dispatchAction() {{{ 222 | 223 | /** 224 | * Formats the value $value according to the action of the option and 225 | * updates the passed Console_CommandLine_Result object. 226 | * 227 | * @param mixed $value The value to format 228 | * @param Console_CommandLine_Result $result The result instance 229 | * @param Console_CommandLine $parser The parser instance 230 | * 231 | * @return void 232 | * @throws Console_CommandLine_Exception 233 | */ 234 | public function dispatchAction($value, $result, $parser) 235 | { 236 | $actionInfo = Console_CommandLine::$actions[$this->action]; 237 | if (true === $actionInfo[1]) { 238 | // we have a "builtin" action 239 | $tokens = explode('_', $actionInfo[0]); 240 | include_once implode('/', $tokens) . '.php'; 241 | } 242 | $clsname = $actionInfo[0]; 243 | if ($this->_action_instance === null) { 244 | $this->_action_instance = new $clsname($result, $this, $parser); 245 | } 246 | 247 | // check value is in option choices 248 | if (!empty($this->choices) && !in_array($this->_action_instance->format($value), $this->choices)) { 249 | throw Console_CommandLine_Exception::factory( 250 | 'OPTION_VALUE_NOT_VALID', 251 | array( 252 | 'name' => $this->name, 253 | 'choices' => implode('", "', $this->choices), 254 | 'value' => $value, 255 | ), 256 | $parser, 257 | $this->messages 258 | ); 259 | } 260 | $this->_action_instance->execute($value, $this->action_params); 261 | } 262 | 263 | // }}} 264 | // validate() {{{ 265 | 266 | /** 267 | * Validates the option instance. 268 | * 269 | * @return void 270 | * @throws Console_CommandLine_Exception 271 | * @todo use exceptions instead 272 | */ 273 | public function validate() 274 | { 275 | // check if the option name is valid 276 | if (!preg_match('/^[a-zA-Z_\x7f-\xff]+[a-zA-Z0-9_\x7f-\xff]*$/', 277 | $this->name)) { 278 | Console_CommandLine::triggerError('option_bad_name', 279 | E_USER_ERROR, array('{$name}' => $this->name)); 280 | } 281 | // call the parent validate method 282 | parent::validate(); 283 | // a short_name or a long_name must be provided 284 | if ($this->short_name == null && $this->long_name == null) { 285 | Console_CommandLine::triggerError('option_long_and_short_name_missing', 286 | E_USER_ERROR, array('{$name}' => $this->name)); 287 | } 288 | // check if the option short_name is valid 289 | if ($this->short_name != null && 290 | !(preg_match('/^\-[a-zA-Z]{1}$/', $this->short_name))) { 291 | Console_CommandLine::triggerError('option_bad_short_name', 292 | E_USER_ERROR, array( 293 | '{$name}' => $this->name, 294 | '{$short_name}' => $this->short_name 295 | )); 296 | } 297 | // check if the option long_name is valid 298 | if ($this->long_name != null && 299 | !preg_match('/^\-\-[a-zA-Z]+[a-zA-Z0-9_\-]*$/', $this->long_name)) { 300 | Console_CommandLine::triggerError('option_bad_long_name', 301 | E_USER_ERROR, array( 302 | '{$name}' => $this->name, 303 | '{$long_name}' => $this->long_name 304 | )); 305 | } 306 | // check if we have a valid action 307 | if (!is_string($this->action)) { 308 | Console_CommandLine::triggerError('option_bad_action', 309 | E_USER_ERROR, array('{$name}' => $this->name)); 310 | } 311 | if (!isset(Console_CommandLine::$actions[$this->action])) { 312 | Console_CommandLine::triggerError('option_unregistered_action', 313 | E_USER_ERROR, array( 314 | '{$action}' => $this->action, 315 | '{$name}' => $this->name 316 | )); 317 | } 318 | // if the action is a callback, check that we have a valid callback 319 | if ($this->action == 'Callback' && !is_callable($this->callback)) { 320 | Console_CommandLine::triggerError('option_invalid_callback', 321 | E_USER_ERROR, array('{$name}' => $this->name)); 322 | } 323 | } 324 | 325 | // }}} 326 | // setDefaults() {{{ 327 | 328 | /** 329 | * Set the default value according to the configured action. 330 | * 331 | * Note that for backward compatibility issues this method is only called 332 | * when the 'force_options_defaults' is set to true, it will become the 333 | * default behaviour in the next major release of Console_CommandLine. 334 | * 335 | * @return void 336 | */ 337 | public function setDefaults() 338 | { 339 | if ($this->default !== null) { 340 | // already set 341 | return; 342 | } 343 | switch ($this->action) { 344 | case 'Counter': 345 | case 'StoreInt': 346 | $this->default = 0; 347 | break; 348 | case 'StoreFloat': 349 | $this->default = 0.0; 350 | break; 351 | case 'StoreArray': 352 | $this->default = array(); 353 | break; 354 | case 'StoreTrue': 355 | $this->default = false; 356 | break; 357 | case 'StoreFalse': 358 | $this->default = true; 359 | break; 360 | default: 361 | return; 362 | } 363 | } 364 | 365 | // }}} 366 | } 367 | -------------------------------------------------------------------------------- /Console/CommandLine/Outputter.php: -------------------------------------------------------------------------------- 1 | 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 | * Outputters common interface, all outputters must implement this interface. 27 | * 28 | * @category Console 29 | * @package Console_CommandLine 30 | * @author David JEAN LOUIS 31 | * @copyright 2007 David JEAN LOUIS 32 | * @license http://opensource.org/licenses/mit-license.php MIT License 33 | * @version Release: @package_version@ 34 | * @link http://pear.php.net/package/Console_CommandLine 35 | * @since Class available since release 0.1.0 36 | */ 37 | interface Console_CommandLine_Outputter 38 | { 39 | // stdout() {{{ 40 | 41 | /** 42 | * Processes the output for a message that should be displayed on STDOUT. 43 | * 44 | * @param string $msg The message to output 45 | * 46 | * @return void 47 | */ 48 | public function stdout($msg); 49 | 50 | // }}} 51 | // stderr() {{{ 52 | 53 | /** 54 | * Processes the output for a message that should be displayed on STDERR. 55 | * 56 | * @param string $msg The message to output 57 | * 58 | * @return void 59 | */ 60 | public function stderr($msg); 61 | 62 | // }}} 63 | } 64 | -------------------------------------------------------------------------------- /Console/CommandLine/Outputter/Default.php: -------------------------------------------------------------------------------- 1 | 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 Outputter interface. 27 | */ 28 | require_once 'Console/CommandLine/Outputter.php'; 29 | 30 | /** 31 | * Console_CommandLine default Outputter. 32 | * 33 | * @category Console 34 | * @package Console_CommandLine 35 | * @author David JEAN LOUIS 36 | * @copyright 2007 David JEAN LOUIS 37 | * @license http://opensource.org/licenses/mit-license.php MIT License 38 | * @version Release: @package_version@ 39 | * @link http://pear.php.net/package/Console_CommandLine 40 | * @since Class available since release 0.1.0 41 | */ 42 | class Console_CommandLine_Outputter_Default implements Console_CommandLine_Outputter 43 | { 44 | // stdout() {{{ 45 | 46 | /** 47 | * Writes the message $msg to STDOUT. 48 | * 49 | * @param string $msg The message to output 50 | * 51 | * @return void 52 | */ 53 | public function stdout($msg) 54 | { 55 | if (defined('STDOUT')) { 56 | fwrite(STDOUT, $msg); 57 | } else { 58 | echo $msg; 59 | } 60 | } 61 | 62 | // }}} 63 | // stderr() {{{ 64 | 65 | /** 66 | * Writes the message $msg to STDERR. 67 | * 68 | * @param string $msg The message to output 69 | * 70 | * @return void 71 | */ 72 | public function stderr($msg) 73 | { 74 | if (defined('STDERR')) { 75 | fwrite(STDERR, $msg); 76 | } else { 77 | echo $msg; 78 | } 79 | } 80 | 81 | // }}} 82 | } 83 | -------------------------------------------------------------------------------- /Console/CommandLine/Renderer.php: -------------------------------------------------------------------------------- 1 | 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 | * Renderers common interface, all renderers must implement this interface. 27 | * 28 | * @category Console 29 | * @package Console_CommandLine 30 | * @author David JEAN LOUIS 31 | * @copyright 2007 David JEAN LOUIS 32 | * @license http://opensource.org/licenses/mit-license.php MIT License 33 | * @version Release: @package_version@ 34 | * @link http://pear.php.net/package/Console_CommandLine 35 | * @since Class available since release 0.1.0 36 | */ 37 | interface Console_CommandLine_Renderer 38 | { 39 | // usage() {{{ 40 | 41 | /** 42 | * Returns the full usage message. 43 | * 44 | * @return string The usage message 45 | */ 46 | public function usage(); 47 | 48 | // }}} 49 | // error() {{{ 50 | 51 | /** 52 | * Returns a formatted error message. 53 | * 54 | * @param string $error The error message to format 55 | * 56 | * @return string The error string 57 | */ 58 | public function error($error); 59 | 60 | // }}} 61 | // version() {{{ 62 | 63 | /** 64 | * Returns the program version string. 65 | * 66 | * @return string The version string 67 | */ 68 | public function version(); 69 | 70 | // }}} 71 | } 72 | -------------------------------------------------------------------------------- /Console/CommandLine/Renderer/Default.php: -------------------------------------------------------------------------------- 1 | 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 36 | * @copyright 2007 David JEAN LOUIS 37 | * @license http://opensource.org/licenses/mit-license.php MIT License 38 | * @version Release: @package_version@ 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($ret, 2); 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 .= " "; 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($ret, 2); 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[2] as $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 | $text = trim(substr($text, strlen($ret))); 413 | if (empty($text)) { 414 | return $ret; 415 | } 416 | 417 | $chunks = $this->wrap($text, $this->line_width - $cw); 418 | $tokens = explode("\n", $chunks); 419 | foreach ($tokens as $token) { 420 | if (!empty($token)) { 421 | $ret .= "\n" . str_repeat(' ', $cw) . $token; 422 | } else { 423 | $ret .= "\n"; 424 | } 425 | } 426 | return $ret; 427 | } 428 | 429 | // }}} 430 | } 431 | -------------------------------------------------------------------------------- /Console/CommandLine/Result.php: -------------------------------------------------------------------------------- 1 | 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 | * A lightweight class to store the result of the command line parsing. 27 | * 28 | * @category Console 29 | * @package Console_CommandLine 30 | * @author David JEAN LOUIS 31 | * @copyright 2007 David JEAN LOUIS 32 | * @license http://opensource.org/licenses/mit-license.php MIT License 33 | * @version Release: @package_version@ 34 | * @link http://pear.php.net/package/Console_CommandLine 35 | * @since Class available since release 0.1.0 36 | */ 37 | class Console_CommandLine_Result 38 | { 39 | // Public properties {{{ 40 | 41 | /** 42 | * The result options associative array. 43 | * Key is the name of the option and value its value. 44 | * 45 | * @var array $options Result options array 46 | */ 47 | public $options = array(); 48 | 49 | /** 50 | * The result arguments array. 51 | * 52 | * @var array $args Result arguments array 53 | */ 54 | public $args = array(); 55 | 56 | /** 57 | * Name of the command invoked by the user, false if no command invoked. 58 | * 59 | * @var string $command_name Result command name 60 | */ 61 | public $command_name = false; 62 | 63 | /** 64 | * A result instance for the subcommand. 65 | * 66 | * @var Console_CommandLine_Result Result instance for the subcommand 67 | */ 68 | public $command = false; 69 | 70 | // }}} 71 | } 72 | -------------------------------------------------------------------------------- /Console/CommandLine/XmlParser.php: -------------------------------------------------------------------------------- 1 | 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 | * Required file 27 | */ 28 | require_once 'Console/CommandLine.php'; 29 | 30 | /** 31 | * Parser for command line xml definitions. 32 | * 33 | * @category Console 34 | * @package Console_CommandLine 35 | * @author David JEAN LOUIS 36 | * @copyright 2007 David JEAN LOUIS 37 | * @license http://opensource.org/licenses/mit-license.php MIT License 38 | * @version Release: @package_version@ 39 | * @link http://pear.php.net/package/Console_CommandLine 40 | * @since Class available since release 0.1.0 41 | */ 42 | class Console_CommandLine_XmlParser 43 | { 44 | // parse() {{{ 45 | 46 | /** 47 | * Parses the given xml definition file and returns a 48 | * Console_CommandLine instance constructed with the xml data. 49 | * 50 | * @param string $xmlfile The xml file to parse 51 | * 52 | * @return Console_CommandLine A parser instance 53 | */ 54 | public static function parse($xmlfile) 55 | { 56 | if (!is_readable($xmlfile)) { 57 | Console_CommandLine::triggerError('invalid_xml_file', 58 | E_USER_ERROR, array('{$file}' => $xmlfile)); 59 | } 60 | $doc = new DomDocument(); 61 | $doc->load($xmlfile); 62 | self::validate($doc); 63 | $nodes = $doc->getElementsByTagName('command'); 64 | $root = $nodes->item(0); 65 | return self::_parseCommandNode($root, true); 66 | } 67 | 68 | // }}} 69 | // parseString() {{{ 70 | 71 | /** 72 | * Parses the given xml definition string and returns a 73 | * Console_CommandLine instance constructed with the xml data. 74 | * 75 | * @param string $xmlstr The xml string to parse 76 | * 77 | * @return Console_CommandLine A parser instance 78 | */ 79 | public static function parseString($xmlstr) 80 | { 81 | $doc = new DomDocument(); 82 | $doc->loadXml($xmlstr); 83 | self::validate($doc); 84 | $nodes = $doc->getElementsByTagName('command'); 85 | $root = $nodes->item(0); 86 | return self::_parseCommandNode($root, true); 87 | } 88 | 89 | // }}} 90 | // validate() {{{ 91 | 92 | /** 93 | * Validates the xml definition using Relax NG. 94 | * 95 | * @param DomDocument $doc The document to validate 96 | * 97 | * @return boolean Whether the xml data is valid or not. 98 | * @throws Console_CommandLine_Exception 99 | * @todo use exceptions 100 | */ 101 | public static function validate($doc) 102 | { 103 | $pkgRoot = __DIR__ . '/../../'; 104 | $paths = array( 105 | // PEAR/Composer 106 | '@data_dir@/Console_CommandLine/data/xmlschema.rng', 107 | // Composer 108 | $pkgRoot . 'data/Console_CommandLine/data/xmlschema.rng', 109 | $pkgRoot . 'data/console_commandline/data/xmlschema.rng', 110 | // Git 111 | $pkgRoot . 'data/xmlschema.rng', 112 | 'xmlschema.rng', 113 | ); 114 | 115 | foreach ($paths as $path) { 116 | if (is_readable($path)) { 117 | return $doc->relaxNGValidate($path); 118 | } 119 | } 120 | Console_CommandLine::triggerError( 121 | 'invalid_xml_file', 122 | E_USER_ERROR, array('{$file}' => $rngfile)); 123 | } 124 | 125 | // }}} 126 | // _parseCommandNode() {{{ 127 | 128 | /** 129 | * Parses the root command node or a command node and returns the 130 | * constructed Console_CommandLine or Console_CommandLine_Command instance. 131 | * 132 | * @param DomDocumentNode $node The node to parse 133 | * @param bool $isRootNode Whether it is a root node or not 134 | * 135 | * @return mixed Console_CommandLine or Console_CommandLine_Command 136 | */ 137 | private static function _parseCommandNode($node, $isRootNode = false) 138 | { 139 | if ($isRootNode) { 140 | $obj = new Console_CommandLine(); 141 | } else { 142 | include_once 'Console/CommandLine/Command.php'; 143 | $obj = new Console_CommandLine_Command(); 144 | } 145 | foreach ($node->childNodes as $cNode) { 146 | $cNodeName = $cNode->nodeName; 147 | switch ($cNodeName) { 148 | case 'name': 149 | case 'description': 150 | case 'version': 151 | $obj->$cNodeName = trim($cNode->nodeValue); 152 | break; 153 | case 'add_help_option': 154 | case 'add_version_option': 155 | case 'force_posix': 156 | $obj->$cNodeName = self::_bool(trim($cNode->nodeValue)); 157 | break; 158 | case 'option': 159 | $obj->addOption(self::_parseOptionNode($cNode)); 160 | break; 161 | case 'argument': 162 | $obj->addArgument(self::_parseArgumentNode($cNode)); 163 | break; 164 | case 'command': 165 | $obj->addCommand(self::_parseCommandNode($cNode)); 166 | break; 167 | case 'aliases': 168 | if (!$isRootNode) { 169 | foreach ($cNode->childNodes as $subChildNode) { 170 | if ($subChildNode->nodeName == 'alias') { 171 | $obj->aliases[] = trim($subChildNode->nodeValue); 172 | } 173 | } 174 | } 175 | break; 176 | case 'messages': 177 | $obj->messages = self::_messages($cNode); 178 | break; 179 | default: 180 | break; 181 | } 182 | } 183 | return $obj; 184 | } 185 | 186 | // }}} 187 | // _parseOptionNode() {{{ 188 | 189 | /** 190 | * Parses an option node and returns the constructed 191 | * Console_CommandLine_Option instance. 192 | * 193 | * @param DomDocumentNode $node The node to parse 194 | * 195 | * @return Console_CommandLine_Option The built option 196 | */ 197 | private static function _parseOptionNode($node) 198 | { 199 | include_once 'Console/CommandLine/Option.php'; 200 | $obj = new Console_CommandLine_Option($node->getAttribute('name')); 201 | foreach ($node->childNodes as $cNode) { 202 | $cNodeName = $cNode->nodeName; 203 | switch ($cNodeName) { 204 | case 'choices': 205 | foreach ($cNode->childNodes as $subChildNode) { 206 | if ($subChildNode->nodeName == 'choice') { 207 | $obj->choices[] = trim($subChildNode->nodeValue); 208 | } 209 | } 210 | break; 211 | case 'messages': 212 | $obj->messages = self::_messages($cNode); 213 | break; 214 | default: 215 | if (property_exists($obj, $cNodeName)) { 216 | $obj->$cNodeName = trim($cNode->nodeValue); 217 | } 218 | break; 219 | } 220 | } 221 | if ($obj->action == 'Password') { 222 | $obj->argument_optional = true; 223 | } 224 | return $obj; 225 | } 226 | 227 | // }}} 228 | // _parseArgumentNode() {{{ 229 | 230 | /** 231 | * Parses an argument node and returns the constructed 232 | * Console_CommandLine_Argument instance. 233 | * 234 | * @param DomDocumentNode $node The node to parse 235 | * 236 | * @return Console_CommandLine_Argument The built argument 237 | */ 238 | private static function _parseArgumentNode($node) 239 | { 240 | include_once 'Console/CommandLine/Argument.php'; 241 | $obj = new Console_CommandLine_Argument($node->getAttribute('name')); 242 | foreach ($node->childNodes as $cNode) { 243 | $cNodeName = $cNode->nodeName; 244 | switch ($cNodeName) { 245 | case 'description': 246 | case 'help_name': 247 | case 'default': 248 | $obj->$cNodeName = trim($cNode->nodeValue); 249 | break; 250 | case 'multiple': 251 | $obj->multiple = self::_bool(trim($cNode->nodeValue)); 252 | break; 253 | case 'optional': 254 | $obj->optional = self::_bool(trim($cNode->nodeValue)); 255 | break; 256 | case 'choices': 257 | foreach ($cNode->childNodes as $subChildNode) { 258 | if ($subChildNode->nodeName == 'choice') { 259 | $obj->choices[] = trim($subChildNode->nodeValue); 260 | } 261 | } 262 | break; 263 | case 'messages': 264 | $obj->messages = self::_messages($cNode); 265 | break; 266 | default: 267 | break; 268 | } 269 | } 270 | return $obj; 271 | } 272 | 273 | // }}} 274 | // _bool() {{{ 275 | 276 | /** 277 | * Returns a boolean according to true/false possible strings. 278 | * 279 | * @param string $str The string to process 280 | * 281 | * @return boolean 282 | */ 283 | private static function _bool($str) 284 | { 285 | return in_array(strtolower((string)$str), array('true', '1', 'on', 'yes')); 286 | } 287 | 288 | // }}} 289 | // _messages() {{{ 290 | 291 | /** 292 | * Returns an array of custom messages for the element 293 | * 294 | * @param DOMNode $node The messages node to process 295 | * 296 | * @return array an array of messages 297 | * 298 | * @see Console_CommandLine::$messages 299 | * @see Console_CommandLine_Element::$messages 300 | */ 301 | private static function _messages(DOMNode $node) 302 | { 303 | $messages = array(); 304 | 305 | foreach ($node->childNodes as $cNode) { 306 | if ($cNode->nodeType == XML_ELEMENT_NODE) { 307 | $name = $cNode->getAttribute('name'); 308 | $value = trim($cNode->nodeValue); 309 | 310 | $messages[$name] = $value; 311 | } 312 | } 313 | 314 | return $messages; 315 | } 316 | 317 | // }}} 318 | } 319 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ******************* 2 | Console_CommandLine 3 | ******************* 4 | A full featured command line options and arguments parser. 5 | 6 | ``Console_CommandLine`` is a full featured package for managing command-line 7 | options and arguments highly inspired from python ``optparse`` module, it allows 8 | the developer to easily build complex command line interfaces. 9 | 10 | 11 | ============= 12 | Main features 13 | ============= 14 | * handles sub commands (ie. ``$ myscript.php -q subcommand -f file``), 15 | * can be completely built from an XML definition file, 16 | * generate ``--help`` and ``--version`` options automatically, 17 | * can be completely customized, 18 | * builtin support for i18n, 19 | * and much more... 20 | 21 | 22 | ============ 23 | Installation 24 | ============ 25 | 26 | PEAR 27 | ==== 28 | :: 29 | 30 | $ pear install Console_CommandLine 31 | 32 | 33 | Composer 34 | ======== 35 | :: 36 | 37 | $ composer require pear/console_commandline 38 | 39 | 40 | ===== 41 | Links 42 | ===== 43 | Homepage 44 | http://pear.php.net/package/Console_CommandLine 45 | Bug tracker 46 | http://pear.php.net/bugs/search.php?cmd=display&package_name[]=Console_CommandLine 47 | Documentation 48 | http://pear.php.net/manual/en/package.console.console-commandline.php 49 | Unit test status 50 | https://travis-ci.org/pear/Console_CommandLine 51 | 52 | .. image:: https://travis-ci.org/pear/Console_CommandLine.svg?branch=stable 53 | :target: https://travis-ci.org/pear/Console_CommandLine 54 | Packagist 55 | https://packagist.org/packages/pear/console_commandline 56 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pear/console_commandline", 3 | "description": "A full featured command line options and arguments parser.", 4 | "type": "library", 5 | "keywords": [ 6 | "console" 7 | ], 8 | "homepage": "https://github.com/pear/Console_CommandLine", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Richard Quadling", 13 | "email": "rquadling@gmail.com" 14 | }, 15 | { 16 | "name": "David Jean Louis", 17 | "email": "izimobil@gmail.com" 18 | } 19 | ], 20 | "require": { 21 | "php": ">=5.3.0", 22 | "pear/pear_exception": "^1.0.0", 23 | "ext-dom": "*", 24 | "ext-xml": "*" 25 | }, 26 | "autoload": { 27 | "psr-0": { 28 | "Console": "./" 29 | }, 30 | "exclude-from-classmap": ["tests/"] 31 | }, 32 | "include-path": [ 33 | "" 34 | ], 35 | "support": { 36 | "issues": "http://pear.php.net/bugs/search.php?cmd=display&package_name[]=Console_CommandLine", 37 | "source": "https://github.com/pear/Console_CommandLine" 38 | }, 39 | "require-dev": { 40 | "phpunit/phpunit": "*" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /data/xmlschema.rng: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | 11 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | [Tt][Rr][Uu][Ee] 213 | 214 | 215 | [On][Nn] 216 | 217 | 218 | [Yy][Ee][Ss] 219 | 220 | 1 221 | 222 | [Ff][Aa][Ll][Ss][Ee] 223 | 224 | 225 | [Of][Ff][Ff] 226 | 227 | 228 | [Nn][Oo] 229 | 230 | 0 231 | 232 | 233 | 234 | 235 | -------------------------------------------------------------------------------- /docs/examples/ex1.php: -------------------------------------------------------------------------------- 1 | 21 | * @copyright 2007 David JEAN LOUIS 22 | * @license http://opensource.org/licenses/mit-license.php MIT License 23 | * @version CVS: $Id$ 24 | * @link http://pear.php.net/package/Console_CommandLine 25 | * @since File available since release 0.1.0 26 | */ 27 | 28 | // Include the Console_CommandLine package. 29 | require_once 'Console/CommandLine.php'; 30 | 31 | // create the parser 32 | $parser = new Console_CommandLine(array( 33 | 'description' => 'zip given files using the php zip module.', 34 | 'version' => '1.0.0' 35 | )); 36 | 37 | // add an option to make the program verbose 38 | $parser->addOption('verbose', array( 39 | 'short_name' => '-v', 40 | 'long_name' => '--verbose', 41 | 'action' => 'StoreTrue', 42 | 'description' => 'turn on verbose output' 43 | )); 44 | 45 | // add an option to delete original files after zipping 46 | $parser->addOption('delete', array( 47 | 'short_name' => '-d', 48 | 'long_name' => '--delete', 49 | 'action' => 'StoreString', 50 | 'description' => 'delete original files after zip operation', 51 | 'choices' => array('foo', 'bar'), 52 | 'add_list_option' => true 53 | )); 54 | 55 | // add the files argument, the user can specify one or several files 56 | $parser->addArgument('files', array( 57 | 'multiple' => true, 58 | 'description' => 'list of files to zip separated by spaces' 59 | )); 60 | 61 | // add the zip file name argument 62 | $parser->addArgument('zipfile', array('description' => 'zip file name')); 63 | 64 | // run the parser 65 | try { 66 | $result = $parser->parse(); 67 | // write your program here... 68 | print_r($result->options); 69 | print_r($result->args); 70 | } catch (Exception $exc) { 71 | $parser->displayError($exc->getMessage()); 72 | } 73 | 74 | ?> 75 | -------------------------------------------------------------------------------- /docs/examples/ex2.php: -------------------------------------------------------------------------------- 1 | 20 | * @copyright 2007 David JEAN LOUIS 21 | * @license http://opensource.org/licenses/mit-license.php MIT License 22 | * @version CVS: $Id$ 23 | * @link http://pear.php.net/package/Console_CommandLine 24 | * @since File available since release 0.1.0 25 | */ 26 | 27 | // Include the Console_CommandLine package. 28 | require_once 'Console/CommandLine.php'; 29 | 30 | // create the parser from xml file 31 | $xmlfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ex2.xml'; 32 | $parser = Console_CommandLine::fromXmlFile($xmlfile); 33 | 34 | 35 | // run the parser 36 | try { 37 | $result = $parser->parse(); 38 | // write your program here... 39 | print_r($result->options); 40 | print_r($result->args); 41 | } catch (Exception $exc) { 42 | $parser->displayError($exc->getMessage()); 43 | } 44 | 45 | ?> 46 | -------------------------------------------------------------------------------- /docs/examples/ex2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | zip given files using the php zip module. 4 | 1.0.0 5 | 15 | 21 | 27 | 28 | a list of files to zip together 29 | true 30 | 31 | 32 | path to the zip file to generate 33 | 34 | 35 | -------------------------------------------------------------------------------- /docs/examples/ex3.php: -------------------------------------------------------------------------------- 1 | 19 | * @copyright 2007 David JEAN LOUIS 20 | * @license http://opensource.org/licenses/mit-license.php MIT License 21 | * @version CVS: $Id$ 22 | * @link http://pear.php.net/package/Console_CommandLine 23 | * @since File available since release 0.1.0 24 | */ 25 | 26 | // Include the Console_CommandLine package. 27 | require_once 'Console/CommandLine.php'; 28 | 29 | // create the parser 30 | $parser = new Console_CommandLine(array( 31 | 'description' => 'A great program that can foo and bar !', 32 | 'version' => '1.0.0' 33 | )); 34 | 35 | // add a global option to make the program verbose 36 | $parser->addOption('verbose', array( 37 | 'short_name' => '-v', 38 | 'long_name' => '--verbose', 39 | 'action' => 'StoreTrue', 40 | 'description' => 'turn on verbose output' 41 | )); 42 | 43 | // add the foo subcommand 44 | $foo_cmd = $parser->addCommand('foo', array( 45 | 'description' => 'output the given string with a foo prefix' 46 | )); 47 | $foo_cmd->addOption('reverse', array( 48 | 'short_name' => '-r', 49 | 'long_name' => '--reverse', 50 | 'action' => 'StoreTrue', 51 | 'description' => 'reverse the given string before echoing it' 52 | )); 53 | $foo_cmd->addArgument('text', array( 54 | 'description' => 'the text to output' 55 | )); 56 | 57 | // add the bar subcommand with a "baz" alias 58 | $bar_cmd = $parser->addCommand('bar', array( 59 | 'description' => 'output the given string with a bar prefix', 60 | 'aliases' => array('baz'), 61 | )); 62 | $bar_cmd->addOption('reverse', array( 63 | 'short_name' => '-r', 64 | 'long_name' => '--reverse', 65 | 'action' => 'StoreTrue', 66 | 'description' => 'reverse the given string before echoing it' 67 | )); 68 | $bar_cmd->addArgument('text', array( 69 | 'description' => 'the text to output' 70 | )); 71 | 72 | // run the parser 73 | try { 74 | $result = $parser->parse(); 75 | if ($result->command_name) { 76 | $st = $result->command->options['reverse'] 77 | ? strrev($result->command->args['text']) 78 | : $result->command->args['text']; 79 | if ($result->command_name == 'foo') { 80 | echo "Foo says: $st\n"; 81 | } else if ($result->command_name == 'bar') { 82 | echo "Bar says: $st\n"; 83 | } 84 | } 85 | } catch (Exception $exc) { 86 | $parser->displayError($exc->getMessage()); 87 | } 88 | 89 | ?> 90 | -------------------------------------------------------------------------------- /docs/examples/ex4.php: -------------------------------------------------------------------------------- 1 | 20 | * @copyright 2007 David JEAN LOUIS 21 | * @license http://opensource.org/licenses/mit-license.php MIT License 22 | * @version CVS: $Id$ 23 | * @link http://pear.php.net/package/Console_CommandLine 24 | * @since File available since release 0.1.0 25 | */ 26 | 27 | // Include the Console_CommandLine package. 28 | require_once 'Console/CommandLine.php'; 29 | 30 | // create the parser 31 | $xmlfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ex4.xml'; 32 | $parser = Console_CommandLine::fromXmlFile($xmlfile); 33 | 34 | // run the parser 35 | try { 36 | $result = $parser->parse(); 37 | if ($result->command_name) { 38 | $st = $result->command->options['reverse'] 39 | ? strrev($result->command->args['text']) 40 | : $result->command->args['text']; 41 | if ($result->command_name == 'foo') { 42 | echo "Foo says: $st\n"; 43 | } else if ($result->command_name == 'bar') { 44 | echo "Bar says: $st\n"; 45 | } 46 | } 47 | } catch (Exception $exc) { 48 | $parser->displayError($exc->getMessage()); 49 | } 50 | 51 | ?> 52 | -------------------------------------------------------------------------------- /docs/examples/ex4.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | A great program that can foo and bar ! 4 | 1.0.0 5 | 11 | 12 | foo 13 | output the given string with a foo prefix 14 | 20 | 21 | the text to output 22 | 23 | 24 | 25 | bar 26 | output the given string with a bar prefix 27 | 33 | 34 | the text to output 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /docs/examples/ex5.php: -------------------------------------------------------------------------------- 1 | 15 | * @link http://pear.php.net/package/Console_CommandLine 16 | */ 17 | 18 | // Include the Console_CommandLine package. 19 | require_once 'Console/CommandLine.php'; 20 | 21 | // create the parser 22 | $xmlfile = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ex5.xml'; 23 | $parser = Console_CommandLine::fromXmlFile($xmlfile); 24 | 25 | // run the parser 26 | try { 27 | $result = $parser->parse(); 28 | if ($result->command_name) { 29 | $st = implode(', ', $result->command->args['item']); 30 | echo "List says: $st\n"; 31 | } 32 | } catch (Exception $exc) { 33 | $parser->displayError($exc->getMessage()); 34 | } 35 | 36 | ?> 37 | -------------------------------------------------------------------------------- /docs/examples/ex5.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | A great program that can list foo elements ! 4 | 1.0.0 5 | 11 | 12 | list 13 | output the list of foo elements 14 | 15 | May be either "foo", "bar", "baz" 16 | true 17 | 18 | foo 19 | bar 20 | baz 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | tests/ 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /tests/AllTests.php: -------------------------------------------------------------------------------- 1 | 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 1.0.0 22 | */ 23 | 24 | if (!defined('PHPUnit_MAIN_METHOD')) { 25 | define('PHPUnit_MAIN_METHOD', 'Console_CommandLine_AllTests::main'); 26 | } 27 | 28 | if ($fp = @fopen('PHPUnit/Autoload.php', 'r', true)) { 29 | require_once 'PHPUnit/Autoload.php'; 30 | } elseif ($fp = @fopen('PHPUnit/Framework.php', 'r', true)) { 31 | require_once 'PHPUnit/Framework.php'; 32 | } else { 33 | die("skip could not find PHPUnit"); 34 | } 35 | fclose($fp); 36 | 37 | /** 38 | * Console_CommandLine phpt test suite. 39 | * 40 | * Run all tests from the package root directory: 41 | * $ phpunit Console_CommandLine_AllTests tests/AllTests.php 42 | * or 43 | * $ php tests/AllTests.php 44 | * 45 | * @category Console 46 | * @package Console_CommandLine 47 | * @author David JEAN LOUIS 48 | * @copyright 2007 David JEAN LOUIS 49 | * @license http://opensource.org/licenses/mit-license.php MIT License 50 | * @version Release: @package_version@ 51 | * @link http://pear.php.net/package/Console_CommandLine 52 | * @since Class available since release 1.0.0 53 | */ 54 | class Console_CommandLine_AllTests 55 | { 56 | /** 57 | * Runs the test suite 58 | * 59 | * @return void 60 | * @static 61 | */ 62 | public static function main() 63 | { 64 | PHPUnit_TextUI_TestRunner::run(self::suite()); 65 | } 66 | 67 | /** 68 | * Return the phpt test suite 69 | * 70 | * @return object the PHPUnit_Framework_TestSuite object 71 | * @static 72 | */ 73 | public static function suite() 74 | { 75 | return new PHPUnit_Extensions_PhptTestSuite(dirname(__FILE__)); 76 | } 77 | } 78 | 79 | if (PHPUnit_MAIN_METHOD == 'Console_CommandLine_AllTests::main') { 80 | Console_CommandLine_AllTests::main(); 81 | } 82 | 83 | ?> 84 | -------------------------------------------------------------------------------- /tests/console_commandline_accept.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::accept() method. 3 | --FILE-- 4 | accept(new CustomRenderer()); 12 | echo get_class($parser->renderer) . "\n"; 13 | // outputter 14 | $parser->accept(new CustomOutputter()); 15 | echo get_class($parser->outputter) . "\n"; 16 | $parser->accept(new CustomMessageProvider()); 17 | echo get_class($parser->message_provider) . "\n"; 18 | $parser->accept(new stdclass()); 19 | } catch (Console_CommandLine_Exception $exc) { 20 | $parser->displayError($exc->getMessage()); 21 | } 22 | 23 | ?> 24 | --EXPECT-- 25 | CustomRenderer 26 | CustomOutputter 27 | CustomMessageProvider 28 | STDERR >> CustomRenderer::error(INVALID_CUSTOM_INSTANCE) 29 | -------------------------------------------------------------------------------- /tests/console_commandline_addargument.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addArgument() method. 3 | --FILE-- 4 | addArgument('arg1'); 10 | $parser->addArgument('arg2', array( 11 | 'multiple' => true, 12 | 'description' => 'description of arg2' 13 | )); 14 | $arg3 = new Console_CommandLine_Argument('arg3', array( 15 | 'multiple' => true, 16 | 'description' => 'description of arg3' 17 | )); 18 | $parser->addArgument($arg3); 19 | $parser->addArgument('arg4', array('optional' => true)); 20 | 21 | var_dump($parser->args); 22 | 23 | // a bad argument 24 | $parser->addArgument('Some invalid name'); 25 | 26 | ?> 27 | --EXPECTF-- 28 | array(4) { 29 | ["arg1"]=> 30 | object(Console_CommandLine_Argument)#%d (8) { 31 | ["multiple"]=> 32 | bool(false) 33 | ["optional"]=> 34 | bool(false) 35 | ["choices"]=> 36 | array(0) { 37 | } 38 | ["name"]=> 39 | string(4) "arg1" 40 | ["help_name"]=> 41 | string(4) "arg1" 42 | ["description"]=> 43 | NULL 44 | ["default"]=> 45 | NULL 46 | ["messages"]=> 47 | array(0) { 48 | } 49 | } 50 | ["arg2"]=> 51 | object(Console_CommandLine_Argument)#%d (8) { 52 | ["multiple"]=> 53 | bool(true) 54 | ["optional"]=> 55 | bool(false) 56 | ["choices"]=> 57 | array(0) { 58 | } 59 | ["name"]=> 60 | string(4) "arg2" 61 | ["help_name"]=> 62 | string(4) "arg2" 63 | ["description"]=> 64 | string(19) "description of arg2" 65 | ["default"]=> 66 | NULL 67 | ["messages"]=> 68 | array(0) { 69 | } 70 | } 71 | ["arg3"]=> 72 | object(Console_CommandLine_Argument)#%d (8) { 73 | ["multiple"]=> 74 | bool(true) 75 | ["optional"]=> 76 | bool(false) 77 | ["choices"]=> 78 | array(0) { 79 | } 80 | ["name"]=> 81 | string(4) "arg3" 82 | ["help_name"]=> 83 | string(4) "arg3" 84 | ["description"]=> 85 | string(19) "description of arg3" 86 | ["default"]=> 87 | NULL 88 | ["messages"]=> 89 | array(0) { 90 | } 91 | } 92 | ["arg4"]=> 93 | object(Console_CommandLine_Argument)#%d (8) { 94 | ["multiple"]=> 95 | bool(false) 96 | ["optional"]=> 97 | bool(true) 98 | ["choices"]=> 99 | array(0) { 100 | } 101 | ["name"]=> 102 | string(4) "arg4" 103 | ["help_name"]=> 104 | string(4) "arg4" 105 | ["description"]=> 106 | NULL 107 | ["default"]=> 108 | NULL 109 | ["messages"]=> 110 | array(0) { 111 | } 112 | } 113 | } 114 | 115 | Fatal error: argument name must be a valid php variable name (got: Some invalid name) in %sCommandLine.php on line %d 116 | -------------------------------------------------------------------------------- /tests/console_commandline_addargument_2.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addArgument() method. 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | foo 7 | --FILE-- 8 | addArgument('arg1'); 14 | $parser->addArgument('arg2', array( 15 | 'optional' => true, 16 | 'default' => 'bar' 17 | )); 18 | 19 | $result = $parser->parse(); 20 | echo $result->args['arg1'] . ' ' . $result->args['arg2']; 21 | 22 | // a bad argument 23 | $parser->addArgument('arg3', array('default' => 'baz')); 24 | 25 | ?> 26 | --EXPECTF-- 27 | foo bar 28 | Fatal error: only optional arguments can have a default value in %sCommandLine.php on line %d 29 | -------------------------------------------------------------------------------- /tests/console_commandline_addcommand.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addCommand() method. 3 | --FILE-- 4 | addCommand('cmd1'); 10 | $parser->addCommand('cmd2', array( 11 | 'description' => 'description of cmd2' 12 | )); 13 | $cmd3 = new Console_CommandLine_Command(array( 14 | 'name' => 'cmd3', 15 | 'description' => 'description of cmd3' 16 | )); 17 | $parser->addCommand($cmd3); 18 | 19 | var_dump(array_keys($parser->commands)); 20 | var_dump($parser->commands['cmd2']->description); 21 | var_dump($parser->commands['cmd3']->description); 22 | 23 | ?> 24 | --EXPECT-- 25 | array(3) { 26 | [0]=> 27 | string(4) "cmd1" 28 | [1]=> 29 | string(4) "cmd2" 30 | [2]=> 31 | string(4) "cmd3" 32 | } 33 | string(19) "description of cmd2" 34 | string(19) "description of cmd3" 35 | -------------------------------------------------------------------------------- /tests/console_commandline_addcommand_2.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addCommand() method. 3 | --ARGS-- 4 | cmd1 --help 2>&1 5 | --FILE-- 6 | renderer->line_width = 30; 12 | $parser->addCommand('cmd1', array( 13 | 'description' => '1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30' 14 | )); 15 | $parser->parse(); 16 | 17 | ?> 18 | --EXPECTF-- 19 | 20 | 1 2 3 4 5 6 7 8 9 10 11 12 13 21 | 14 15 16 17 18 19 20 21 22 23 22 | 24 25 26 27 28 29 30 23 | 24 | Usage: 25 | %sconsole_commandline_addcommand_2.php 26 | [options] cmd1 [options] 27 | 28 | Options: 29 | -h, --help show this help 30 | message and exit 31 | -------------------------------------------------------------------------------- /tests/console_commandline_addcommand_3.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addCommand() method. 3 | --ARGS-- 4 | --FILE-- 5 | true)); 10 | $parser->addCommand('cmd1'); 11 | $parser->addCommand('cmd2'); 12 | $parser->addCommand('cmd3'); 13 | try { 14 | $parser->parse(); 15 | } catch (Console_CommandLine_Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECTF-- 21 | Please enter one of the following command: cmd1, cmd2, cmd3. 22 | -------------------------------------------------------------------------------- /tests/console_commandline_addoption.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addOption() method. 3 | --FILE-- 4 | addOption('opt1', array( 10 | 'short_name' => '-a' 11 | )); 12 | $parser->addOption('opt2', array( 13 | 'short_name' => '-b', 14 | 'long_name' => '--foo', 15 | 'description' => 'description of opt2', 16 | 'action' => 'StoreInt', 17 | 'help_name' => 'bar', 18 | 'choices' => array(1, 2, 3), 19 | 'add_list_option' => true, 20 | 'default' => 2 21 | )); 22 | $opt3 = new Console_CommandLine_Option('opt3', array( 23 | 'long_name' => '--bar', 24 | 'description' => 'description of opt3', 25 | )); 26 | $parser->addOption($opt3); 27 | 28 | var_dump($parser->options); 29 | 30 | ?> 31 | --EXPECTF-- 32 | array(4) { 33 | ["opt1"]=> 34 | object(Console_CommandLine_Option)#%d (14) { 35 | ["short_name"]=> 36 | string(2) "-a" 37 | ["long_name"]=> 38 | NULL 39 | ["action"]=> 40 | string(11) "StoreString" 41 | ["choices"]=> 42 | array(0) { 43 | } 44 | ["callback"]=> 45 | NULL 46 | ["action_params"]=> 47 | array(0) { 48 | } 49 | ["argument_optional"]=> 50 | bool(false) 51 | ["add_list_option"]=> 52 | bool(false) 53 | [%s]=> 54 | NULL 55 | ["name"]=> 56 | string(4) "opt1" 57 | ["help_name"]=> 58 | string(4) "opt1" 59 | ["description"]=> 60 | NULL 61 | ["default"]=> 62 | NULL 63 | ["messages"]=> 64 | array(0) { 65 | } 66 | } 67 | ["opt2"]=> 68 | object(Console_CommandLine_Option)#%d (14) { 69 | ["short_name"]=> 70 | string(2) "-b" 71 | ["long_name"]=> 72 | string(5) "--foo" 73 | ["action"]=> 74 | string(8) "StoreInt" 75 | ["choices"]=> 76 | array(3) { 77 | [0]=> 78 | int(1) 79 | [1]=> 80 | int(2) 81 | [2]=> 82 | int(3) 83 | } 84 | ["callback"]=> 85 | NULL 86 | ["action_params"]=> 87 | array(0) { 88 | } 89 | ["argument_optional"]=> 90 | bool(false) 91 | ["add_list_option"]=> 92 | bool(true) 93 | [%s]=> 94 | NULL 95 | ["name"]=> 96 | string(4) "opt2" 97 | ["help_name"]=> 98 | string(3) "bar" 99 | ["description"]=> 100 | string(19) "description of opt2" 101 | ["default"]=> 102 | int(2) 103 | ["messages"]=> 104 | array(0) { 105 | } 106 | } 107 | ["list_opt2"]=> 108 | object(Console_CommandLine_Option)#%d (14) { 109 | ["short_name"]=> 110 | NULL 111 | ["long_name"]=> 112 | string(11) "--list-opt2" 113 | ["action"]=> 114 | string(4) "List" 115 | ["choices"]=> 116 | array(0) { 117 | } 118 | ["callback"]=> 119 | NULL 120 | ["action_params"]=> 121 | array(1) { 122 | ["list"]=> 123 | array(3) { 124 | [0]=> 125 | int(1) 126 | [1]=> 127 | int(2) 128 | [2]=> 129 | int(3) 130 | } 131 | } 132 | ["argument_optional"]=> 133 | bool(false) 134 | ["add_list_option"]=> 135 | bool(false) 136 | [%s]=> 137 | NULL 138 | ["name"]=> 139 | string(9) "list_opt2" 140 | ["help_name"]=> 141 | string(9) "list_opt2" 142 | ["description"]=> 143 | string(35) "lists valid choices for option opt2" 144 | ["default"]=> 145 | NULL 146 | ["messages"]=> 147 | array(0) { 148 | } 149 | } 150 | ["opt3"]=> 151 | object(Console_CommandLine_Option)#%d (14) { 152 | ["short_name"]=> 153 | NULL 154 | ["long_name"]=> 155 | string(5) "--bar" 156 | ["action"]=> 157 | string(11) "StoreString" 158 | ["choices"]=> 159 | array(0) { 160 | } 161 | ["callback"]=> 162 | NULL 163 | ["action_params"]=> 164 | array(0) { 165 | } 166 | ["argument_optional"]=> 167 | bool(false) 168 | ["add_list_option"]=> 169 | bool(false) 170 | [%s]=> 171 | NULL 172 | ["name"]=> 173 | string(4) "opt3" 174 | ["help_name"]=> 175 | string(4) "opt3" 176 | ["description"]=> 177 | string(19) "description of opt3" 178 | ["default"]=> 179 | NULL 180 | ["messages"]=> 181 | array(0) { 182 | } 183 | } 184 | } 185 | -------------------------------------------------------------------------------- /tests/console_commandline_addoption_errors_1.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addOption() method (errors 1). 3 | --FILE-- 4 | addOption('Some invalid name'); 10 | 11 | ?> 12 | --EXPECTF-- 13 | 14 | Fatal error: option name must be a valid php variable name (got: Some invalid name) in %sCommandLine.php on line %d 15 | -------------------------------------------------------------------------------- /tests/console_commandline_addoption_errors_2.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addOption() method (errors 2). 3 | --FILE-- 4 | addOption('name', array()); 10 | 11 | ?> 12 | --EXPECTF-- 13 | 14 | Fatal error: you must provide at least an option short name or long name for option "name" in %sCommandLine.php on line %d 15 | -------------------------------------------------------------------------------- /tests/console_commandline_addoption_errors_3.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addOption() method (errors 3). 3 | --FILE-- 4 | addOption('name', array('short_name'=>'d')); 10 | 11 | ?> 12 | --EXPECTF-- 13 | 14 | Fatal error: option "name" short name must be a dash followed by a letter (got: "d") in %sCommandLine.php on line %d 15 | -------------------------------------------------------------------------------- /tests/console_commandline_addoption_errors_4.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addOption() method (errors 4). 3 | --FILE-- 4 | addOption('name', array('long_name'=>'d')); 10 | 11 | ?> 12 | --EXPECTF-- 13 | 14 | Fatal error: option "name" long name must be 2 dashes followed by a word (got: "d") in %sCommandLine.php on line %d 15 | -------------------------------------------------------------------------------- /tests/console_commandline_addoption_errors_5.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addOption() method (errors 5). 3 | --FILE-- 4 | addOption('name', array('short_name'=>'-d', 'action'=>true)); 10 | 11 | ?> 12 | --EXPECTF-- 13 | 14 | Fatal error: invalid action for option "name". in %sCommandLine.php on line %d 15 | -------------------------------------------------------------------------------- /tests/console_commandline_addoption_errors_6.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addOption() method (errors 6). 3 | --FILE-- 4 | addOption('name', array('short_name'=>'-d', 'action'=>'Inexistant')); 10 | 11 | ?> 12 | --EXPECTF-- 13 | 14 | Fatal error: unregistered action "Inexistant" for option "name". in %sCommandLine.php on line %d 15 | -------------------------------------------------------------------------------- /tests/console_commandline_addoption_errors_7.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::addOption() method (errors 7). 3 | --FILE-- 4 | addOption('name', array('short_name'=>'-d', 'action'=>'Callback')); 10 | 11 | ?> 12 | --EXPECTF-- 13 | 14 | Fatal error: you must provide a valid callback for option "name" in %sCommandLine.php on line %d 15 | -------------------------------------------------------------------------------- /tests/console_commandline_bug18682.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for bug #18682: columnWrap() in Default Renderer eats up lines with only a EOL. 3 | --ARGS-- 4 | cmd1 --help 2>&1 5 | --FILE-- 6 | columnWrap($this->parser->description, 2); 13 | } 14 | } 15 | 16 | $parser = new Console_CommandLine(); 17 | $parser->accept(new Renderer); 18 | $parser->renderer->line_width = 75; 19 | $parser->addCommand('cmd1', array( 20 | 'description' => ' 21 | Installs listed packages. 22 | 23 | local package.xml example: 24 | php pyrus.phar install package.xml 25 | 26 | local package archive example: 27 | php pyrus.phar install PackageName-1.2.0.tar 28 | 29 | remote package archive example: 30 | php pyrus.phar install http://www.example.com/PackageName-1.2.0.tgz 31 | 32 | Examples of an abstract package: 33 | php pyrus.phar install PackageName 34 | installs PackageName from the default channel with stability preferred_state 35 | php pyrus.phar pear/PackageName 36 | installs PackageName from the pear.php.net channel with stability preferred_state 37 | php pyrus.phar install channel://doc.php.net/PackageName 38 | installs PackageName from the doc.php.net channel with stability preferred_state 39 | php pyrus.phar install PackageName-beta 40 | installs PackageName from the default channel, beta or stable stability 41 | php pyrus.phar install PackageName-1.2.0 42 | installs PackageName from the default channel, version 1.2.0' 43 | )); 44 | $parser->parse(); 45 | 46 | ?> 47 | --EXPECTF-- 48 | Installs listed packages. 49 | 50 | local package.xml example: 51 | php pyrus.phar install package.xml 52 | 53 | local package archive example: 54 | php pyrus.phar install PackageName-1.2.0.tar 55 | 56 | remote package archive example: 57 | php pyrus.phar install http://www.example.com/PackageName-1.2.0.tgz 58 | 59 | Examples of an abstract package: 60 | php pyrus.phar install PackageName 61 | installs PackageName from the default channel with stability 62 | preferred_state 63 | php pyrus.phar pear/PackageName 64 | installs PackageName from the pear.php.net channel with stability 65 | preferred_state 66 | php pyrus.phar install channel://doc.php.net/PackageName 67 | installs PackageName from the doc.php.net channel with stability 68 | preferred_state 69 | php pyrus.phar install PackageName-beta 70 | installs PackageName from the default channel, beta or stable stability 71 | php pyrus.phar install PackageName-1.2.0 72 | installs PackageName from the default channel, version 1.2.0 73 | 74 | Usage: 75 | %sconsole_commandline_bug18682.php 76 | [options] cmd1 [options] 77 | 78 | Options: 79 | -h, --help show this help message and exit 80 | -------------------------------------------------------------------------------- /tests/console_commandline_fromxmlfile.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::fromXmlFile() method. 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --help 2>&1 7 | --FILE-- 8 | parse(); 14 | 15 | ?> 16 | --EXPECTF-- 17 | zip/unzip files 18 | 19 | Usage: 20 | test [options] 21 | test [options] [options] [args] 22 | 23 | Options: 24 | -c choice, --choice=choice choice option 25 | --list-choice lists valid choices for option choice 26 | -p password, --password=password zip file password 27 | -v, --verbose turn on verbose output 28 | -h, --help show this help message and exit 29 | --version show the program version and exit 30 | 31 | Commands: 32 | zip zip given files in the destination file (aliases: compress, zp) 33 | unzip unzip given file in the destination dir (alias: uzp) 34 | -------------------------------------------------------------------------------- /tests/console_commandline_fromxmlfile_1.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::fromXmlFile() method. 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | zip --help 2>&1 7 | --FILE-- 8 | parse(); 14 | 15 | ?> 16 | --EXPECTF-- 17 | zip given files in the destination file 18 | 19 | Usage: 20 | test [options] zip [options] files1 files2 ... zipfile 21 | 22 | Options: 23 | -h, --help show this help message and exit 24 | 25 | Arguments: 26 | files a list of files to zip together 27 | zipfile path to the zip file to generate 28 | -------------------------------------------------------------------------------- /tests/console_commandline_fromxmlfile_2.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::fromXmlFile() method. 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | unzip --help 2>&1 7 | --FILE-- 8 | parse(); 14 | 15 | ?> 16 | --EXPECTF-- 17 | unzip given file in the destination dir 18 | 19 | Usage: 20 | test [options] unzip [options] outputdir zipfile 21 | 22 | Options: 23 | -h, --help show this help message and exit 24 | 25 | Arguments: 26 | outputdir destination directory 27 | zipfile path to the zip file to unzip 28 | -------------------------------------------------------------------------------- /tests/console_commandline_fromxmlfile_error.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::fromXmlFile() method (error). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --help 2>&1 7 | --FILE-- 8 | parse(); 15 | 16 | ?> 17 | --EXPECTF-- 18 | 19 | Fatal error: XML definition file "%sunexisting.xml" does not exists or is not readable in %sCommandLine.php on line %d 20 | -------------------------------------------------------------------------------- /tests/console_commandline_fromxmlstring.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::fromXmlString() method. 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --help 2>&1 7 | --FILE-- 8 | parse(); 15 | 16 | ?> 17 | --EXPECT-- 18 | zip/unzip files 19 | 20 | Usage: 21 | test [options] 22 | test [options] [options] [args] 23 | 24 | Options: 25 | -c choice, --choice=choice choice option 26 | --list-choice lists valid choices for option choice 27 | -p password, --password=password zip file password 28 | -v, --verbose turn on verbose output 29 | -h, --help show this help message and exit 30 | --version show the program version and exit 31 | 32 | Commands: 33 | zip zip given files in the destination file (aliases: compress, zp) 34 | unzip unzip given file in the destination dir (alias: uzp) 35 | -------------------------------------------------------------------------------- /tests/console_commandline_options_defaults.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine options defaults. 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | force_options_defaults = true; 13 | $result = $parser->parse(); 14 | foreach ($result->options as $k => $v) { 15 | echo $k . ":"; var_dump($v); 16 | } 17 | } catch (Console_CommandLine_Exception $exc) { 18 | $parser->displayError($exc->getMessage()); 19 | } 20 | 21 | ?> 22 | --EXPECT-- 23 | true:bool(false) 24 | false:bool(true) 25 | int:int(0) 26 | float:float(0) 27 | string:NULL 28 | counter:int(0) 29 | callback:NULL 30 | array:array(0) { 31 | } 32 | password:NULL 33 | help:NULL 34 | version:NULL 35 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_1.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (--version). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --version 7 | --FILE-- 8 | parse(); 14 | 15 | ?> 16 | --EXPECT-- 17 | some_program version 0.1.0. 18 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_10.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (subcommand). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | -v install -f foo 7 | --FILE-- 8 | parse(); 14 | var_dump($result->options); 15 | var_dump($result->command_name); 16 | var_dump($result->command->options); 17 | 18 | ?> 19 | --EXPECT-- 20 | array(4) { 21 | ["verbose"]=> 22 | bool(true) 23 | ["logfile"]=> 24 | NULL 25 | ["help"]=> 26 | NULL 27 | ["version"]=> 28 | NULL 29 | } 30 | string(7) "install" 31 | array(2) { 32 | ["force"]=> 33 | bool(true) 34 | ["help"]=> 35 | NULL 36 | } 37 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_11.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (subcommand help 1). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --help 2>&1 7 | --FILE-- 8 | parse(); 14 | 15 | ?> 16 | --EXPECT-- 17 | Description of our parser goes here... 18 | 19 | Usage: 20 | some_program [options] 21 | some_program [options] [options] [args] 22 | 23 | Options: 24 | -v, --verbose verbose mode 25 | -l logfile, --logfile=logfile path to logfile 26 | -h, --help show this help message and exit 27 | --version show the program version and exit 28 | 29 | Commands: 30 | install install given package (aliases: inst, instbis) 31 | uninstall uninstall given package 32 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_12.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (subcommand help 2). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | inst --help 2>&1 7 | --FILE-- 8 | parse(); 14 | 15 | ?> 16 | --EXPECT-- 17 | install given package 18 | 19 | Usage: 20 | some_program [options] install [options] package 21 | 22 | Options: 23 | -f, --force force installation 24 | -h, --help show this help message and exit 25 | 26 | Arguments: 27 | package package to install 28 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_13.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (user errors 1). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --float=foo foo bar 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Option "float" requires a value of type float (got "foo"). 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_14.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (user errors 2). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --int=foo foo bar 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Option "int" requires a value of type int (got "foo"). 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_15.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (subcommand error). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | install -f 2>&1 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | $parser->displayError($exc->getMessage()); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Error: You must provide at least 1 argument. 22 | Type "some_program --help" to get help. 23 | Type "some_program --help" to get help on specific command. 24 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_16.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (user errors 3). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | -s fooz foo bar 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Option "string" must be one of the following: "foo", "bar", "baz" (got "fooz"). 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_17.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (user argc/argv 1). 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | parse($argc, $argv); 15 | var_dump($result); 16 | } catch (Console_CommandLine_Exception $exc) { 17 | $parser->displayError($exc->getMessage()); 18 | } 19 | 20 | ?> 21 | --EXPECTF-- 22 | object(Console_CommandLine_Result)#%d (4) { 23 | ["options"]=> 24 | array(11) { 25 | ["true"]=> 26 | bool(true) 27 | ["false"]=> 28 | bool(false) 29 | ["int"]=> 30 | int(1) 31 | ["float"]=> 32 | float(1.2) 33 | ["string"]=> 34 | NULL 35 | ["counter"]=> 36 | NULL 37 | ["callback"]=> 38 | NULL 39 | ["array"]=> 40 | array(2) { 41 | [0]=> 42 | string(4) "spam" 43 | [1]=> 44 | string(3) "egg" 45 | } 46 | ["password"]=> 47 | NULL 48 | ["help"]=> 49 | NULL 50 | ["version"]=> 51 | NULL 52 | } 53 | ["args"]=> 54 | array(2) { 55 | ["simple"]=> 56 | string(3) "foo" 57 | ["multiple"]=> 58 | array(1) { 59 | [0]=> 60 | string(3) "bar" 61 | } 62 | } 63 | ["command_name"]=> 64 | bool(false) 65 | ["command"]=> 66 | bool(false) 67 | } 68 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_18.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (user argc/argv 2). 3 | --SKIPIF-- 4 | 5 | --FILE-- 6 | parse($argc, $argv); 15 | var_dump($result); 16 | } catch (Console_CommandLine_Exception $exc) { 17 | $parser->displayError($exc->getMessage()); 18 | } 19 | 20 | ?> 21 | --EXPECTF-- 22 | object(Console_CommandLine_Result)#%d (4) { 23 | ["options"]=> 24 | array(4) { 25 | ["verbose"]=> 26 | bool(true) 27 | ["logfile"]=> 28 | NULL 29 | ["help"]=> 30 | NULL 31 | ["version"]=> 32 | NULL 33 | } 34 | ["args"]=> 35 | array(0) { 36 | } 37 | ["command_name"]=> 38 | string(7) "install" 39 | ["command"]=> 40 | object(Console_CommandLine_Result)#%d (4) { 41 | ["options"]=> 42 | array(2) { 43 | ["force"]=> 44 | bool(true) 45 | ["help"]=> 46 | NULL 47 | } 48 | ["args"]=> 49 | array(1) { 50 | ["package"]=> 51 | string(3) "foo" 52 | } 53 | ["command_name"]=> 54 | bool(false) 55 | ["command"]=> 56 | bool(false) 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_19.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (subcommand help 1). 3 | --STDIN-- 4 | some_package 5 | --SKIPIF-- 6 | 7 | --ARGS-- 8 | -v instbis -f - 9 | --FILE-- 10 | parse(); 17 | print_r($result); 18 | } catch (Exception $exc) { 19 | echo $exc->getMessage(); 20 | } 21 | 22 | ?> 23 | --EXPECT-- 24 | Console_CommandLine_Result Object 25 | ( 26 | [options] => Array 27 | ( 28 | [verbose] => 1 29 | [logfile] => 30 | [help] => 31 | [version] => 32 | ) 33 | 34 | [args] => Array 35 | ( 36 | ) 37 | 38 | [command_name] => install 39 | [command] => Console_CommandLine_Result Object 40 | ( 41 | [options] => Array 42 | ( 43 | [force] => 1 44 | [help] => 45 | ) 46 | 47 | [args] => Array 48 | ( 49 | [package] => some_package 50 | 51 | ) 52 | 53 | [command_name] => 54 | [command] => 55 | ) 56 | 57 | ) 58 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_2.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (--help). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --help 2>&1 7 | --FILE-- 8 | parse(); 14 | 15 | ?> 16 | --EXPECT-- 17 | Description of our parser goes here... 18 | 19 | Usage: 20 | some_program [options] simple [multiple1 multiple2 ...] 21 | 22 | Options: 23 | -t, --true test the StoreTrue action 24 | -f, --false test the StoreFalse action 25 | --int=INT test the StoreInt action 26 | --float=FLOAT test the StoreFloat action 27 | -s STRING, --string=STRING test the StoreString action 28 | -c, --counter test the Counter action 29 | --callback=callback test the Callback action 30 | -a ARRAY, --array=ARRAY test the StoreArray action 31 | -p password, --password=password test the Password action 32 | -h, --help show this help message and exit 33 | -v, --version show the program version and exit 34 | 35 | Arguments: 36 | simple test a simple argument 37 | multiple test a multiple argument 38 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_20.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::fromXmlFile() method. 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --list-choice 7 | --FILE-- 8 | parse(); 14 | 15 | ?> 16 | --EXPECTF-- 17 | Valid choices are: ham, spam 18 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_21.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (--help with renderer options). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --help 2>&1 7 | --FILE-- 8 | renderer->line_width = 0; 14 | $parser->renderer->options_on_different_lines = true; 15 | $parser->parse(); 16 | 17 | ?> 18 | --EXPECT-- 19 | Description of our parser goes here... 20 | 21 | Usage: 22 | some_program [options] simple [multiple1 multiple2 ...] 23 | 24 | Options: 25 | -t 26 | --true test the StoreTrue action 27 | -f 28 | --false test the StoreFalse action 29 | --int=INT test the StoreInt action 30 | --float=FLOAT test the StoreFloat action 31 | -s STRING 32 | --string=STRING test the StoreString action 33 | -c 34 | --counter test the Counter action 35 | --callback=callback test the Callback action 36 | -a ARRAY 37 | --array=ARRAY test the StoreArray action 38 | -p password 39 | --password=password test the Password action 40 | -h 41 | --help show this help message and exit 42 | -v 43 | --version show the program version and exit 44 | 45 | Arguments: 46 | simple test a simple argument 47 | multiple test a multiple argument 48 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_22.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (--help with renderer options). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --list 7 | --FILE-- 8 | addOption('list', array( 14 | 'long_name' => '--list', 15 | 'action' => 'List', 16 | 'action_params' => array( 17 | 'list' => array('foo', 'bar', 'baz'), 18 | 'message' => 'foobarbaz---', 19 | 'delimiter' => '|', 20 | 'post' => '---foobarbaz', 21 | ), 22 | )); 23 | $parser->parse(); 24 | 25 | ?> 26 | --EXPECT-- 27 | foobarbaz---foo|bar|baz---foobarbaz 28 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_23.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (invalid subcommand detection). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | -v invalid subcommand 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Command "invalid" is not valid. 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_24.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (invalid subcommand detection). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | upgrade 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Package name is required. 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_25.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (invalid subcommand detection). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | upgrade -s foo 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Valid states are "stable" and "beta". 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_26.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (invalid subcommand detection). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | upgrade -s 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Option requires value. 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_27.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (invalid subcommand detection). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | upgrade -t 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Mysterious option encountered. 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_28.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (invalid subcommand detection). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | upgrade --dry-run=foo 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Option should not have a value. 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_29.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (invalid subcommand detection). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | foo 7 | --FILE-- 8 | parse(); 15 | } catch (Exception $exc) { 16 | echo $exc->getMessage(); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Only "upgrade" is supported. 22 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_3.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (various options). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | -tfsfoo --int=3 --flo 4.0 -cccc --callback=somestring -a foo bar baz foo bar 7 | --FILE-- 8 | parse(); 14 | var_dump($result->options); 15 | var_dump($result->args); 16 | 17 | ?> 18 | --EXPECT-- 19 | array(11) { 20 | ["true"]=> 21 | bool(true) 22 | ["false"]=> 23 | bool(false) 24 | ["int"]=> 25 | int(3) 26 | ["float"]=> 27 | float(4) 28 | ["string"]=> 29 | string(3) "foo" 30 | ["counter"]=> 31 | int(4) 32 | ["callback"]=> 33 | string(20) "foo__fbzrfgevat__bar" 34 | ["array"]=> 35 | array(3) { 36 | [0]=> 37 | string(3) "foo" 38 | [1]=> 39 | string(3) "bar" 40 | [2]=> 41 | string(3) "baz" 42 | } 43 | ["password"]=> 44 | NULL 45 | ["help"]=> 46 | NULL 47 | ["version"]=> 48 | NULL 49 | } 50 | array(2) { 51 | ["simple"]=> 52 | string(3) "foo" 53 | ["multiple"]=> 54 | array(1) { 55 | [0]=> 56 | string(3) "bar" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_4.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (errors 1). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | -d 2>&1 7 | --FILE-- 8 | parse(); 15 | } catch (Console_CommandLine_Exception $exc) { 16 | $parser->displayError($exc->getMessage()); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Error: Unknown option "-d". 22 | Type "some_program --help" to get help. 23 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_5.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (errors 2). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --float 2>&1 7 | --FILE-- 8 | parse(); 15 | } catch (Console_CommandLine_Exception $exc) { 16 | $parser->displayError($exc->getMessage()); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Error: Option "float" requires a value. 22 | Type "some_program --help" to get help. 23 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_6.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (errors 3). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | --float=1.2 2>&1 7 | --FILE-- 8 | parse(); 15 | } catch (Console_CommandLine_Exception $exc) { 16 | $parser->displayError($exc->getMessage()); 17 | } 18 | 19 | ?> 20 | --EXPECT-- 21 | Error: You must provide at least 1 argument. 22 | Type "some_program --help" to get help. 23 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_7.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (special cases 1). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | -t -- -f - --float=1.2 foo 2>&1 7 | --FILE-- 8 | parse(); 15 | var_dump($result); 16 | } catch (Console_CommandLine_Exception $exc) { 17 | $parser->displayError($exc->getMessage()); 18 | } 19 | 20 | ?> 21 | --EXPECTF-- 22 | object(Console_CommandLine_Result)#%d (4) { 23 | ["options"]=> 24 | array(11) { 25 | ["true"]=> 26 | bool(true) 27 | ["false"]=> 28 | NULL 29 | ["int"]=> 30 | int(1) 31 | ["float"]=> 32 | float(1) 33 | ["string"]=> 34 | NULL 35 | ["counter"]=> 36 | NULL 37 | ["callback"]=> 38 | NULL 39 | ["array"]=> 40 | array(2) { 41 | [0]=> 42 | string(4) "spam" 43 | [1]=> 44 | string(3) "egg" 45 | } 46 | ["password"]=> 47 | NULL 48 | ["help"]=> 49 | NULL 50 | ["version"]=> 51 | NULL 52 | } 53 | ["args"]=> 54 | array(2) { 55 | ["simple"]=> 56 | string(2) "-f" 57 | ["multiple"]=> 58 | array(3) { 59 | [0]=> 60 | string(1) "-" 61 | [1]=> 62 | string(11) "--float=1.2" 63 | [2]=> 64 | string(3) "foo" 65 | } 66 | } 67 | ["command_name"]=> 68 | bool(false) 69 | ["command"]=> 70 | bool(false) 71 | } 72 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_8.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (special cases 2). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | -t foo bar -f 2>&1 7 | --FILE-- 8 | force_posix = true; 15 | $result = $parser->parse(); 16 | var_dump($result); 17 | } catch (Console_CommandLine_Exception $exc) { 18 | $parser->displayError($exc->getMessage()); 19 | } 20 | 21 | ?> 22 | --EXPECTF-- 23 | object(Console_CommandLine_Result)#%d (4) { 24 | ["options"]=> 25 | array(11) { 26 | ["true"]=> 27 | bool(true) 28 | ["false"]=> 29 | NULL 30 | ["int"]=> 31 | int(1) 32 | ["float"]=> 33 | float(1) 34 | ["string"]=> 35 | NULL 36 | ["counter"]=> 37 | NULL 38 | ["callback"]=> 39 | NULL 40 | ["array"]=> 41 | array(2) { 42 | [0]=> 43 | string(4) "spam" 44 | [1]=> 45 | string(3) "egg" 46 | } 47 | ["password"]=> 48 | NULL 49 | ["help"]=> 50 | NULL 51 | ["version"]=> 52 | NULL 53 | } 54 | ["args"]=> 55 | array(2) { 56 | ["simple"]=> 57 | string(3) "foo" 58 | ["multiple"]=> 59 | array(2) { 60 | [0]=> 61 | string(3) "bar" 62 | [1]=> 63 | string(2) "-f" 64 | } 65 | } 66 | ["command_name"]=> 67 | bool(false) 68 | ["command"]=> 69 | bool(false) 70 | } 71 | -------------------------------------------------------------------------------- /tests/console_commandline_parse_9.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() method (password option). 3 | --SKIPIF-- 4 | 5 | --ARGS-- 6 | -p -- foo bar 7 | --STDIN-- 8 | secretpass 9 | --FILE-- 10 | parse(); 16 | var_dump($result->options); 17 | var_dump($result->args); 18 | 19 | ?> 20 | --EXPECTF-- 21 | Password%s array(11) { 22 | ["true"]=> 23 | NULL 24 | ["false"]=> 25 | NULL 26 | ["int"]=> 27 | int(1) 28 | ["float"]=> 29 | float(1) 30 | ["string"]=> 31 | NULL 32 | ["counter"]=> 33 | NULL 34 | ["callback"]=> 35 | NULL 36 | ["array"]=> 37 | array(2) { 38 | [0]=> 39 | string(4) "spam" 40 | [1]=> 41 | string(3) "egg" 42 | } 43 | ["password"]=> 44 | string(10) "secretpass" 45 | ["help"]=> 46 | NULL 47 | ["version"]=> 48 | NULL 49 | } 50 | array(2) { 51 | ["simple"]=> 52 | string(3) "foo" 53 | ["multiple"]=> 54 | array(1) { 55 | [0]=> 56 | string(3) "bar" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /tests/console_commandline_webrequest_1.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() with a web request 1 3 | --GET-- 4 | version=1 5 | --FILE-- 6 | parse(); 12 | 13 | ?> 14 | --EXPECT-- 15 | some_program version 0.1.0. 16 | -------------------------------------------------------------------------------- /tests/console_commandline_webrequest_2.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() with a web request 2 3 | --GET-- 4 | help 5 | --FILE-- 6 | parse(); 12 | 13 | ?> 14 | --EXPECT-- 15 | Description of our parser goes here... 16 | 17 | Usage: 18 | some_program [options] simple [multiple1 multiple2 ...] 19 | 20 | Options: 21 | -t, --true test the StoreTrue action 22 | -f, --false test the StoreFalse action 23 | --int=INT test the StoreInt action 24 | --float=FLOAT test the StoreFloat action 25 | -s STRING, --string=STRING test the StoreString action 26 | -c, --counter test the Counter action 27 | --callback=callback test the Callback action 28 | -a ARRAY, --array=ARRAY test the StoreArray action 29 | -p password, --password=password test the Password action 30 | -h, --help show this help message and exit 31 | -v, --version show the program version and exit 32 | 33 | Arguments: 34 | simple test a simple argument 35 | multiple test a multiple argument 36 | -------------------------------------------------------------------------------- /tests/console_commandline_webrequest_3.phpt: -------------------------------------------------------------------------------- 1 | --TEST-- 2 | Test for Console_CommandLine::parse() with a web request 3 3 | --POST-- 4 | true=1&false=1&string=foo&int=3&float=4.0&callback=somestring&-a[]=foo&-a[]=bar&-a[]=baz&simple=foo&multiple=bar 5 | --FILE-- 6 | parse(); 12 | var_dump($result->options); 13 | var_dump($result->args); 14 | 15 | ?> 16 | --EXPECT-- 17 | array(11) { 18 | ["true"]=> 19 | bool(true) 20 | ["false"]=> 21 | bool(false) 22 | ["int"]=> 23 | int(3) 24 | ["float"]=> 25 | float(4) 26 | ["string"]=> 27 | string(3) "foo" 28 | ["counter"]=> 29 | NULL 30 | ["callback"]=> 31 | string(20) "foo__fbzrfgevat__bar" 32 | ["array"]=> 33 | array(3) { 34 | [0]=> 35 | string(3) "foo" 36 | [1]=> 37 | string(3) "bar" 38 | [2]=> 39 | string(3) "baz" 40 | } 41 | ["password"]=> 42 | NULL 43 | ["help"]=> 44 | NULL 45 | ["version"]=> 46 | NULL 47 | } 48 | array(2) { 49 | ["simple"]=> 50 | string(3) "foo" 51 | ["multiple"]=> 52 | array(1) { 53 | [0]=> 54 | string(3) "bar" 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | true 5 | test 6 | zip/unzip files 7 | 1.0.0 8 | 9 | 23 | 29 | 35 | 36 | 37 | compress 38 | zp 39 | 40 | zip 41 | 42 | zip given files in the destination file 43 | 44 | a list of files to zip together 45 | True 46 | 47 | 48 | 49 | path to the zip file to generate 50 | 51 | 52 | 53 | 54 | destination directory 55 | False 56 | 57 | unzip 58 | 59 | uzp 60 | 61 | unzip given file in the destination dir 62 | 63 | path to the zip file to unzip 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /tests/tests.inc.php: -------------------------------------------------------------------------------- 1 | 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 | */ 23 | 24 | if (php_sapi_name() != 'cli') { 25 | // tests with php-cgi need this 26 | ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR 27 | . dirname(__FILE__) . '/../'); 28 | } 29 | 30 | /** 31 | * Required classes 32 | */ 33 | require_once 'Console/CommandLine.php'; 34 | require_once 'Console/CommandLine/Renderer.php'; 35 | require_once 'Console/CommandLine/Outputter.php'; 36 | require_once 'Console/CommandLine/MessageProvider.php'; 37 | 38 | // rot13Callback() {{{ 39 | 40 | /** 41 | * A dummy callback for tests purposes. 42 | * 43 | * @param mixed $value value provided by the user 44 | * @param object $option the option instance 45 | * @param object $result the result instance 46 | * @param object $parser the parser instance 47 | * @param array $params optional params array 48 | * 49 | * @return string 50 | */ 51 | function rot13Callback($value, $option, $result, $parser, $params=array()) 52 | { 53 | $ret = ''; 54 | if (isset($params['prefix'])) { 55 | $ret .= $params['prefix'] . '__'; 56 | } 57 | $ret .= str_rot13($value); 58 | if (isset($params['suffix'])) { 59 | $ret .= '__' . $params['suffix']; 60 | } 61 | return $ret; 62 | } 63 | 64 | // }}} 65 | // buildParser1() {{{ 66 | 67 | /** 68 | * Build a parser instance and return it. 69 | * 70 | * @return object Console_CommandLine instance 71 | */ 72 | function buildParser1() 73 | { 74 | $parser = new Console_CommandLine(); 75 | $parser->name = 'some_program'; 76 | $parser->version = '0.1.0'; 77 | $parser->description = 'Description of our parser goes here...'; 78 | 79 | // add options 80 | $parser->addOption('true', array( 81 | 'short_name' => '-t', 82 | 'long_name' => '--true', 83 | 'action' => 'StoreTrue', 84 | 'description' => 'test the StoreTrue action' 85 | )); 86 | $parser->addOption('false', array( 87 | 'short_name' => '-f', 88 | 'long_name' => '--false', 89 | 'action' => 'StoreFalse', 90 | 'description' => 'test the StoreFalse action' 91 | )); 92 | $parser->addOption('int', array( 93 | 'long_name' => '--int', 94 | 'action' => 'StoreInt', 95 | 'description' => 'test the StoreInt action', 96 | 'help_name' => 'INT', 97 | 'default' => 1 98 | )); 99 | $parser->addOption('float', array( 100 | 'long_name' => '--float', 101 | 'action' => 'StoreFloat', 102 | 'description' => 'test the StoreFloat action', 103 | 'help_name' => 'FLOAT', 104 | 'default' => 1.0 105 | )); 106 | $parser->addOption('string', array( 107 | 'short_name' => '-s', 108 | 'long_name' => '--string', 109 | 'action' => 'StoreString', 110 | 'description' => 'test the StoreString action', 111 | 'help_name' => 'STRING', 112 | 'choices' => array('foo', 'bar', 'baz') 113 | )); 114 | $parser->addOption('counter', array( 115 | 'short_name' => '-c', 116 | 'long_name' => '--counter', 117 | 'action' => 'Counter', 118 | 'description' => 'test the Counter action' 119 | )); 120 | $parser->addOption('callback', array( 121 | 'long_name' => '--callback', 122 | 'action' => 'Callback', 123 | 'description' => 'test the Callback action', 124 | 'callback' => 'rot13Callback', 125 | 'action_params' => array('prefix' => 'foo', 'suffix' => 'bar') 126 | )); 127 | $parser->addOption('array', array( 128 | 'short_name' => '-a', 129 | 'long_name' => '--array', 130 | 'default' => array('spam', 'egg'), 131 | 'action' => 'StoreArray', 132 | 'help_name' => 'ARRAY', 133 | 'description' => 'test the StoreArray action' 134 | )); 135 | $parser->addOption('password', array( 136 | 'short_name' => '-p', 137 | 'long_name' => '--password', 138 | 'action' => 'Password', 139 | 'description' => 'test the Password action' 140 | )); 141 | $parser->addArgument('simple', array( 142 | 'description' => 'test a simple argument' 143 | )); 144 | $parser->addArgument('multiple', array( 145 | 'description' => 'test a multiple argument', 146 | 'multiple' => true, 147 | 'optional' => true 148 | )); 149 | return $parser; 150 | } 151 | 152 | // }}} 153 | // buildParser2() {{{ 154 | 155 | /** 156 | * Build a parser instance and return it. 157 | * 158 | * @return object Console_CommandLine instance 159 | */ 160 | function buildParser2() 161 | { 162 | $parser = new Console_CommandLine(); 163 | $parser->name = 'some_program'; 164 | $parser->version = '0.1.0'; 165 | $parser->description = 'Description of our parser goes here...'; 166 | 167 | // add general options 168 | $parser->addOption('verbose', array( 169 | 'short_name' => '-v', 170 | 'long_name' => '--verbose', 171 | 'action' => 'StoreTrue', 172 | 'description' => 'verbose mode' 173 | )); 174 | $parser->addOption('logfile', array( 175 | 'short_name' => '-l', 176 | 'long_name' => '--logfile', 177 | 'action' => 'StoreString', 178 | 'description' => 'path to logfile' 179 | )); 180 | 181 | // install subcommand 182 | $cmd1 = $parser->addCommand('install', array( 183 | 'description' => 'install given package', 184 | 'aliases' => array('inst', 'instbis'), 185 | )); 186 | $cmd1->addOption('force', array( 187 | 'short_name' => '-f', 188 | 'long_name' => '--force', 189 | 'action' => 'StoreTrue', 190 | 'description' => 'force installation' 191 | )); 192 | $cmd1->addArgument('package', array( 193 | 'description' => 'package to install' 194 | )); 195 | 196 | // uninstall subcommand 197 | $cmd2 = $parser->addCommand('uninstall', array( 198 | 'description' => 'uninstall given package' 199 | )); 200 | $cmd2->addArgument('package', array( 201 | 'description' => 'package to uninstall' 202 | )); 203 | return $parser; 204 | } 205 | 206 | // }}} 207 | // buildParser3() {{{ 208 | 209 | /** 210 | * Build a parser instance and return it. 211 | * 212 | * @return object Console_CommandLine instance 213 | */ 214 | function buildParser3() 215 | { 216 | $parser = new Console_CommandLine(); 217 | $parser->name = 'some_program'; 218 | $parser->version = '0.1.0'; 219 | $parser->description = 'Description of our parser goes here...'; 220 | // we force options default values 221 | $parser->force_options_defaults = true; 222 | 223 | // add options 224 | $parser->addOption('true', array( 225 | 'short_name' => '-t', 226 | 'long_name' => '--true', 227 | 'action' => 'StoreTrue', 228 | 'description' => 'test the StoreTrue action', 229 | )); 230 | $parser->addOption('false', array( 231 | 'short_name' => '-f', 232 | 'long_name' => '--false', 233 | 'action' => 'StoreFalse', 234 | 'description' => 'test the StoreFalse action', 235 | )); 236 | $parser->addOption('int', array( 237 | 'long_name' => '--int', 238 | 'action' => 'StoreInt', 239 | 'description' => 'test the StoreInt action', 240 | 'help_name' => 'INT', 241 | )); 242 | $parser->addOption('float', array( 243 | 'long_name' => '--float', 244 | 'action' => 'StoreFloat', 245 | 'description' => 'test the StoreFloat action', 246 | 'help_name' => 'FLOAT', 247 | )); 248 | $parser->addOption('string', array( 249 | 'short_name' => '-s', 250 | 'long_name' => '--string', 251 | 'action' => 'StoreString', 252 | 'description' => 'test the StoreString action', 253 | 'help_name' => 'STRING', 254 | 'choices' => array('foo', 'bar', 'baz') 255 | )); 256 | $parser->addOption('counter', array( 257 | 'short_name' => '-c', 258 | 'long_name' => '--counter', 259 | 'action' => 'Counter', 260 | 'description' => 'test the Counter action' 261 | )); 262 | $parser->addOption('callback', array( 263 | 'long_name' => '--callback', 264 | 'action' => 'Callback', 265 | 'description' => 'test the Callback action', 266 | 'callback' => 'rot13Callback', 267 | 'action_params' => array('prefix' => 'foo', 'suffix' => 'bar') 268 | )); 269 | $parser->addOption('array', array( 270 | 'short_name' => '-a', 271 | 'long_name' => '--array', 272 | 'action' => 'StoreArray', 273 | 'help_name' => 'ARRAY', 274 | 'description' => 'test the StoreArray action' 275 | )); 276 | $parser->addOption('password', array( 277 | 'short_name' => '-p', 278 | 'long_name' => '--password', 279 | 'action' => 'Password', 280 | 'description' => 'test the Password action' 281 | )); 282 | return $parser; 283 | } 284 | 285 | // }}} 286 | // {{{ buildParser4() 287 | 288 | /** 289 | * Build a parser instance and return it. 290 | * 291 | * For testing custom messages. 292 | * 293 | * @return object Console_CommandLine instance 294 | */ 295 | function buildParser4() 296 | { 297 | $parser = new Console_CommandLine(array( 298 | 'messages' => array( 299 | 'INVALID_SUBCOMMAND' => 'Only "upgrade" is supported.', 300 | ), 301 | )); 302 | $parser->name = 'some_program'; 303 | $parser->version = '0.1.0'; 304 | $parser->description = 'Description of our parser goes here...'; 305 | 306 | // some subcommand 307 | $cmd1 = $parser->addCommand('upgrade', array( 308 | 'description' => 'upgrade given package', 309 | 'aliases' => array('up'), 310 | 'messages' => array( 311 | 'ARGUMENT_REQUIRED' => 'Package name is required.', 312 | 'OPTION_VALUE_REQUIRED' => 'Option requires value.', 313 | 'OPTION_VALUE_UNEXPECTED' => 'Option should not have a value.', 314 | 'OPTION_UNKNOWN' => 'Mysterious option encountered.', 315 | ), 316 | )); 317 | // add option 318 | $cmd1->addOption('state', array( 319 | 'short_name' => '-s', 320 | 'long_name' => '--state', 321 | 'action' => 'StoreString', 322 | 'choices' => array('stable', 'beta'), 323 | 'description' => 'accepted package states', 324 | 'messages' => array( 325 | 'OPTION_VALUE_NOT_VALID' => 'Valid states are "stable" and "beta".', 326 | ), 327 | )); 328 | // add another option 329 | $cmd1->addOption('dry_run', array( 330 | 'short_name' => '-d', 331 | 'long_name' => '--dry-run', 332 | 'action' => 'StoreTrue', 333 | 'description' => 'dry run', 334 | )); 335 | // add argument 336 | $cmd1->addArgument('package', array( 337 | 'description' => 'package to upgrade' 338 | )); 339 | 340 | return $parser; 341 | } 342 | 343 | // }}} 344 | // CustomRenderer() {{{ 345 | 346 | /** 347 | * Some custom renderer for tests purposes. 348 | * 349 | * @category Console 350 | * @package Console_CommandLine 351 | * @author David JEAN LOUIS 352 | * @copyright 2007 David JEAN LOUIS 353 | * @license http://opensource.org/licenses/mit-license.php MIT License 354 | * @version Release: @package_version@ 355 | * @link http://pear.php.net/package/Console_CommandLine 356 | * @since File available since release 0.1.0 357 | */ 358 | class CustomRenderer implements Console_CommandLine_Renderer 359 | { 360 | // usage() {{{ 361 | 362 | /** 363 | * Return the full usage message 364 | * 365 | * @return string the usage message 366 | * @access public 367 | */ 368 | public function usage() 369 | { 370 | return __METHOD__ . '()'; 371 | } 372 | // }}} 373 | // error() {{{ 374 | 375 | /** 376 | * Return a formatted error message 377 | * 378 | * @param string $error the error message to format 379 | * 380 | * @return string the error string 381 | * @access public 382 | */ 383 | public function error($error) 384 | { 385 | return __METHOD__ . "($error)"; 386 | } 387 | 388 | // }}} 389 | // version() {{{ 390 | 391 | /** 392 | * Return the program version string 393 | * 394 | * @return string the version string 395 | * @access public 396 | */ 397 | public function version() 398 | { 399 | return __METHOD__ . '()'; 400 | } 401 | 402 | // }}} 403 | } 404 | 405 | // }}} 406 | // CustomOutputter() {{{ 407 | 408 | /** 409 | * Some custom outputter for tests purposes. 410 | * 411 | * @category Console 412 | * @package Console_CommandLine 413 | * @author David JEAN LOUIS 414 | * @copyright 2007 David JEAN LOUIS 415 | * @license http://opensource.org/licenses/mit-license.php MIT License 416 | * @version Release: @package_version@ 417 | * @link http://pear.php.net/package/Console_CommandLine 418 | * @since File available since release 0.1.0 419 | */ 420 | class CustomOutputter implements Console_CommandLine_Outputter 421 | { 422 | // stdout() {{{ 423 | 424 | /** 425 | * Called for stdout messages. 426 | * 427 | * @param string $msg the message to output 428 | * 429 | * @return void 430 | * @access public 431 | */ 432 | public function stdout($msg) 433 | { 434 | echo "STDOUT >> $msg\n"; 435 | } 436 | 437 | // }}} 438 | // stderr() {{{ 439 | 440 | /** 441 | * Called for stderr messages. 442 | * 443 | * @param string $msg the message to output 444 | * 445 | * @return void 446 | * @access public 447 | */ 448 | public function stderr($msg) 449 | { 450 | echo "STDERR >> $msg\n"; 451 | } 452 | 453 | // }}} 454 | } 455 | 456 | // }}} 457 | // CustomMessageProvider() {{{ 458 | 459 | /** 460 | * Some custom message provider for tests purposes. 461 | * 462 | * @category Console 463 | * @package Console_CommandLine 464 | * @author David JEAN LOUIS 465 | * @copyright 2007 David JEAN LOUIS 466 | * @license http://opensource.org/licenses/mit-license.php MIT License 467 | * @version Release: @package_version@ 468 | * @link http://pear.php.net/package/Console_CommandLine 469 | * @since File available since release 0.1.0 470 | */ 471 | class CustomMessageProvider implements Console_CommandLine_MessageProvider 472 | { 473 | // get() {{{ 474 | 475 | /** 476 | * Retrieve the given string identifier corresponding message. 477 | * 478 | * @param string $code the string identifier of the message 479 | * @param array $vars an array of template variables 480 | * 481 | * @return string 482 | * @access public 483 | */ 484 | public function get($code, $vars = array()) 485 | { 486 | return $code; 487 | } 488 | 489 | // }}} 490 | } 491 | 492 | // }}} 493 | 494 | ?> 495 | --------------------------------------------------------------------------------