public inbox for frysk-cvs@sourceware.org
help / color / mirror / Atom feed
* [SCM]  master: Report as an error, an option with a missing argument.
@ 2007-11-08 15:47 cagney
  0 siblings, 0 replies; only message in thread
From: cagney @ 2007-11-08 15:47 UTC (permalink / raw)
  To: frysk-cvs

The branch, master has been updated
       via  67936b53edea993c47c32d70f6190c29aef01cfe (commit)
      from  3b6b68b00544cc4eb9e352de1cdc578083569f9d (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email.

- Log -----------------------------------------------------------------
commit 67936b53edea993c47c32d70f6190c29aef01cfe
Author: Andrew Cagney <cagney@redhat.com>
Date:   Thu Nov 8 10:44:43 2007 -0500

    Report as an error, an option with a missing argument.
    
    frysk-core/frysk/hpd/ChangeLog
    2007-11-08  Andrew Cagney  <cagney@redhat.com>
    
    	* ParameterizedCommand.java
    	(handleOption(Input,String,int,Object)): Fix off-by-one errors.
    	(HelpException): Delete.
    	* TestParameterizedCommand.java (setUp()): Add options.
    	(check(String,String[],boolean,String)):  New.
    	(checkInvalid(String)): New.
    	(testTooManyArgs()): New.
    	(testMissingArg()): New.
    	(testExtraArg()): New.
    	(testMissingOption()): New.

-----------------------------------------------------------------------

Summary of changes:
 frysk-core/frysk/hpd/ChangeLog                     |   11 ++
 frysk-core/frysk/hpd/ParameterizedCommand.java     |   67 +++++-----
 frysk-core/frysk/hpd/TestParameterizedCommand.java |  142 +++++++++----------
 3 files changed, 112 insertions(+), 108 deletions(-)

First 500 lines of diff:
diff --git a/frysk-core/frysk/hpd/ChangeLog b/frysk-core/frysk/hpd/ChangeLog
index d318cee..a1df095 100644
--- a/frysk-core/frysk/hpd/ChangeLog
+++ b/frysk-core/frysk/hpd/ChangeLog
@@ -1,5 +1,16 @@
 2007-11-08  Andrew Cagney  <cagney@redhat.com>
 
+	* ParameterizedCommand.java
+	(handleOption(Input,String,int,Object)): Fix off-by-one error.
+	(HelpException): Delete.
+	* TestParameterizedCommand.java (setUp()): Add options.
+	(check(String,String[],boolean,String)):  New.
+	(checkInvalid(String)): New.
+	(testTooManyArgs()): New.
+	(testMissingArg()): New.
+	(testExtraArg()): New.
+	(testMissingOption()): New.
+
 	* CompletionFactory.java (completeFileName(CLI,String,int,List)): New.
 	(completeExpression(CLI,PTSet,String,int,List)): New.	
 	(completeFileName(CLI,Input,int,List)): Delete.
diff --git a/frysk-core/frysk/hpd/ParameterizedCommand.java b/frysk-core/frysk/hpd/ParameterizedCommand.java
index 3fbf3a2..eb9db6f 100644
--- a/frysk-core/frysk/hpd/ParameterizedCommand.java
+++ b/frysk-core/frysk/hpd/ParameterizedCommand.java
@@ -55,11 +55,6 @@ abstract class ParameterizedCommand extends Command {
 	super(name, description, syntax, full);
 	this.syntax = syntax;
 	this.full = full;
-	add(new CommandOption("help", "print help") {
-		void parse(String argument, Object options) {
-		    throw new HelpException();
-		}
-	    });
     }
     ParameterizedCommand(String name, String syntax, String description) {
 	this(name, description, syntax, description);
@@ -72,13 +67,13 @@ abstract class ParameterizedCommand extends Command {
 	    shortOptions.put("" + option.shortName, option);
     }
 
-    private static class HelpException extends RuntimeException {
-	static final long serialVersionUID = 1;
-    }
-
     private void handleOption(Input input, String option, int index,
 			      Object options) {
+	// Strip any leading "-"'s
 	String name = option.substring(1);
+	while (name.length() > 0 && name.charAt(0) == '-')
+	    name = name.substring(1);
+	// Strip off =... in -option=...
 	int eq = name.indexOf('=');
 	if (eq != -1)
 	    name = option.substring(0, eq);
@@ -91,21 +86,25 @@ abstract class ParameterizedCommand extends Command {
 	}
 	String argument = null;
 	if (commandOption.parameter != null) {
+	    // Require a single parameter.
+	    if ((eq >= 0 && index != input.size())
+		|| (eq == -1 && index != input.size() - 1))
+		throw new InvalidCommandException
+		    ("option -" + commandOption.longName
+		     + " expects a single parameter "
+		     + commandOption.parameter);
 	    if (eq == -1) {
-		if (input.size() < index)
-		    throw new InvalidCommandException
-			("option -" + commandOption.longName
-			 + " expects parameter "
-			 + commandOption.parameter);
 		argument = input.parameter(index);
 		input.remove(index);
 	    } else {
 		argument = option.substring(eq + 1);
 	    }
-	} else if (eq != -1) {
-	    throw new InvalidCommandException("option -"
-					      + commandOption.longName
-					      + " doesn't allow an argument");
+	} else {
+	    // Reject a parameter.
+	    if (eq != -1 || index != input.size())
+		throw new InvalidCommandException
+		    ("option -" + commandOption.longName
+		     + " doesn't allow an argument");
 	}
 	commandOption.parse(argument, options);
     }
@@ -116,23 +115,25 @@ abstract class ParameterizedCommand extends Command {
      */
     public final void interpret(CLI cli, Input input) {
 	Object options = options();
-	try {
-	    for (int currentIndex = input.size() - 1;
-		 currentIndex > -1;
-		 --currentIndex) {
-		String string = input.parameter(currentIndex);
-		if (string.equals("--")) {
-		    input.remove(currentIndex);
-		    break;
-		}
-		if (string.charAt(0) != '-')
-		    continue;
-		handleOption(input, string, currentIndex + 1, options);
+	for (int currentIndex = input.size() - 1; currentIndex > -1;
+	     --currentIndex) {
+	    String string = input.parameter(currentIndex);
+	    if (string.equals("--")) {
+		if (currentIndex != input.size() - 1)
+		    throw new InvalidCommandException
+			("Invalid option "
+			 + input.parameter(currentIndex + 1));
 		input.remove(currentIndex);
+		break;
+	    }
+	    if (string.equals("-help")) {
+		help(cli, input);
+		return;
 	    }
-	} catch (HelpException h) {
-	    help(cli, input);
-	    return;
+	    if (string.charAt(0) != '-')
+		continue;
+	    handleOption(input, string, currentIndex + 1, options);
+	    input.remove(currentIndex);
 	}
 	interpret(cli, input, options);
     }
diff --git a/frysk-core/frysk/hpd/TestParameterizedCommand.java b/frysk-core/frysk/hpd/TestParameterizedCommand.java
index 746154a..00edef9 100644
--- a/frysk-core/frysk/hpd/TestParameterizedCommand.java
+++ b/frysk-core/frysk/hpd/TestParameterizedCommand.java
@@ -69,6 +69,21 @@ public class TestParameterizedCommand extends TestLib {
 		    return -1;
 		}
 	    };
+	command.add(new CommandOption("arg", "long parameterized option",
+				      "ARG") {
+		void parse(String arg, Object options) {
+		    parsedOption = true;
+		    argument = arg;
+		    assertNotNull("-arg's argument", argument);
+		}
+	    });
+	command.add(new CommandOption("opt", "long option") {
+		void parse(String arg, Object options) {
+		    parsedOption = true;
+		    argument = arg;
+		    assertNull("-opt's argument", argument);
+		}
+	    });
     }
     public void tearDown() {
 	command = null;
@@ -82,103 +97,80 @@ public class TestParameterizedCommand extends TestLib {
 	command.interpret(null, input);
     }
 
+    private void check(String string, String[] parameters,
+		       boolean parsedOption, String argument) {
+	parse(string);
+	assertEquals("input size", parameters.length, input.size());
+	assertEquals("parsedOption", this.parsedOption, parsedOption);
+	assertEquals("argument", this.argument, argument);
+	for (int i = 0; i < parameters.length; i++) {
+	    assertEquals("parameter " + i, parameters[i], input.parameter(i));
+	}
+	assertTrue("interpreted", interpreted);
+	assertFalse("helped", helped);
+    }
+
     public void testDashDash() {
-	parse("parser --");
-	assertEquals("Params list should be empty", 0, input.size());
+	check("parser --", new String[0], false, null);
     }
 
     public void testRegular() {
-	parse("parser argument");
-	assertEquals("Param list should have one item", 1, input.size());
-	assertEquals("Argument should be 'argument'", "argument",
-		     input.parameter(0));
+	check("parser argument", new String[] { "argument" }, false, null);
     }
 
     public void testRegularDashDash() {
-	parse("parser argument --");
-	assertEquals("Param list should have one item", 1, input.size());
-	assertEquals("Argument should be 'argument'", "argument",
-		     input.parameter(0));
+	check("parser argument --", new String[] { "argument" }, false, null);
     }
 
     public void testOption() {
-	command.add(new CommandOption("short", "short option") {
-		void parse(String argument, Object options) {
-		    parsedOption = true;
-		}
-	    });
-	parse("parser -short");
-	assertEquals("Params list should be empty", 0, input.size());
-	assertTrue("Option should have been handled", parsedOption);
-	assertTrue("interpreted", interpreted);
-	assertFalse("helped", helped);
+	check("parser -opt", new String[0], true, null);
     }
 
     public void testOptionAfterDashDash() {
-	command.add(new CommandOption("short", "short option") {
-		void parse(String argument, Object options) {
-		    parsedOption = true;
-		}
-	    });
-	parse("parser -- -short");
-	assertEquals("Params list should be empty", 0, input.size());
-	assertTrue("Option should have been handled", parsedOption);
+	check("parser -- -opt", new String[0], true, null);
     }
 
     public void testOptionBeforeDashDash() {
-	command.add(new CommandOption("short", "short option") {
-		void parse(String argument, Object options) {
-		    fail("Should not have parsed option");
-		}
-	    });
-	parse("parser -short --");
-	assertEquals("Param list should have one item", 1, input.size());
-	assertEquals("Argument should be '-short'", "-short",
-		     input.parameter(0));
-	assertFalse("Argument should not have been parsed", parsedOption);
+	check("parser -opt --", new String[] { "-opt" }, false, null);
     }
 
-    public void testOptionWithArgs() {
-	command.add(new CommandOption("short", "short option", "ARG") {
-		void parse(String arg, Object options) {
-		    parsedOption = true;
-		    argument = arg;
-		}
-	    });
-	parse("parser -short argument");
-	assertEquals("Params list should be empty", 0, input.size());
-	assertTrue("Option should have been handled", parsedOption);
-	assertEquals("Option should have argument 'argument'",
-		     "argument", argument);
+    public void testOptionWithArg() {
+	check("parser -arg argument", new String[0], true, "argument");
     }
 
-    public void testOptionWithArgsAfterDashDash() {
-	command.add(new CommandOption("short", "short option", "ARG") {
-		void parse(String arg, Object options) {
-		    parsedOption = true;
-		    argument = arg;
-		}
-	    });
-	parse("parser -- -short argument");
-	assertEquals("Params list should be empty", 0, input.size());
-	assertTrue("Option should have been handled", parsedOption);
-	assertEquals("Option should have argument 'argument'",
-		     "argument", argument);
+    public void testOptionWithArgAfterDashDash() {
+	check("parser -- -arg argument", new String[0], true, "argument");
     }
 
-    public void testOptionWithArgsBeforeDashDash() {
-	command.add(new CommandOption("short", "short option", "ARG") {
-		void parse(String arg, Object options) {
-		    parsedOption = true;
-		    argument = arg;
-		}
-	    });
-	parse("parser -short argument --");
-	assertEquals("Params list should have 2 elements", 2, input.size());
-	assertEquals("First argument: '-short", "-short", input.parameter(0));
-	assertEquals("Second argument: 'argument", "argument",
-		     input.parameter(1));
-	assertFalse("Option should not have been handled", parsedOption);
+    public void testOptionWithArgBeforeDashDash() {
+	check("parser -arg argument --", new String[] { "-arg", "argument" },
+	      false, null);
+    }
+
+    private void checkInvalid(String string) {
+	RuntimeException thrown = null;
+	try {
+	    parse(string);
+	} catch (InvalidCommandException e) {
+	    thrown = e;
+	}
+	assertNotNull("exception thrown", thrown);
+    }
+
+    public void testMissingArg() {
+	checkInvalid("parser -arg");
+    }
+
+    public void testTooManyArgs() {
+	checkInvalid("parser -arg arg1 arg2");
+    }
+
+    public void testExtraArg() {
+	checkInvalid("parser -opt arg");
+    }
+
+    public void testMissingOption() {
+	checkInvalid("parser -- arg");
     }
 
     public void testHelp() {


hooks/post-receive
--
frysk system monitor/debugger


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2007-11-08 15:47 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-08 15:47 [SCM] master: Report as an error, an option with a missing argument cagney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).