I'm checking this in.
jar had an error-printing case that was redundant with some code in
getopt. This particular code just prints a message about using
'--help' when the user uses bad command-line options, so it seemed
best to push this into a validate() method on Parser.
This patch also updates jar to clean up the mess.
Other tools ought to use this as appropriate.
Tom
2006-05-14 Tom Tromey <[EMAIL PROTECTED]>
* tools/gnu/classpath/tools/jar/Updater.java (run): No longer throws
OptionException.
* tools/gnu/classpath/tools/jar/Creator.java (run): No longer throws
OptionException.
* tools/gnu/classpath/tools/jar/Action.java (run): No longer throws
OptionException.
* tools/gnu/classpath/tools/jar/Indexer.java (run): Removed. Moved
validation to JarParser.
* tools/gnu/classpath/tools/jar/Main.java (JarParser): New class.
(run): Moved validation to JarParser. Don't throw OptionException.
(initializeParser): Create a JarParser.
(main): Don't catch OptionException.
* tools/gnu/classpath/tools/getopt/Parser.java (printHelp): No longer
public.
(validate): New method.
(parse): Call it. Print '-help' in error message when long-only.
Index: tools/gnu/classpath/tools/getopt/Parser.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/getopt/Parser.java,v
retrieving revision 1.5
diff -u -r1.5 Parser.java
--- tools/gnu/classpath/tools/getopt/Parser.java 10 May 2006 21:00:43
-0000 1.5
+++ tools/gnu/classpath/tools/getopt/Parser.java 15 May 2006 01:52:17
-0000
@@ -250,7 +250,7 @@
this.printHelp(System.out);
}
- protected void printHelp(PrintStream out)
+ void printHelp(PrintStream out)
{
if (headerText != null)
{
@@ -275,6 +275,26 @@
formatText(out, footerText);
}
+ /**
+ * This method can be overridden by subclassses to provide some option
+ * validation. It is called by the parser after all options have been
+ * parsed. If an option validation problem is encountered, this should
+ * throw an [EMAIL PROTECTED] OptionException} whose message should be shown
to
+ * the user.
+ * <p>
+ * It is better to do validation here than after [EMAIL PROTECTED]
#parse(String[])}
+ * returns, because the parser will print a message referring the
+ * user to the <code>--help</code> option.
+ * <p>
+ * The base implementation does nothing.
+ *
+ * @throws OptionException the error encountered
+ */
+ protected void validate() throws OptionException
+ {
+ // Base implementation does nothing.
+ }
+
private String getArgument(String request) throws OptionException
{
++currentIndex;
@@ -380,12 +400,15 @@
// Add remaining arguments to leftovers.
for (++currentIndex; currentIndex < args.length; ++currentIndex)
files.notifyFile(args[currentIndex]);
+ // See if something went wrong.
+ validate();
}
catch (OptionException err)
{
System.err.println(programName + ": " + err.getMessage());
System.err.println(programName + ": Try '" + programName
- + " --help' for more information.");
+ + " " + (longOnly ? "-help" : "--help")
+ + "' for more information.");
System.exit(1);
}
}
Index: tools/gnu/classpath/tools/jar/Action.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/jar/Action.java,v
retrieving revision 1.2
diff -u -r1.2 Action.java
--- tools/gnu/classpath/tools/jar/Action.java 14 May 2006 20:38:32 -0000
1.2
+++ tools/gnu/classpath/tools/jar/Action.java 15 May 2006 01:52:17 -0000
@@ -38,8 +38,6 @@
package gnu.classpath.tools.jar;
-import gnu.classpath.tools.getopt.OptionException;
-
import java.io.IOException;
public abstract class Action
@@ -49,5 +47,5 @@
}
public abstract void run(Main parameters)
- throws IOException, OptionException;
+ throws IOException;
}
Index: tools/gnu/classpath/tools/jar/Creator.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/jar/Creator.java,v
retrieving revision 1.3
diff -u -r1.3 Creator.java
--- tools/gnu/classpath/tools/jar/Creator.java 14 May 2006 20:38:32 -0000
1.3
+++ tools/gnu/classpath/tools/jar/Creator.java 15 May 2006 01:52:17 -0000
@@ -38,8 +38,6 @@
package gnu.classpath.tools.jar;
-import gnu.classpath.tools.getopt.OptionException;
-
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -222,7 +220,7 @@
outputStream.close();
}
- public void run(Main parameters) throws IOException, OptionException
+ public void run(Main parameters) throws IOException
{
if (parameters.archiveFile == null || parameters.archiveFile.equals("-"))
writeCommandLineEntries(parameters, System.out);
Index: tools/gnu/classpath/tools/jar/Indexer.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/jar/Indexer.java,v
retrieving revision 1.2
diff -u -r1.2 Indexer.java
--- tools/gnu/classpath/tools/jar/Indexer.java 14 May 2006 20:44:44 -0000
1.2
+++ tools/gnu/classpath/tools/jar/Indexer.java 15 May 2006 01:52:17 -0000
@@ -38,7 +38,6 @@
package gnu.classpath.tools.jar;
-import gnu.classpath.tools.getopt.OptionException;
import gnu.java.net.IndexListParser;
import java.io.ByteArrayInputStream;
@@ -135,15 +134,4 @@
writeFile(false, in, IndexListParser.JAR_INDEX_FILE,
parameters.verbose);
}
}
-
- public void run(Main parameters) throws IOException, OptionException
- {
- if (! parameters.entries.isEmpty())
- throw new OptionException("can't specify file arguments when using -i");
- if (! parameters.wantManifest)
- throw new OptionException("can't specify -M with -i");
- if (parameters.manifestFile != null)
- throw new OptionException("can't specify -m with -i");
- super.run(parameters);
- }
}
Index: tools/gnu/classpath/tools/jar/Main.java
===================================================================
RCS file: /cvsroot/classpath/classpath/tools/gnu/classpath/tools/jar/Main.java,v
retrieving revision 1.4
diff -u -r1.4 Main.java
--- tools/gnu/classpath/tools/jar/Main.java 14 May 2006 20:44:44 -0000
1.4
+++ tools/gnu/classpath/tools/jar/Main.java 15 May 2006 01:52:17 -0000
@@ -138,9 +138,37 @@
}
}
+ private class JarParser extends ClasspathToolParser
+ {
+ public JarParser(String name)
+ {
+ super(name);
+ }
+
+ protected void validate() throws OptionException
+ {
+ if (operationMode == null)
+ throw new OptionException("must specify one of -t, -c, -u, -x, or -i");
+ if (changedDirectory != null)
+ throw new OptionException("-C argument requires both directory and
filename");
+ if (! wantManifest && manifestFile != null)
+ throw new OptionException("can't specify both -m and -M");
+ if (operationMode == Indexer.class)
+ {
+ // Some extra validation for -i.
+ if (! entries.isEmpty())
+ throw new OptionException("can't specify file arguments when using
-i");
+ if (! wantManifest)
+ throw new OptionException("can't specify -M with -i");
+ if (manifestFile != null)
+ throw new OptionException("can't specify -m with -i");
+ }
+ }
+ }
+
private Parser initializeParser()
{
- Parser p = new ClasspathToolParser("jar");
+ Parser p = new JarParser("jar");
p.setHeader("Usage: jar -ctxui [OPTIONS] jar-file [-C DIR FILE] FILE...");
OptionGroup grp = new OptionGroup("Operation mode");
@@ -205,20 +233,14 @@
return p;
}
- private void run(String[] args) throws OptionException,
- InstantiationException, IllegalAccessException, IOException
+ private void run(String[] args)
+ throws InstantiationException, IllegalAccessException, IOException
{
Parser p = initializeParser();
// Special hack to emulate old tar-style commands.
if (args.length > 0 && args[0].charAt(0) != '-')
args[0] = '-' + args[0];
p.parse(args, new HandleFile());
- if (operationMode == null)
- throw new OptionException("must specify one of -t, -c, -u, -x, or -i");
- if (changedDirectory != null)
- throw new OptionException("-C argument requires both directory and
filename");
- if (! wantManifest && manifestFile != null)
- throw new OptionException("can't specify both -m and -M");
Action t = (Action) operationMode.newInstance();
t.run(this);
}
@@ -230,13 +252,6 @@
{
jarprogram.run(args);
}
- catch (OptionException arg)
- {
- System.err.println("jar: " + arg.getMessage());
- // FIXME: this should be pushed into the parser somehow.
- System.err.println("Try 'jar --help' for more information");
- System.exit(1);
- }
catch (Exception e)
{
System.err.println("jar: internal error:");
Index: tools/gnu/classpath/tools/jar/Updater.java
===================================================================
RCS file:
/cvsroot/classpath/classpath/tools/gnu/classpath/tools/jar/Updater.java,v
retrieving revision 1.3
diff -u -r1.3 Updater.java
--- tools/gnu/classpath/tools/jar/Updater.java 14 May 2006 20:38:32 -0000
1.3
+++ tools/gnu/classpath/tools/jar/Updater.java 15 May 2006 01:52:17 -0000
@@ -38,8 +38,6 @@
package gnu.classpath.tools.jar;
-import gnu.classpath.tools.getopt.OptionException;
-
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
@@ -66,7 +64,7 @@
return result;
}
- public void run(Main parameters) throws IOException, OptionException
+ public void run(Main parameters) throws IOException
{
// Set this early so that createManifest can use it.
inputJar = new JarFile(parameters.archiveFile);