diff options
author | Raif S. Naffah <raif@swiftdsl.com.au> | 2006-05-10 21:00:43 +0000 |
---|---|---|
committer | Raif S. Naffah <raif@swiftdsl.com.au> | 2006-05-10 21:00:43 +0000 |
commit | 8b840382d0488c4990eb37bda8efb956b49a2577 (patch) | |
tree | 63f5f8c8f78f24d2020a8577d9e1a2950fca2408 /tools | |
parent | e7973b1c95320968f1c0ea82ee984bf95ed042ef (diff) | |
download | classpath-8b840382d0488c4990eb37bda8efb956b49a2577.tar.gz |
2006-05-11 Raif S. Naffah <raif@swiftdsl.com.au>
* tools/gnu/classpath/tools/getopt/OptionGroup.java
(FILLER): New constant.
(formatText(PrintStream,String,int)): New method.
(formatText(PrintStream,String,int,Locale)): Likewise.
(printHelp): Use formatText method.
* tools/gnu/classpath/tools/getopt/Parser.java
(MAX_LINE_LENGTH): New constant.
(formatText(PrintStream,String)): New method.
(formatText(PrintStream,String,Locale)): Likewise.
(printHelp): New method.
(printHelp(PrintStream)): Increased visibility to protected.
Use formatText method.
Diffstat (limited to 'tools')
-rw-r--r-- | tools/gnu/classpath/tools/getopt/OptionGroup.java | 90 | ||||
-rw-r--r-- | tools/gnu/classpath/tools/getopt/Parser.java | 79 |
2 files changed, 162 insertions, 7 deletions
diff --git a/tools/gnu/classpath/tools/getopt/OptionGroup.java b/tools/gnu/classpath/tools/getopt/OptionGroup.java index 518d9f938..f7d966d94 100644 --- a/tools/gnu/classpath/tools/getopt/OptionGroup.java +++ b/tools/gnu/classpath/tools/getopt/OptionGroup.java @@ -39,8 +39,10 @@ package gnu.classpath.tools.getopt; import java.io.PrintStream; +import java.text.BreakIterator; import java.util.ArrayList; import java.util.Iterator; +import java.util.Locale; /** * An option group holds a collection of Options. It also has a name. Option @@ -48,6 +50,9 @@ import java.util.Iterator; */ public class OptionGroup { + /** An 80-character string of whitespaces to use as a source for padding. */ + private static final String FILLER = " " + + " "; private String name; ArrayList options = new ArrayList(); @@ -70,6 +75,85 @@ public class OptionGroup } /** + * 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 Parser#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. + * @param leftMargin a positive value indicating the column position of the + * start of the first line. Continuation lines, if they exist, are + * printed starting at <code>leftMargin + 2</code> as per GNU + * convention. + * @see Parser#MAX_LINE_LENGTH + */ + protected static void formatText(PrintStream out, String text, int leftMargin) + { + formatText(out, text, leftMargin, Locale.getDefault()); + } + + /** + * Similar to the method with the same name and three 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 Parser#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 leftMargin a positive value indicating the column position of the + * start of the first line. Continuation lines, if they exist, are + * printed starting at <code>leftMargin + 2</code> as per GNU + * convention. + * @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, int leftMargin, + Locale aLocale) + { + BreakIterator bit = BreakIterator.getLineInstance(aLocale); + String[] lines = text.split("\n"); + int length = leftMargin; + String leftPadding = FILLER.substring(0, leftMargin + 2); + for (int i = 0; i < lines.length; i++) + { + text = lines[i]; + bit.setText(text); + int start = bit.first(); + int finish; + while ((finish = bit.next()) != BreakIterator.DONE) + { + String word = text.substring(start, finish); + length += word.length(); + if (length >= Parser.MAX_LINE_LENGTH) + { + out.println(); + out.print(leftPadding); + length = word.length() + leftMargin + 2; + } + out.print(word); + start = finish; + } + out.println(); + if (i != lines.length - 1) + { + length = leftMargin + 2; + out.print(leftPadding); + } + } + } + + /** * Add an option to this option group. * * @param opt the option to add @@ -179,11 +263,9 @@ public class OptionGroup column += 1 + argName.length(); } } - for (; column < maxArgLen; ++column) - out.print(' '); // FIXME: should have a better heuristic for padding. - out.print(" "); - out.println(option.getDescription()); + out.print(FILLER.substring(0, maxArgLen + 4 - column)); + formatText(out, option.getDescription(), maxArgLen + 4); } } } diff --git a/tools/gnu/classpath/tools/getopt/Parser.java b/tools/gnu/classpath/tools/getopt/Parser.java index 883a8c1f3..f819fb745 100644 --- a/tools/gnu/classpath/tools/getopt/Parser.java +++ b/tools/gnu/classpath/tools/getopt/Parser.java @@ -39,8 +39,10 @@ package gnu.classpath.tools.getopt; import java.io.PrintStream; +import java.text.BreakIterator; 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 +54,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 +89,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"); + 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". * @@ -177,11 +245,16 @@ public class Parser optionGroups.add(optionGroups.size() - 1, group); } - void printHelp(PrintStream out) + public void printHelp() + { + this.printHelp(System.out); + } + + protected void printHelp(PrintStream out) { if (headerText != null) { - out.println(headerText); + formatText(out, headerText); out.println(); } @@ -199,7 +272,7 @@ public class Parser } if (footerText != null) - out.println(footerText); + formatText(out, footerText); } private String getArgument(String request) throws OptionException |