/** * Constructor. * * @param array $params An optional array of parameters * * @return void */ public function __construct($params = array()) { if (isset($params['aliases'])) { $this->aliases = $params['aliases']; } parent::__construct($params); }
/** * Validates the argument instance. * * @return void * @throws PEAR2\Console\CommandLine_Exception * @todo use exceptions */ public function validate() { // check if the argument name is valid if (!preg_match('/^[a-zA-Z_\\x7f-\\xff]+[a-zA-Z0-9_\\x7f-\\xff]*$/', $this->name)) { \PEAR2\Console\CommandLine::triggerError('argument_bad_name', E_USER_ERROR, array('{$name}' => $this->name)); } if (!$this->optional && $this->default !== null) { Console_CommandLine::triggerError('argument_no_default', E_USER_ERROR); } parent::validate(); }
/** * Validates the option instance. * * @return void * @throws PEAR2\Console\CommandLine_Exception * @todo use exceptions instead */ public function validate() { // check if the option name is valid if (!preg_match('/^[a-zA-Z_\x7f-\xff]+[a-zA-Z0-9_\x7f-\xff]*$/', $this->name)) { Console\CommandLine::triggerError('option_bad_name', E_USER_ERROR, array('{$name}' => $this->name)); } // call the parent validate method parent::validate(); // a short_name or a long_name must be provided if ($this->short_name == null && $this->long_name == null) { Console\CommandLine::triggerError('option_long_and_short_name_missing', E_USER_ERROR, array('{$name}' => $this->name)); } // check if the option short_name is valid if ($this->short_name != null && !(preg_match('/^\-[a-zA-Z]{1}$/', $this->short_name))) { Console\CommandLine::triggerError('option_bad_short_name', E_USER_ERROR, array( '{$name}' => $this->name, '{$short_name}' => $this->short_name )); } // check if the option long_name is valid if ($this->long_name != null && !preg_match('/^\-\-[a-zA-Z]+[a-zA-Z0-9_\-]*$/', $this->long_name)) { Console\CommandLine::triggerError('option_bad_long_name', E_USER_ERROR, array( '{$name}' => $this->name, '{$long_name}' => $this->long_name )); } // check if we have a valid action if (!is_string($this->action)) { Console\CommandLine::triggerError('option_bad_action', E_USER_ERROR, array('{$name}' => $this->name)); } if (!isset(Console\CommandLine::$actions[$this->action])) { Console\CommandLine::triggerError('option_unregistered_action', E_USER_ERROR, array( '{$action}' => $this->action, '{$name}' => $this->name )); } // if the action is a callback, check that we have a valid callback if ($this->action == 'Callback' && !is_callable($this->callback)) { Console\CommandLine::triggerError('option_invalid_callback', E_USER_ERROR, array('{$name}' => $this->name)); } }
} if (false === $dataDir) { fwrite(STDERR, 'Unable to find data dir.'); exit(11); } $consoleDefFile = is_file($dataDir . '/roscon.xml') ? $dataDir . '/roscon.xml' : false; if (false === $consoleDefFile) { fwrite(STDERR, <<<HEREDOC The console definition file (roscon.xml) was not found at the data dir, which was found to be at {$dataDir} HEREDOC ); exit(12); } $cmdParser = CommandLine::fromXmlFile($consoleDefFile); try { $cmd = $cmdParser->parse(); } catch (CommandLine\Exception $e) { fwrite(STDERR, 'Error when parsing command line: ' . $e->getMessage() . "\n"); $cmdParser->displayUsage(13); } $comTimeout = null === $cmd->options['conTime'] ? null === $cmd->options['time'] ? (int) ini_get('default_socket_timeout') : $cmd->options['time'] : $cmd->options['conTime']; $cmd->options['time'] = $cmd->options['time'] ?: 3; $comContext = null === $cmd->options['caPath'] ? null : stream_context_create(is_file($cmd->options['caPath']) ? array('ssl' => array('verify_peer' => true, 'cafile' => $cmd->options['caPath'])) : array('ssl' => array('verify_peer' => true, 'capath' => $cmd->options['caPath']))); $cColors = array('SEND' => '', 'SENT' => '', 'RECV' => '', 'ERR' => '', 'NOTE' => '', '' => ''); if ('auto' === $cmd->options['isColored']) { $cmd->options['isColored'] = (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN' || getenv('ANSICON_VER') != false) && class_exists('PEAR2\\Console\\Color', true) ? 'yes' : 'no'; } if ('yes' === $cmd->options['isColored']) { if (class_exists('PEAR2\\Console\\Color', true)) {
/** * Validates the xml definition using Relax NG. * * @param DomDocument $doc The document to validate * * @return boolean Whether the xml data is valid or not. * @throws PEAR2\Console\CommandLine\Exception * @todo use exceptions only */ public static function validate($doc) { $rngfile = __DIR__ . '/../../../../data/pear2.php.net/PEAR2_Console_CommandLine/xmlschema.rng'; if (!is_file($rngfile)) { $rngfile = __DIR__ . '/../../../../data/xmlschema.rng'; } if (!is_readable($rngfile)) { CommandLine::triggerError('invalid_xml_file', E_USER_ERROR, array('{$file}' => $rngfile)); } return $doc->relaxNGValidate($rngfile); }
/** * Validates the xml definition using Relax NG. * * @param DOMDocument $doc The document to validate * * @return boolean Whether the xml data is valid or not. * @throws PEAR2\Console\CommandLine\Exception * * @todo use exceptions only */ public static function validate(DOMDocument $doc) { $paths = array(); if (!class_exists('Phar', false) || !Phar::running()) { // Pyrus $paths[] = '@data_dir@/pear2.php.net/PEAR2_Console_CommandLine/xmlschema.rng'; // PEAR $pearDataDirEnv = getenv('PHP_PEAR_DATA_DIR'); if ($pearDataDirEnv) { $paths[] = $pearDataDirEnv . '/PEAR2_Console_CommandLine/xmlschema.rng'; } $paths[] = '@data_dir@/PEAR2_Console_CommandLine/xmlschema.rng'; } $pkgData = __DIR__ . '/../../../../data/'; // PHAR dep $paths[] = $pkgData . 'pear2.php.net/PEAR2_Console_CommandLine/xmlschema.rng'; $paths[] = $pkgData . 'PEAR2_Console_CommandLine/xmlschema.rng'; $paths[] = $pkgData . 'pear2/console_commandline/xmlschema.rng'; // Git/Composer $paths[] = $pkgData . 'xmlschema.rng'; $paths[] = 'xmlschema.rng'; foreach ($paths as $path) { if (is_readable($path)) { return $doc->relaxNGValidate($path); } } CommandLine::triggerError('invalid_xml_file', E_USER_ERROR, array('{$file}' => $path)); }
<?php require_once __DIR__ . '/autoload.php'; use Tools\LogCLI; use Tools\Errors; use HypoConf\Commands; use HypoConf\Paths; use PEAR2\Console\CommandLine; //use PEAR2\Console\Color; set_error_handler('\\Tools\\Errors::Handle'); Paths::$root = __DIR__; Paths::$db = __DIR__ . '/database'; // create the parser $parser = new CommandLine(array('name' => 'HypoConf', 'description' => 'A configuration manager for nginx, PHP with PHP-FPM and MySQL with a command line interface', 'version' => '0.4 alpha', 'add_help_option' => FALSE, 'add_version_option' => TRUE)); // add a global option to make the program verbose $parser->addOption('verbose', array('short_name' => '-v', 'long_name' => '--verbose', 'action' => 'StoreInt', 'default' => 1, 'description' => 'set verbose level output (-1 quiet, 5 debug level)')); $parser->addOption('stdout', array('short_name' => '-s', 'long_name' => '--stdout', 'action' => 'StoreTrue', 'default' => false, 'description' => 'turn on output to console instead of writing files')); $parser->addOption('debug', array('short_name' => '-d', 'long_name' => '--debug', 'action' => 'StoreTrue', 'default' => false, 'description' => 'turn on debugging')); $parser->addOption('force', array('short_name' => '-f', 'long_name' => '--force', 'action' => 'StoreTrue', 'default' => false, 'description' => 'force a specific action without asking for confirmation')); $cmd = array(); // add the foo subcommand $cmd['enable'] = $parser->addCommand('enable', array('description' => 'Enables a site', 'aliases' => array('en', 'e'))); $cmd['enable']->addArgument('enable', array()); // add the bar subcommand $cmd['disable'] = $parser->addCommand('disable', array('description' => 'Disables a site', 'aliases' => array('dis', 'd'))); $cmd['disable']->addArgument('disable', array()); $cmd['disable']->addOption('placeholder', array('short_name' => '-p', 'long_name' => '--placeholder', 'help_name' => '/home/overquota.html')); // add the set subcommand $cmd['set'] = $parser->addCommand('set', array('description' => 'Sets the parameter of a website, user or template to specified value(s).', 'aliases' => array('s', 'setting'))); $cmd['set']->addArgument('name', array('description' => 'site, user (when prefixed with @) or template (when prefixed with +)')); $cmd['set']->addArgument('chain', array('description' => 'configuration chain (eg. nginx.php)'));