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

Request #15325 Add ability to detect missing sub-command
Submitted: 2008-12-16 16:57 UTC Modified: 2009-06-17 08:42 UTC
From: gauthierm Assigned: izi
Status: Closed Package: Console_CommandLine (version CVS)
PHP Version: Irrelevant OS: Irrelevant
Roadmaps: (Not assigned)    
Subscription  



Patch invalid-subcommand-detection Revisions
Revision 2009-06-17 05:00 UTC
Developer gauthierm
 
Download patch

Index: Console/CommandLine.php
===================================================================
RCS file: /repository/pear/Console_CommandLine/Console/CommandLine.php,v
retrieving revision 1.3
diff -u -r1.3 CommandLine.php
--- Console/CommandLine.php	12 Jun 2009 15:06:21 -0000	1.3
+++ Console/CommandLine.php	17 Jun 2009 04:01:59 -0000
@@ -810,6 +810,19 @@
                 throw $exc;
             }
         }
+        // Check if an invalid subcommand was specified. If there are
+        // subcommands and no arguments, but an argument was provided, it is
+        // an invalid subcommand.
+        if (   count($this->commands) > 0
+            && count($this->args) === 0
+            && count($args) > 0
+        ) {
+            throw Console_CommandLine_Exception::factory(
+                'INVALID_SUBCOMMAND',
+                array('command' => $args[0]),
+                $this
+            );
+        }
         // minimum argument number check
         $argnum = 0;
         foreach ($this->args as $name=>$arg) {
Index: Console/CommandLine/Exception.php
===================================================================
RCS file: /repository/pear/Console_CommandLine/Console/CommandLine/Exception.php,v
retrieving revision 1.1
diff -u -r1.1 Exception.php
--- Console/CommandLine/Exception.php	27 Dec 2008 10:52:27 -0000	1.1
+++ Console/CommandLine/Exception.php	17 Jun 2009 04:01:59 -0000
@@ -51,6 +51,7 @@
     const OPTION_VALUE_TYPE_ERROR = 3;
     const OPTION_UNKNOWN          = 4;
     const ARGUMENT_REQUIRED       = 5;
+    const INVALID_SUBCOMMAND      = 6;
     /**#@-*/
 
     // }}}
Index: Console/CommandLine/MessageProvider/Default.php
===================================================================
RCS file: /repository/pear/Console_CommandLine/Console/CommandLine/MessageProvider/Default.php,v
retrieving revision 1.1
diff -u -r1.1 Default.php
--- Console/CommandLine/MessageProvider/Default.php	27 Dec 2008 10:52:28 -0000	1.1
+++ Console/CommandLine/MessageProvider/Default.php	17 Jun 2009 04:01:59 -0000
@@ -70,6 +70,7 @@
         'INVALID_CUSTOM_INSTANCE' => 'Instance does not implement the required interface',
         'LIST_OPTION_MESSAGE'     => 'lists valid choices for option {$name}',
         'LIST_DISPLAYED_MESSAGE'  => 'Valid choices are: ',
+        'INVALID_SUBCOMMAND'      => 'Command "{$command}" is not valid.',
     );
 
     // }}}