summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorTom Tromey <tromey@redhat.com>2006-05-15 01:57:29 +0000
committerTom Tromey <tromey@redhat.com>2006-05-15 01:57:29 +0000
commit7f25233aa67e76cf7404f7b983102b40fc0c3794 (patch)
tree4a59ad40059bcfb1ef75309be936a7cf1fbb49c4 /tools
parentc94daa0ddcaaec439ab2ea207d05483bd209c74a (diff)
downloadclasspath-7f25233aa67e76cf7404f7b983102b40fc0c3794.tar.gz
* 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.
Diffstat (limited to 'tools')
-rw-r--r--tools/gnu/classpath/tools/getopt/Parser.java27
-rw-r--r--tools/gnu/classpath/tools/jar/Action.java4
-rw-r--r--tools/gnu/classpath/tools/jar/Creator.java4
-rw-r--r--tools/gnu/classpath/tools/jar/Indexer.java12
-rw-r--r--tools/gnu/classpath/tools/jar/Main.java47
-rw-r--r--tools/gnu/classpath/tools/jar/Updater.java4
6 files changed, 59 insertions, 39 deletions
diff --git a/tools/gnu/classpath/tools/getopt/Parser.java b/tools/gnu/classpath/tools/getopt/Parser.java
index f819fb745..0ef815d45 100644
--- a/tools/gnu/classpath/tools/getopt/Parser.java
+++ b/tools/gnu/classpath/tools/getopt/Parser.java
@@ -250,7 +250,7 @@ public class Parser
this.printHelp(System.out);
}
- protected void printHelp(PrintStream out)
+ void printHelp(PrintStream out)
{
if (headerText != null)
{
@@ -275,6 +275,26 @@ public class Parser
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 {@link OptionException} whose message should be shown to
+ * the user.
+ * <p>
+ * It is better to do validation here than after {@link #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 @@ public class Parser
// 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);
}
}
diff --git a/tools/gnu/classpath/tools/jar/Action.java b/tools/gnu/classpath/tools/jar/Action.java
index b6398936d..6363157ae 100644
--- a/tools/gnu/classpath/tools/jar/Action.java
+++ b/tools/gnu/classpath/tools/jar/Action.java
@@ -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 class Action
}
public abstract void run(Main parameters)
- throws IOException, OptionException;
+ throws IOException;
}
diff --git a/tools/gnu/classpath/tools/jar/Creator.java b/tools/gnu/classpath/tools/jar/Creator.java
index caf093b77..e10addd5b 100644
--- a/tools/gnu/classpath/tools/jar/Creator.java
+++ b/tools/gnu/classpath/tools/jar/Creator.java
@@ -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 @@ public class Creator
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);
diff --git a/tools/gnu/classpath/tools/jar/Indexer.java b/tools/gnu/classpath/tools/jar/Indexer.java
index 8182d9098..3fff8e1af 100644
--- a/tools/gnu/classpath/tools/jar/Indexer.java
+++ b/tools/gnu/classpath/tools/jar/Indexer.java
@@ -38,7 +38,6 @@ exception statement from your version. */
package gnu.classpath.tools.jar;
-import gnu.classpath.tools.getopt.OptionException;
import gnu.java.net.IndexListParser;
import java.io.ByteArrayInputStream;
@@ -135,15 +134,4 @@ public class Indexer
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);
- }
}
diff --git a/tools/gnu/classpath/tools/jar/Main.java b/tools/gnu/classpath/tools/jar/Main.java
index 4bb55deae..ae802d0c7 100644
--- a/tools/gnu/classpath/tools/jar/Main.java
+++ b/tools/gnu/classpath/tools/jar/Main.java
@@ -138,9 +138,37 @@ public class Main
}
}
+ 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 @@ public class Main
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 @@ public class Main
{
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:");
diff --git a/tools/gnu/classpath/tools/jar/Updater.java b/tools/gnu/classpath/tools/jar/Updater.java
index b717507a8..29586befd 100644
--- a/tools/gnu/classpath/tools/jar/Updater.java
+++ b/tools/gnu/classpath/tools/jar/Updater.java
@@ -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 @@ public class Updater
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);