From 5cf512d63fa504fe8d63edd83e243f42934953d0 Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel
Date: Thu, 8 Jul 2010 13:40:12 +0200
Subject: [PATCH] Extract the command line parsing.
---
.../Kolab_Filter/lib/Horde/Kolab/Filter/Base.php | 68 ++--------
.../Kolab_Filter/lib/Horde/Kolab/Filter/Cli.php | 149 +++++++++++++++++++++
.../lib/Horde/Kolab/Filter/Cli/Parser.php | 42 ++++++
.../lib/Horde/Kolab/Filter/Exception.php | 2 +-
.../lib/Horde/Kolab/Filter/Exception/Usage.php | 48 +++++++
framework/Kolab_Filter/package.xml | 21 ++-
.../{FilterTest.php => Integration/CliTest.php} | 46 ++++++-
7 files changed, 306 insertions(+), 70 deletions(-)
create mode 100644 framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli.php
create mode 100644 framework/Kolab_Filter/lib/Horde/Kolab/Filter/Cli/Parser.php
create mode 100644 framework/Kolab_Filter/lib/Horde/Kolab/Filter/Exception/Usage.php
rename framework/Kolab_Filter/test/Horde/Kolab/Filter/{FilterTest.php => Integration/CliTest.php} (53%)
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