public inbox for frysk-cvs@sourceware.org
help / color / mirror / Atom feed
From: cagney@sourceware.org
To: frysk-cvs@sourceware.org
Subject: [SCM]  master: Report as an error, an option with a missing argument.
Date: Thu, 08 Nov 2007 15:47:00 -0000	[thread overview]
Message-ID: <20071108154711.5987.qmail@sourceware.org> (raw)

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


                 reply	other threads:[~2007-11-08 15:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20071108154711.5987.qmail@sourceware.org \
    --to=cagney@sourceware.org \
    --cc=frysk-cvs@sourceware.org \
    --cc=frysk@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).