OSDN Git Service

make synopsis parser return unknown tokens
authorscribu <mail@scribu.net>
Mon, 11 Feb 2013 20:17:43 +0000 (22:17 +0200)
committerscribu <mail@scribu.net>
Mon, 11 Feb 2013 20:18:31 +0000 (22:18 +0200)
php/WP_CLI/SynopsisParser.php
tests/test-synopsis.php

index bc594df..94cb054 100644 (file)
@@ -14,6 +14,8 @@ class SynopsisParser {
                $tokens = preg_split( '/[\s\t]+/', $synopsis );
 
                foreach ( $tokens as $token ) {
+                       $type = false;
+
                        foreach ( $patterns as $regex => $desc ) {
                                if ( preg_match( $regex, $token, $matches ) ) {
                                        $type = $desc['type'];
@@ -21,6 +23,10 @@ class SynopsisParser {
                                        break;
                                }
                        }
+
+                       if ( !$type ) {
+                               $params['unknown'][] = $token;
+                       }
                }
 
                return $params;
index 624cc45..a92c7c3 100644 (file)
@@ -13,11 +13,12 @@ class SynopsisParserTest extends PHPUnit_Framework_TestCase {
        }
 
        function testFlag() {
-               $r = SynopsisParser::parse( '--foo' );
-               $this->assertFoundParameters( 0, 'flag', $r ); // flags can't be mandatory
-
                $r = SynopsisParser::parse( '[--foo]' );
                $this->assertFoundParameters( 1, 'flag', $r );
+
+               // flags can't be mandatory
+               $r = SynopsisParser::parse( '--foo' );
+               $this->assertFoundParameters( 1, 'unknown', $r );
        }
 
        function testGeneric() {
@@ -34,6 +35,10 @@ class SynopsisParserTest extends PHPUnit_Framework_TestCase {
                $this->assertFoundParameters( 2, 'assoc', $r );
                $this->assertFalse( $r['assoc'][0]['optional'] );
                $this->assertTrue( $r['assoc'][1]['optional'] );
+
+               // shouldn't pass defaults to assoc parameters
+               $r = SynopsisParser::parse( '--count=100' );
+               $this->assertFoundParameters( 1, 'unknown', $r );
        }
 
        function testCombined() {