diff options
Diffstat (limited to 'tools/gnu/classpath/tools/getopt/Parser.java')
-rw-r--r-- | tools/gnu/classpath/tools/getopt/Parser.java | 176 |
1 files changed, 158 insertions, 18 deletions
diff --git a/tools/gnu/classpath/tools/getopt/Parser.java b/tools/gnu/classpath/tools/getopt/Parser.java index 8d70c01d0..082cf8945 100644 --- a/tools/gnu/classpath/tools/getopt/Parser.java +++ b/tools/gnu/classpath/tools/getopt/Parser.java @@ -39,8 +39,11 @@ package gnu.classpath.tools.getopt; import java.io.PrintStream; +import java.text.BreakIterator; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Iterator; +import java.util.Locale; /** * An instance of this class is used to parse command-line options. It does "GNU @@ -52,6 +55,9 @@ import java.util.Iterator; */ public class Parser { + /** The maximum right column position. */ + public static final int MAX_LINE_LENGTH = 80; + private String programName; private String headerText; @@ -84,6 +90,69 @@ public class Parser } /** + * Print a designated text to a {@link PrintStream}, eventually wrapping the + * lines of text so as to ensure that the width of each line does not overflow + * {@link #MAX_LINE_LENGTH} columns. The line-wrapping is done with a + * {@link BreakIterator} using the default {@link Locale}. + * <p> + * The text to print may contain <code>\n</code> characters. This method will + * force a line-break for each such character. + * + * @param out the {@link PrintStream} destination of the formatted text. + * @param text the text to print. + * @see Parser#MAX_LINE_LENGTH + */ + protected static void formatText(PrintStream out, String text) + { + formatText(out, text, Locale.getDefault()); + } + + /** + * Similar to the method with the same name and two arguments, except that the + * caller MUST specify a non-null {@link Locale} instance. + * <p> + * Print a designated text to a {@link PrintStream}, eventually wrapping the + * lines of text so as to ensure that the width of each line does not overflow + * {@link #MAX_LINE_LENGTH} columns. The line-wrapping is done with a + * {@link BreakIterator} using the designated {@link Locale}. + * <p> + * The text to print may contain <code>\n</code> characters. This method will + * force a line-break for each such character. + * + * @param out the {@link PrintStream} destination of the formatted text. + * @param text the text to print. + * @param aLocale the {@link Locale} instance to use when constructing the + * {@link BreakIterator}. + * @see Parser#MAX_LINE_LENGTH + */ + protected static void formatText(PrintStream out, String text, Locale aLocale) + { + BreakIterator bit = BreakIterator.getLineInstance(aLocale); + String[] lines = text.split("\n"); //$NON-NLS-1$ + for (int i = 0; i < lines.length; i++) + { + text = lines[i]; + bit.setText(text); + int length = 0; + int finish; + int start = bit.first(); + while ((finish = bit.next()) != BreakIterator.DONE) + { + String word = text.substring(start, finish); + length += word.length(); + if (length >= MAX_LINE_LENGTH) + { + out.println(); + length = word.length(); + } + out.print(word); + start = finish; + } + out.println(); + } + } + + /** * Create a new parser. The program name is used when printing error messages. * The version string is printed verbatim in response to "--version". * @@ -95,7 +164,10 @@ public class Parser { this.programName = programName; this.longOnly = longOnly; - defaultGroup.add(new Option("help", "print this help, then exit") + + // Put standard options in their own section near the end. + OptionGroup finalGroup = new OptionGroup(Messages.getString("Parser.StdOptions")); //$NON-NLS-1$ + finalGroup.add(new Option("help", Messages.getString("Parser.PrintHelp")) //$NON-NLS-1$ //$NON-NLS-2$ { public void parsed(String argument) throws OptionException { @@ -103,7 +175,7 @@ public class Parser System.exit(0); } }); - defaultGroup.add(new Option("version", "print version number, then exit") + finalGroup.add(new Option("version", Messages.getString("Parser.PrintVersion")) //$NON-NLS-1$ //$NON-NLS-2$ { public void parsed(String argument) throws OptionException { @@ -111,6 +183,17 @@ public class Parser System.exit(0); } }); + finalGroup.add(new Option('J', Messages.getString("Parser.JArgument"), Messages.getString("Parser.JName")) //$NON-NLS-1$ //$NON-NLS-2$ + { + public void parsed(String argument) throws OptionException + { + // -J should be handled by the appletviewer wrapper binary. + // We add it here so that it shows up in the --help output. + // Note that there is a special case for this in OptionGroup. + } + }); + add(finalGroup); + add(defaultGroup); } @@ -155,14 +238,24 @@ public class Parser public synchronized void add(OptionGroup group) { options.addAll(group.options); - optionGroups.add(group); + // This ensures that the final group always appears at the end + // of the options. + if (optionGroups.isEmpty()) + optionGroups.add(group); + else + optionGroups.add(optionGroups.size() - 1, group); + } + + public void printHelp() + { + this.printHelp(System.out); } void printHelp(PrintStream out) { if (headerText != null) { - out.println(headerText); + formatText(out, headerText); out.println(); } @@ -170,19 +263,49 @@ public class Parser while (it.hasNext()) { OptionGroup group = (OptionGroup) it.next(); - group.printHelp(out); - out.println(); + // An option group might be empty, in which case we don't + // want to print it.. + if (! group.options.isEmpty()) + { + group.printHelp(out, longOnly); + out.println(); + } } if (footerText != null) - out.println(footerText); + 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; if (currentIndex >= args.length) - throw new OptionException("option '" + request + "' requires an argument"); + { + String message + = MessageFormat.format(Messages.getString("Parser.ArgReqd"), //$NON-NLS-1$ + new Object[] { request }); + throw new OptionException(request); + } return args[currentIndex]; } @@ -204,7 +327,11 @@ public class Parser } } if (found == null) - throw new OptionException("unrecognized option '" + real + "'"); + { + String msg = MessageFormat.format(Messages.getString("Parser.Unrecognized"), //$NON-NLS-1$ + new Object[] { real }); + throw new OptionException(msg); + } String argument = null; if (found.getTakesArgument()) { @@ -215,8 +342,10 @@ public class Parser } else if (eq != - 1) { - throw new OptionException("option '" + real.substring(0, eq + index) - + "' doesn't allow an argument"); + String msg + = MessageFormat.format(Messages.getString("Parser.NoArg"), //$NON-NLS-1$ + new Object[] { real.substring(0, eq + index) }); + throw new OptionException(msg); } found.parsed(argument); } @@ -234,10 +363,14 @@ public class Parser } } if (found == null) - throw new OptionException("unrecognized option '-" + option + "'"); + { + String msg = MessageFormat.format(Messages.getString("Parser.UnrecDash"), //$NON-NLS-1$ + new Object[] { "" + option }); //$NON-NLS-1$ + throw new OptionException(msg); + } String argument = null; if (found.getTakesArgument()) - argument = getArgument("-" + option); + argument = getArgument("-" + option); //$NON-NLS-1$ found.parsed(argument); } @@ -266,12 +399,12 @@ public class Parser { if (args[currentIndex].length() == 0 || args[currentIndex].charAt(0) != '-' - || "-".equals(args[currentIndex])) + || "-".equals(args[currentIndex])) //$NON-NLS-1$ { files.notifyFile(args[currentIndex]); continue; } - if ("--".equals(args[currentIndex])) + if ("--".equals(args[currentIndex])) //$NON-NLS-1$ break; if (args[currentIndex].charAt(1) == '-') handleLongOption(args[currentIndex], 2); @@ -283,12 +416,19 @@ 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."); + System.err.println(programName + ": " + err.getMessage()); //$NON-NLS-1$ + String fmt; + if (longOnly) + fmt = Messages.getString("Parser.TryHelpShort"); //$NON-NLS-1$ + else + fmt = Messages.getString("Parser.TryHelpLong"); //$NON-NLS-1$ + String msg = MessageFormat.format(fmt, new Object[] { programName }); + System.err.println(programName + ": " + msg); //$NON-NLS-1$ System.exit(1); } } |