Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.2.0

Request #16370 Allow pre-processing of arguments for lists.
Submitted: 2009-06-23 14:42 UTC Modified: 2009-11-11 17:55 UTC
From: rquadling Assigned: izi
Status: Closed Package: Console_CommandLine (version 1.1.0)
PHP Version: 5_3 CVS-2009-06-23 OS: Windows XP SP3
Roadmaps: (Not assigned)    
Subscription  



Patch allow-formatting-of-choice Revisions
Revision 2009-06-23 14:43 UTC
Developer rquadling
 
Download patch

This patch is obsolete

Obsoleted by patches:

Index: CommandLine.php
===================================================================
RCS file: /repository/pear/Console_CommandLine/Console/CommandLine.php,v
retrieving revision 1.5
diff -u -r1.5 CommandLine.php
--- CommandLine.php	19 Jun 2009 10:22:48 -0000	1.5
+++ CommandLine.php	23 Jun 2009 13:41:41 -0000
@@ -212,18 +212,20 @@
      * @var array $actions List of valid actions
      */
     public static $actions = array(
-        'StoreTrue'   => array('Console_CommandLine_Action_StoreTrue', true),
-        'StoreFalse'  => array('Console_CommandLine_Action_StoreFalse', true),
-        'StoreString' => array('Console_CommandLine_Action_StoreString', true),
-        'StoreInt'    => array('Console_CommandLine_Action_StoreInt', true),
-        'StoreFloat'  => array('Console_CommandLine_Action_StoreFloat', true),
-        'StoreArray'  => array('Console_CommandLine_Action_StoreArray', true),
-        'Callback'    => array('Console_CommandLine_Action_Callback', true),
-        'Counter'     => array('Console_CommandLine_Action_Counter', true),
-        'Help'        => array('Console_CommandLine_Action_Help', true),
-        'Version'     => array('Console_CommandLine_Action_Version', true),
-        'Password'    => array('Console_CommandLine_Action_Password', true),
-        'List'        => array('Console_CommandLine_Action_List', true),
+        'StoreTrue'                => array('Console_CommandLine_Action_StoreTrue', true),
+        'StoreFalse'               => array('Console_CommandLine_Action_StoreFalse', true),
+        'StoreString'              => array('Console_CommandLine_Action_StoreString', true),
+        'StoreInt'                 => array('Console_CommandLine_Action_StoreInt', true),
+        'StoreFloat'               => array('Console_CommandLine_Action_StoreFloat', true),
+        'StoreArray'               => array('Console_CommandLine_Action_StoreArray', true),
+        'StoreArrayUnique'         => array('Console_CommandLine_Action_StoreArrayUnique', true),
+        'StoreArrayUniqueCaseless' => array('Console_CommandLine_Action_StoreArrayUniqueCaseless', true),
+        'Callback'                 => array('Console_CommandLine_Action_Callback', true),
+        'Counter'                  => array('Console_CommandLine_Action_Counter', true),
+        'Help'                     => array('Console_CommandLine_Action_Help', true),
+        'Version'                  => array('Console_CommandLine_Action_Version', true),
+        'Password'                 => array('Console_CommandLine_Action_Password', true),
+        'List'                     => array('Console_CommandLine_Action_List', true),
     );
 
     /**
Index: CommandLine/Action.php
===================================================================
RCS file: /repository/pear/Console_CommandLine/Console/CommandLine/Action.php,v
retrieving revision 1.1
diff -u -r1.1 Action.php
--- CommandLine/Action.php	27 Dec 2008 10:52:27 -0000	1.1
+++ CommandLine/Action.php	23 Jun 2009 13:34:40 -0000
@@ -79,6 +79,19 @@
     }
 
     // }}}
+    // format() {{{
+
+    /**
+     * Allow a value to be pre-formatted prior to being used in a choices test.
+     *
+     * @return mixed The supplied value
+     */
+    public function format($value)
+    {
+        return $value;
+    }
+
+    // }}}
     // getResult() {{{
 
     /**
Index: CommandLine/Option.php
===================================================================
RCS file: /repository/pear/Console_CommandLine/Console/CommandLine/Option.php,v
retrieving revision 1.4
diff -u -r1.4 Option.php
--- CommandLine/Option.php	19 Jun 2009 10:22:48 -0000	1.4
+++ CommandLine/Option.php	23 Jun 2009 13:37:47 -0000
@@ -240,8 +240,19 @@
      */
     public function dispatchAction($value, $result, $parser)
     {
+        $actionInfo = Console_CommandLine::$actions[$this->action];
+        if (true === $actionInfo[1]) {
+            // we have a "builtin" action
+            $tokens = explode('_', $actionInfo[0]);
+            include_once implode('/', $tokens) . '.php';
+        }
+        $clsname = $actionInfo[0];
+        if ($this->_action_instance === null) {
+            $this->_action_instance  = new $clsname($result, $this, $parser);
+        }
+
         // check value is in option choices
-        if (!empty($this->choices) && !in_array($value, $this->choices)) {
+        if (!empty($this->choices) && !in_array($this->_action_instance->format($value), $this->choices)) {
             throw Console_CommandLine_Exception::factory(
                 'OPTION_VALUE_NOT_VALID',
                 array(
@@ -253,16 +264,6 @@
                 $this->messages
             );
         }
-        $actionInfo = Console_CommandLine::$actions[$this->action];
-        if (true === $actionInfo[1]) {
-            // we have a "builtin" action
-            $tokens = explode('_', $actionInfo[0]);
-            include_once implode('/', $tokens) . '.php';
-        }
-        $clsname = $actionInfo[0];
-        if ($this->_action_instance === null) {
-            $this->_action_instance  = new $clsname($result, $this, $parser);
-        }
         $this->_action_instance->execute($value, $this->action_params);
     }
 
--- StoreArrayUnique.php
+++ StoreArrayUnique.php
@@ -0,0 +1,11 @@
+<?php
+require_once 'Console/CommandLine/Action/StoreArray.php';
+
+class Console_CommandLine_Action_StoreArrayUnique extends Console_CommandLine_Action_StoreArray
+{
+    public function execute($value = false, $params = array())
+    {
+        parent::execute($value, $params);
+        $this->setResult(array_unique($this->getResult()));
+    }
+}

--- StoreArrayUniqueCaseless.php
+++ StoreArrayUniqueCaseless.php
@@ -0,0 +1,15 @@
+<?php
+require_once 'StoreArrayUnique.php';
+
+class Console_CommandLine_Action_StoreArrayUniqueCaseless extends Console_CommandLine_Action_StoreArrayUnique
+{
+    public function format($value)
+    {
+        return strtoupper($value);
+    }
+
+    public function execute($value = false, $params = array())
+    {
+        parent::execute($this->format($value), $params);
+    }
+}