public function testCustomProgName() { $parser = new Horde_Argv_Parser(array('prog' => 'thingy', 'version' => "%prog 0.1", 'usage' => "%prog arg arg")); $parser->removeOption('-h'); $parser->removeOption('--version'); $expectedUsage = "Usage: thingy arg arg\n"; $this->assertUsage($parser, $expectedUsage); $this->assertVersion($parser, "thingy 0.1"); $this->assertHelp($parser, $expectedUsage . "\n"); }
/** * Parse the command line arguments. * * @return NULL */ public function parse() { try { list($this->_options, $this->_arguments) = $this->_parser->parseArgs(); } catch (InvalidArgumentException $e) { throw new Horde_Kolab_Filter_Exception_Usage($e->getMessage() . "\n\n" . $this->_parser->getUsage()); } if (empty($this->_options['recipient'])) { throw new Horde_Kolab_Filter_Exception_Usage(sprintf("Please provide one or more recipients.\n\n%s", $this->_parser->getUsage())); } }
public function testCallbackHelp() { // This test was prompted by SF bug #960515 -- the point is // not to inspect the help text, just to make sure that // formatHelp() doesn't crash. $parser = new Horde_Argv_Parser(array('usage' => Horde_Argv_Option::SUPPRESS_USAGE)); $parser->removeOption('-h'); $parser->addOption('-t', '--test', array('action' => 'callback', 'callback' => array($this, 'returnNull'), 'type' => 'string', 'help' => 'foo')); $expectedHelp = "Options:\n -t TEST, --test=TEST foo\n"; $this->assertHelp($parser, $expectedHelp); }
public function testMatchAbbrevError() { $s = '--f'; $wordmap = array("--foz" => null, "--foo" => null, "--fie" => null); try { Horde_Argv_Parser::matchAbbrev($s, $wordmap); $this->fail(); } catch (Horde_Argv_BadOptionException $e) { $this->assertEquals("ambiguous option: --f (--fie, --foo, --foz?)", $e->getMessage()); } }
* * See the enclosed file COPYING for license information (GPL). If you * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. * * @author Michael Slusarz <*****@*****.**> * @category Horde * @copyright 2013 Horde LLC * @license http://www.fsf.org/copyleft/gpl.html GPL */ require 'vendor/autoload.php'; require 'lib/HordeInstaller.php'; $c = new Horde_Cli(); $c->writeln($c->bold('----------------------')); $c->writeln($c->bold('Horde installer script')); $c->writeln($c->bold('----------------------')); $argv = new Horde_Argv_Parser(); $argv->addOption('-i', '--install-dir', array('action' => 'store', 'dest' => 'horde_dir', 'help' => 'Horde install directory')); $argv->addOption('-l', '--log', array('action' => 'store', 'dest' => 'log', 'help' => 'Log filename')); list($values, ) = $argv->parseArgs(); $version = phpversion(); if (version_compare($version, '5.3.0') === -1) { $c->fatal('You need at least PHP version 5.3.0 to run Horde.'); } $c->writeln(); $c->message('PHP version: ' . $version, 'cli.message'); exec('which pear', $pear_output); if (empty($pear_output)) { $c->fatal('Could not find PEAR script in your include path.'); } $c->message('PEAR location: ' . $pear_output[0], 'cli.message'); while (!strlen($values->horde_dir)) {
/** * The main entry point for the application. * * @param array $parameters A list of named configuration parameters. */ public static function main(array $parameters = array()) { $parser = new Horde_Argv_Parser(array('usage' => '%prog [OPTIONS] [SOURCE://ID]')); $parser->addOptions(array(new Horde_Argv_Option('-c', '--config', array('action' => 'store', 'help' => Horde_Push_Translation::t('Path to the configuration file.'))), new Horde_Argv_Option('-S', '--summary', array('action' => 'store', 'help' => Horde_Push_Translation::t('A summary replacing the value provided by the source.'))), new Horde_Argv_Option('-R', '--recipients', array('action' => 'store', 'help' => Horde_Push_Translation::t('A comma delimited list of recipients.'))), new Horde_Argv_Option('-T', '--tags', array('action' => 'store', 'help' => Horde_Push_Translation::t('A comma delimited list of tags.'))), new Horde_Argv_Option('-L', '--links', array('action' => 'store', 'help' => Horde_Push_Translation::t('A comma delimited list of links.'))), new Horde_Argv_Option('-p', '--pretend', array('action' => 'store_true', 'help' => Horde_Push_Translation::t('Do not push the content but display what would be done.'))))); list($options, $arguments) = $parser->parseArgs(); global $conf; if (isset($options['config'])) { if (!file_exists($options['config'])) { throw new Horde_Push_Exception(sprintf('The specified config file %s does not exist!', $options['config'])); } include $options['config']; } else { $conf = array('recipients' => array('mock')); } if (empty($arguments)) { $arguments = explode(' ', trim(file_get_contents('php://stdin'))); } $push_factory = new Horde_Push_Factory_Push(); $pushes = $push_factory->create($arguments, $options, $conf); $fail = false; foreach ($pushes as $push) { if (isset($options['summary'])) { $push->setSummary($options['summary']); } if (isset($options['tags'])) { foreach (explode(',', $options['tags']) as $tag) { $push->addTag($tag); } } if (isset($options['links'])) { foreach (explode(',', $options['links']) as $reference) { $push->addReference($reference); } } $recipient_factory = new Horde_Push_Factory_Recipients(); $recipients = $recipient_factory->create($options, $conf); foreach ($recipients as $recipient) { $push->addRecipient($recipient); } $results = $push->push(array('pretend' => !empty($options['pretend']))); $cli = Horde_Cli::init(); foreach ($results as $result) { if ($result instanceof Exception) { $cli->message($result->getMessage(), 'cli.error'); $fail = true; } else { $cli->message((string) $result, 'cli.success'); } } } if ($fail === true) { $status = 1; } else { $status = 0; } if (empty($parameters['no_exit'])) { exit($status); } else { return $status; } }
/** * Constructor. * */ public function __construct(Horde_Argv_Parser $parser) { $this->_parser = $parser; $parser->addOption(new Horde_Argv_Option('-c', '--config', array('action' => 'store', 'help' => sprintf('the path to the configuration file for the components script (default : %s).', Components_Constants::getConfigFile()), 'default' => Components_Constants::getConfigFile()))); $parser->addOption(new Horde_Argv_Option('-q', '--quiet', array('action' => 'store_true', 'help' => 'Reduce output to a minimum'))); $parser->addOption(new Horde_Argv_Option('-v', '--verbose', array('action' => 'store_true', 'help' => 'Reduce output to a maximum'))); $parser->addOption(new Horde_Argv_Option('-P', '--pretend', array('action' => 'store_true', 'help' => 'Just pretend and indicate what would be done rather than performing the action.'))); $parser->addOption(new Horde_Argv_Option('-N', '--nocolor', array('action' => 'store_true', 'help' => 'Avoid colors in the output'))); $parser->addOption(new Horde_Argv_Option('-t', '--templatedir', array('action' => 'store', 'help' => 'Location of a template directory that contains template definitions (see the data directory of this package to get an impression of which templates are available).'))); $parser->addOption(new Horde_Argv_Option('-D', '--destination', array('action' => 'store', 'help' => 'Path to an (existing) destination directory where any output files will be placed.'))); $parser->addOption(new Horde_Argv_Option('-R', '--pearrc', array('action' => 'store', 'help' => 'the path to the configuration of the PEAR installation you want to use for all PEAR based actions (leave empty to use your system default PEAR environment).'))); $parser->addOption(new Horde_Argv_Option('--allow-remote', array('action' => 'store_true', 'help' => 'allow horde-components to access the remote http://pear.horde.org for dealing with stable releases. This option is not required in case you work locally in your git checkout and will only work for some actions that are able to operate on stable release packages.'))); $parser->addOption(new Horde_Argv_Option('-G', '--commit', array('action' => 'store_true', 'help' => 'Commit any changes during the selected action to git.'))); $parser->addOption(new Horde_Argv_Option('--horde-root', array('action' => 'store', 'help' => 'The root of the Horde git repository.'))); list($this->_options, $this->_arguments) = $this->_parser->parseArgs(); }
/** */ public function __construct($options = array()) { global $registry, $conf; parent::__construct($options); $options = array(new Horde_Argv_Option('-b', '--base', array('type' => 'string')), new Horde_Argv_Option('-u', '--user', array('type' => 'string')), new Horde_Argv_Option('-p', '--pass', array('type' => 'string'))); $parser = new Horde_Argv_Parser(array('allowUnknownArgs' => true, 'optionList' => $options, 'addHelpOption' => false)); list($this->_argv, $args) = $parser->parseArgs(); if (!count($args)) { throw new Koward_Exception('unknown command: ' . implode(' ', $args)); } /** * FIXME: Workaround to fix the path so that the command line call * really only needs the route. */ $this->_path = $registry->get('webroot', 'koward') . '/' . $args[0]; try { $registry->pushApp('koward', false); } catch (Horde_Exception $e) { if ($e->getCode() == 'permission_denied') { echo 'Perission denied!'; exit; } } $this->_cmd_argv = array(); /* Authenticate the user if possible. */ if ($this->_argv->user) { $auth = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Auth')->create(); if (!$auth->authenticate($this->_argv->user, array('password' => $this->_argv->pass))) { throw new InvalidArgumentException('Failed to log in!'); } } try { $registry->pushApp('koward', empty($this->auth_handler) || $this->auth_handler != $this->params[':action']); } catch (Horde_Exception $e) { if ($e->getCode() == 'permission_denied') { $this->urlFor(array('controller' => 'index', 'action' => 'login'))->redirect(); } } /** * A rough command line handler that allows us to map CLI requests into * the web view of the system. */ switch ($args[0]) { case 'object/add': $this->_cmd_argv['formname'] = 'koward_form_object'; /** Has the object type been set? */ if ($this->_argv->type && is_array($this->_argv->type) && count($this->_argv->type) == 1) { $type = $this->_argv->type[0]; /** * FIXME: Editing on the command line does not work if we don't * specify the full set of form attributes. Yet another reason * for not using the Form. */ if ($this->_argv->id && is_array($this->_argv->id) && count($this->_argv->id) == 1) { $this->_cmd_argv['id'] = $this->_argv->id[0]; } else { $this->_cmd_argv['id'] = $this->_argv->id; } /** * Fake the selected type for the form handler and short circuit the * type selection machinery. */ $this->_cmd_argv['__old_type'] = $type; /** * Fake the form token. Hm, it does not really make much sense * to use the standard form mechanisms via CLI. Think of some * alternatives here. */ $token = $GLOBALS['injector']->getInstance('Horde_Token')->get('cli'); $this->_cmd_argv['koward_form_object_formToken'] = $token; /** * FIXME: Allow retrieving the form fields without specifying $vars. */ $object = null; $form = new Koward_Form_Object(Horde_Variables::getDefaultVariables(), $object); $fields = array_keys($form->getTypeFields($type)); /** * Now that we know the type of object that should be edited we * can restrict the amount of options we allow. */ $options = array(new Horde_Argv_Option('-b', '--base', array('type' => 'string')), new Horde_Argv_Option('-u', '--user', array('type' => 'string')), new Horde_Argv_Option('-p', '--pass', array('type' => 'string')), new Horde_Argv_Option('-t', '--type', array('type' => 'string')), new Horde_Argv_Option('-i', '--id', array('type' => 'string'))); foreach ($fields as $field) { $options[] = new Horde_Argv_Option(null, '--' . $field, array('type' => 'string')); } $parser = new Horde_Argv_Parser(array('allowUnknownArgs' => false, 'optionList' => $options, 'addHelpOption' => true)); list($cmd_argv, $cmd) = $parser->parseArgs(); foreach ($cmd_argv as $field => $value) { if ($field == 'userPassword') { /** * FIXME: Obvious hack and probably another reason why * mixing forms and CLI does not make that much * sense. */ $this->_cmd_argv['object']['userPassword']['original'] = $value; $this->_cmd_argv['object']['userPassword']['confirm'] = $value; } else { if (in_array($field, $fields) && $value !== null) { $this->_cmd_argv['object'][$field] = $value; } } } } break; case 'object/delete': if ($this->_argv->id && is_array($this->_argv->id) && count($this->_argv->id) == 1) { $this->_cmd_argv['id'] = $this->_argv->id[0]; } else { $this->_cmd_argv['id'] = $this->_argv->id; } /** * Provide a token for immediate deletion. */ $this->_cmd_argv['token'] = $GLOBALS['injector']->getInstance('Horde_Token')->get('object.delete'); break; } }
#!/usr/bin/env php <?php /** * The Autoloader allows us to omit "require/include" statements. */ require_once 'Horde/Autoloader/Default.php'; /** * FIXME START * * Simplify the Autoloader configuration so that the user needs no knowledge of * the app root. */ $options = array(new Horde_Argv_Option('-b', '--base', array('type' => 'string'))); $parser = new Horde_Argv_Parser(array('allowUnknownArgs' => true, 'optionList' => $options)); list($opts, $cmd) = $parser->parseArgs(); if (!$opts->base) { throw new InvalidArgumentException('The path to the application base has not been specified!'); } /** * Ensure that the base parameters (especially SERVER_NAME) get set for the * command line. */ $cli = Horde_Cli::init(); /** * Hm, the fact that we need the registry at this point for * identifying the location where we installed the application is not * really satisfying. We need it to know the location of the * configuration though. */ $koward_authentication = 'none'; require_once $opts->base . '/koward/config/base.php';
#!/usr/bin/env php <?php /** * @package Feed */ /* Get a Horde framework include_path set up. */ require 'Horde/Autoloader.php'; $p = new Horde_Argv_Parser(array('usage' => "%prog opml_url\n\nExample:\n%prog subscriptions.opml")); list($values, $args) = $p->parseArgs(); if (count($args) != 1) { $p->printHelp(); exit(1); } $blogroll = Horde_Feed::readFile($args[0]); echo $blogroll->title . "\n\n"; foreach ($blogroll as $blog) { $feed = $blog->getFeed(); echo $feed->title . "\n\n"; foreach ($feed as $entry) { echo "{$entry->title}\n"; } echo "\n\n"; } exit(0);