From: Gunnar Wrobel
Date: Thu, 8 Jul 2010 11:40:12 +0000 (+0200)
Subject: Extract the command line parsing.
X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=5cf512d63fa504fe8d63edd83e243f42934953d0;p=horde.git
Extract the command line parsing.
---
diff --git a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Base.php b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Base.php
index 55434f152..d7240f2fa 100644
--- a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Base.php
+++ b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Base.php
@@ -74,6 +74,13 @@ class Horde_Kolab_Filter_Base
var $_sasl_username;
/**
+ * Comman line parser.
+ *
+ * @param Horde_Kolab_Filter_Cli
+ */
+ private $_cli;
+
+ /**
* The log backend that needs to implement the debug(), info() and err()
* methods.
*
@@ -87,8 +94,10 @@ class Horde_Kolab_Filter_Base
* @param mixed $logger The logger.
*/
public function __construct(
+ Horde_Kolab_Filter_Cli $cli,
$logger
) {
+ $this->_cli = $cli;
$this->_logger = $logger;
}
@@ -187,57 +196,12 @@ class Horde_Kolab_Filter_Base
{
global $conf;
- /* Get command line options. */
- $p = new Horde_Kolab_Filter_Argv_Parser(
- array('optionList' =>
- array(
- new Horde_Argv_Option('-s',
- '--sender',
- array('help' => 'The message sender.',
- 'type' => 'string',
- 'nargs' => 1)),
- new Horde_Argv_Option('-r',
- '--recipient',
- array('help' => 'A message recipient.',
- 'action' => 'append',
- 'type' => 'string')),
- new Horde_Argv_Option('-H',
- '--host',
- array('help' => 'The host running this script.')),
- new Horde_Argv_Option('-c',
- '--client',
- array('help' => 'The client sending the message.')),
- new Horde_Argv_Option('-u',
- '--user',
- array('help' => 'ID of the currently authenticated user.',
- 'default' => '')),
- new Horde_Argv_Option('-C',
- '--config',
- array('help' => 'Path to the configuration file for this filter.'))
- )));
-
- try {
- list($values, $args) = $p->parseArgs();
- } catch (InvalidArgumentException $e) {
- $msg = $e->getMessage() . "\n\n" . $p->getUsage();
- return PEAR::raiseError($msg, OUT_STDOUT | EX_USAGE);
- }
+ $values = $this->_cli->getOptions();
if (!empty($values['config']) && file_exists($values['config'])) {
require_once $values['config'];
}
- if (empty($values['recipient'])) {
- throw new Horde_Kolab_Filter_Exception(
- sprintf(
- "Please provide one or more recipients.\n\n%s",
- $p->getUsage()
- ),
- Horde_Kolab_Filter_Exception::OUT_STDOUT |
- Horde_Kolab_Filter_Exception::EX_USAGE
- );
- }
-
$this->_sender = strtolower($values['sender']);
$this->_recipients = array_map('strtolower', $values['recipient']);
$this->_client_address = $values['client'];
@@ -298,15 +262,3 @@ class Horde_Kolab_Filter_Base
}
}
-class Horde_Kolab_Filter_Argv_Parser extends Horde_Argv_Parser
-{
- public function parserError($msg)
- {
- throw new InvalidArgumentException(sprintf("%s: error: %s\n", $this->getProgName(), $msg));
- }
-
- public function parserExit($status = 0, $msg = null)
- {
- throw new InvalidArgumentException(sprintf("%s: error: %s\n", $this->getProgName(), $msg));
- }
-}
diff --git a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli.php b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli.php
new file mode 100644
index 000000000..70cc29182
--- /dev/null
+++ b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli.php
@@ -0,0 +1,149 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * The command line handling for the Kolab_Filter package.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you did not
+ * receive this file, see
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Cli
+{
+ /**
+ * The CLI argument parser.
+ *
+ * @var Horde_Argv_Parser
+ */
+ private $_parser;
+
+ /**
+ * The CLI options.
+ *
+ * @var Horde_Argv_Values
+ */
+ private $_options;
+
+ /**
+ * The CLI arguments.
+ *
+ * @var array
+ */
+ private $_arguments;
+
+ /**
+ * Constructor.
+ */
+ public function __construct()
+ {
+ $this->_parser = new Horde_Kolab_Filter_Cli_Parser(
+ array('optionList' =>
+ array(
+ new Horde_Argv_Option(
+ '-s',
+ '--sender',
+ array(
+ 'help' => 'The message sender.',
+ 'type' => 'string',
+ 'nargs' => 1
+ )
+ ),
+ new Horde_Argv_Option(
+ '-r',
+ '--recipient',
+ array(
+ 'help' => 'A message recipient.',
+ 'action' => 'append',
+ 'type' => 'string'
+ )
+ ),
+ new Horde_Argv_Option(
+ '-H',
+ '--host',
+ array(
+ 'help' => 'The host running this script.'
+ )
+ ),
+ new Horde_Argv_Option(
+ '-c',
+ '--client',
+ array(
+ 'help' => 'The client sending the message.'
+ )
+ ),
+ new Horde_Argv_Option(
+ '-u',
+ '--user',
+ array(
+ 'help' => 'ID of the currently authenticated user.',
+ 'default' => ''
+ )
+ ),
+ new Horde_Argv_Option(
+ '-C',
+ '--config',
+ array(
+ 'help' => 'Path to the configuration file for this filter.'
+ )
+ )
+ )
+ )
+ );
+ }
+
+ /**
+ * 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()
+ )
+ );
+ }
+ }
+
+ /**
+ * Return the command line options.
+ *
+ * @return Horde_Argv_Values The command line values.
+ */
+ public function getOptions()
+ {
+ if ($this->_options === null) {
+ $this->parse();
+ }
+ return $this->_options;
+ }
+}
\ No newline at end of file
diff --git a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli/Parser.php b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli/Parser.php
new file mode 100644
index 000000000..8c1c1ed4d
--- /dev/null
+++ b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli/Parser.php
@@ -0,0 +1,42 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * A parser variant that does not automatically exit on a parser error.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you did not
+ * receive this file, see
+ * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Cli_Parser extends Horde_Argv_Parser
+{
+ public function parserError($msg)
+ {
+ return $this->parserExit(0, $msg);
+ }
+
+ public function parserExit($status = 0, $msg = null)
+ {
+ throw new InvalidArgumentException(
+ sprintf("%s: error: %s\n", $this->getProgName(), $msg)
+ );
+ }
+}
diff --git a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception.php b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception.php
index 6f01eb1b0..7aca1a0eb 100644
--- a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception.php
+++ b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception.php
@@ -26,7 +26,7 @@
* @link http://pear.horde.org/index.php?package=Kolab_Filter
*/
class Horde_Kolab_Filter_Exception
-extends Exception
+extends Horde_Exception
{
/**
* Failure constants from postfix src/global/sys_exits.h
diff --git a/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception/Usage.php b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception/Usage.php
new file mode 100644
index 000000000..b86af8c14
--- /dev/null
+++ b/framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception/Usage.php
@@ -0,0 +1,48 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * This class provides an error thrown when the user supplied invalid command
+ * line parameters.
+ *
+ * Copyright 2010 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @author Gunnar Wrobel
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Exception_Usage
+extends Horde_Kolab_Filter_Exception
+{
+ /**
+ * Construct the exception
+ *
+ * @param string $msg
+ * @param Exception $previous
+ */
+ public function __construct($msg = '', Exception $previous = null)
+ {
+ parent::__construct(
+ $msg,
+ Horde_Kolab_Filter_Exception::OUT_STDOUT |
+ Horde_Kolab_Filter_Exception::EX_USAGE,
+ $previous
+ );
+ }
+}
diff --git a/framework/Kolab_Filter/package.xml b/framework/Kolab_Filter/package.xml
index 39d1a980e..1d192a01e 100644
--- a/framework/Kolab_Filter/package.xml
+++ b/framework/Kolab_Filter/package.xml
@@ -74,9 +74,16 @@ http://pear.php.net/dtd/package-2.0.xsd">
+
+
+
+
+
+
+
@@ -192,12 +199,11 @@ http://pear.php.net/dtd/package-2.0.xsd">
1.4.0b1
- Horde
+ Core
pear.horde.org
- 0.0.2
- Icalendar
+ Horde_iCalendar
pear.horde.org
0.0.3
@@ -206,6 +212,10 @@ http://pear.php.net/dtd/package-2.0.xsd">
pear.horde.org
+ Exception
+ pear.horde.org
+
+
Horde_MIME
pear.horde.org
0.0.2
@@ -242,10 +252,13 @@ http://pear.php.net/dtd/package-2.0.xsd">
-
+
+
+
+
diff --git a/framework/Kolab_Filter/test/Horde/Kolab/Filter/FilterTest.php b/framework/Kolab_Filter/test/Horde/Kolab/Filter/FilterTest.php
deleted file mode 100644
index a033ab4d0..000000000
--- a/framework/Kolab_Filter/test/Horde/Kolab/Filter/FilterTest.php
+++ /dev/null
@@ -1,71 +0,0 @@
-
- * @license http://www.fsf.org/copyleft/lgpl.html LGPL
- * @link http://pear.horde.org/index.php?package=Kolab_Filter
- */
-
-/**
- * Prepare the test setup.
- */
-require_once dirname(__FILE__) . '/Autoload.php';
-
-/**
- * Test the filter class.
- *
- * Copyright 2008 Klarälvdalens Datakonsult AB
- *
- * See the enclosed file COPYING for license information (LGPL). If you
- * did not receive this file, see http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
- *
- * @category Kolab
- * @package Kolab_Filter
- * @subpackage UnitTests
- * @author Gunnar Wrobel
- * @license http://www.fsf.org/copyleft/lgpl.html LGPL
- * @link http://pear.horde.org/index.php?package=Kolab_Filter
- */
-class Horde_Kolab_Filter_FilterTest extends PHPUnit_Framework_TestCase
-{
-
- /**
- * Set up testing.
- */
- protected function setUp()
- {
- $GLOBALS['conf']['log']['enabled'] = false;
-
- $_SERVER['SERVER_NAME'] = 'localhost';
- $_SERVER['REMOTE_ADDR'] = 'ADDR';
- $_SERVER['REMOTE_HOST'] = 'HOST';
- }
-
-
- /**
- * Test incorrect usage of the Filter
- */
- public function testIncorrectUsage()
- {
- $_SERVER['argv'] = array($_SERVER['argv'][0]);
- $parser = new Horde_Kolab_Filter_Incoming(
- new Horde_Log_Logger(
- new Horde_Log_Handler_Mock()
- )
- );
- $inh = fopen(dirname(__FILE__) . '/fixtures/tiny.eml', 'r');
- try {
- $result = $parser->parse($inh, 'echo');
- } catch (Horde_Kolab_Filter_Exception $e) {
- $this->assertContains('Please provide one or more recipients.', $e->getMessage());
- return;
- }
- $this->assertFail('No exception!');
- }
-}
diff --git a/framework/Kolab_Filter/test/Horde/Kolab/Filter/Integration/CliTest.php b/framework/Kolab_Filter/test/Horde/Kolab/Filter/Integration/CliTest.php
new file mode 100644
index 000000000..72abeb608
--- /dev/null
+++ b/framework/Kolab_Filter/test/Horde/Kolab/Filter/Integration/CliTest.php
@@ -0,0 +1,103 @@
+
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+
+/**
+ * Prepare the test setup.
+ */
+require_once dirname(__FILE__) . '/../Autoload.php';
+
+/**
+ * Test the CLI handling.
+ *
+ * Copyright 2008 Klarälvdalens Datakonsult AB
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+ *
+ * @category Kolab
+ * @package Kolab_Filter
+ * @subpackage UnitTests
+ * @author Gunnar Wrobel
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @link http://pear.horde.org/index.php?package=Kolab_Filter
+ */
+class Horde_Kolab_Filter_Integration_CliTest extends PHPUnit_Framework_TestCase
+{
+
+ /**
+ * Set up testing.
+ */
+ protected function setUp()
+ {
+ $GLOBALS['conf']['log']['enabled'] = false;
+
+ $_SERVER['SERVER_NAME'] = 'localhost';
+ $_SERVER['REMOTE_ADDR'] = 'ADDR';
+ $_SERVER['REMOTE_HOST'] = 'HOST';
+ }
+
+
+ /**
+ * Test incorrect usage of the Filter.
+ */
+ public function testIncorrectUsage()
+ {
+ $_SERVER['argv'] = array($_SERVER['argv'][0]);
+ $parser = new Horde_Kolab_Filter_Incoming(
+ new Horde_Kolab_Filter_Cli(),
+ new Horde_Log_Logger(
+ new Horde_Log_Handler_Mock()
+ )
+ );
+ $inh = fopen(dirname(__FILE__) . '/../fixtures/tiny.eml', 'r');
+ try {
+ $result = $parser->parse($inh, 'echo');
+ } catch (Horde_Kolab_Filter_Exception $e) {
+ $this->assertContains(
+ 'Please provide one or more recipients.',
+ $e->getMessage()
+ );
+ return;
+ }
+ $this->assertFail('No exception!');
+ }
+
+ /**
+ * Test incorrect usage of the Filter by providing an invalid option.
+ */
+ public function testIncorrectUsageWithInvalidOption()
+ {
+ $_SERVER['argv'] = array(
+ $_SERVER['argv'][0],
+ '--recipient'
+ );
+ $parser = new Horde_Kolab_Filter_Incoming(
+ new Horde_Kolab_Filter_Cli(),
+ new Horde_Log_Logger(
+ new Horde_Log_Handler_Mock()
+ )
+ );
+ $inh = fopen(dirname(__FILE__) . '/../fixtures/tiny.eml', 'r');
+ try {
+ $result = $parser->parse($inh, 'echo');
+ } catch (Horde_Kolab_Filter_Exception $e) {
+ $this->assertContains(
+ 'error: --recipient option requires an argument',
+ $e->getMessage()
+ );
+ return;
+ }
+ $this->assertFail('No exception!');
+ }
+}