From f0d3d349d6faa9c62263bcea33e15a03248ec0c2 Mon Sep 17 00:00:00 2001 From: scribu Date: Mon, 11 Feb 2013 22:17:43 +0200 Subject: [PATCH] make synopsis parser return unknown tokens --- php/WP_CLI/SynopsisParser.php | 6 ++++++ tests/test-synopsis.php | 11 ++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/php/WP_CLI/SynopsisParser.php b/php/WP_CLI/SynopsisParser.php index bc594df3..94cb0544 100644 --- a/php/WP_CLI/SynopsisParser.php +++ b/php/WP_CLI/SynopsisParser.php @@ -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; diff --git a/tests/test-synopsis.php b/tests/test-synopsis.php index 624cc45c..a92c7c35 100644 --- a/tests/test-synopsis.php +++ b/tests/test-synopsis.php @@ -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() { -- 2.11.0