roxspring 2003/10/22 10:01:06
Modified: cli/src/java/org/apache/commons/cli2 HelpFormatter.java
CommandLineParser.java
cli/src/test/org/apache/commons/cli2 DocumentationTest.java
HelpFormatterTest.java
Log:
Added PrintWriter attribute to HelpFormatter and removed the remaining
argment from the print(..) method.
A bonus ofthis is that none of the cli2 tests print to System.out any more
Revision Changes Path
1.3 +24 -4
jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/HelpFormatter.java
Index: HelpFormatter.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/HelpFormatter.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HelpFormatter.java 22 Oct 2003 16:32:14 -0000 1.2
+++ HelpFormatter.java 22 Oct 2003 17:01:06 -0000 1.3
@@ -113,6 +113,8 @@
private String footer = null;
private String shellCommand = "";
+
+ private PrintWriter out = new PrintWriter(System.out);//or should this default
to .err?
private final String gutterLeft;
private final String gutterCenter;
@@ -156,8 +158,10 @@
throw new IllegalArgumentException("The gutter strings leave
no space for output! Supply shorter gutters or more width.");
}
}
+
+
- public void print(final PrintWriter out) throws IOException {
+ public void print() throws IOException {
printHeader(out);
printException(out);
printUsage(out);
@@ -516,5 +520,21 @@
public String getShellCommand() {
return shellCommand;
}
+
+ /**
+ * @param out The out to set.
+ */
+ public void setPrintWriter(PrintWriter out)
+ {
+ this.out = out;
+ }
+
+ /**
+ * @return Returns the out.
+ */
+ public PrintWriter getPrintWriter()
+ {
+ return out;
+ }
}
1.2 +4 -5
jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/CommandLineParser.java
Index: CommandLineParser.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli2/CommandLineParser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CommandLineParser.java 18 Oct 2003 22:00:07 -0000 1.1
+++ CommandLineParser.java 22 Oct 2003 17:01:06 -0000 1.2
@@ -137,8 +137,7 @@
}
// print help
- helpFormatter.print(
- new PrintWriter(new OutputStreamWriter(System.out)));
+ helpFormatter.print();
return null;
}
1.2 +313 -306
jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/DocumentationTest.java
Index: DocumentationTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/DocumentationTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DocumentationTest.java 18 Oct 2003 22:00:33 -0000 1.1
+++ DocumentationTest.java 22 Oct 2003 17:01:06 -0000 1.2
@@ -1,306 +1,313 @@
-/*
- * Created on 10-Aug-2003
- *
- * To change the template for this generated file go to
- * Window - Preferences - Java - Code Generation - Code and Comments
- */
-package org.apache.commons.cli2;
-
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.PrintWriter;
-
-import junit.framework.TestCase;
-
-/**
- * @author Rob
- *
- * To change the template for this generated type comment go to
- * Window - Preferences - Java - Code Generation - Code and Comments
- */
-public class DocumentationTest extends TestCase {
-
- public void testBasicUsage() throws IOException, OptionException {
- /*
- * --version
- * -? -h --help
- * -log file
- * -s|-q|-v|-d
- * Bursting
- * File/Num/Date validation
- * Switches
- * Commands
- * Auto help
- * Auto exception help
- *
- */
- DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
- Option version =
- obuilder
- .withLongName("version")
- .withDescription("Displays version information and
then exits")
- .create();
-
- Option help =
- obuilder
- .withShortName("h")
- .withShortName("?")
- .withLongName("help")
- .withDescription("Displays help on usage and then
exits")
- .create();
-
- ArgumentBuilder abuilder = new ArgumentBuilder();
- Argument logFile =
- abuilder
- .withDescription("The log file to write to")
- .withName("file")
- .withMinimum(1)
- .withMaximum(1)
- .create();
- Option log =
- obuilder
- .withArgument(logFile)
- .withShortName("log")
- .withDescription("Log progress information to a file")
- .create();
-
- GroupBuilder gbuilder = new GroupBuilder();
- Group outputQuality =
- gbuilder
- .withName("quality")
- .withDescription("Controls the quality of console
output")
- .withMaximum(1)
- .withOption(
- obuilder
- .withShortName("s")
- .withDescription("Silent")
- .create())
- .withOption(
- obuilder
- .withShortName("q")
- .withDescription("Quiet")
- .create())
- .withOption(
- obuilder
- .withShortName("n")
- .withDescription("Normal")
- .create())
- .withOption(
- obuilder
- .withShortName("v")
- .withDescription("Verbose")
- .create())
- .withOption(
- obuilder
- .withShortName("d")
- .withDescription("Debug")
- .create())
- .create();
-
- Group options =
- new GroupBuilder()
- .withName("options")
- .withOption(version)
- .withOption(help)
- .withOption(log)
- .withOption(outputQuality)
- .create();
-
- final String[] args = new String[] { "--bad-option" };
-
- CommandLineParser parser = new CommandLineParser();
- parser.setGroup(options);
- parser.setHelpOption(help);
- CommandLine commandLine = parser.parseAndHelp(args);
- if (commandLine != null) {
- if (commandLine.hasOption(version)) {
- System.out.println("MyApp ver 1.0");
- return;
- }
- if (commandLine.hasOption("-log")) {
- String filename = (String)
commandLine.getValue("-log");
- //...
- }
- }
-
- try {
- commandLine = parser.parse(new String[] { "--bad-option" });
- fail("Unexpected Option!");
- } catch (UnexpectedOptionException uoe) {
- assertEquals(
- "Unexpected --bad-option while processing options",
- uoe.getMessage());
- }
- }
-
- public void testExampleAnt() throws IOException {
- // Based on Ant 1.5.3
-
- final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
- final ArgumentBuilder abuilder = new ArgumentBuilder();
- final GroupBuilder gbuilder = new GroupBuilder();
-
- Option help =
- obuilder
- .withShortName("help")
- .withDescription("print this message")
- .create();
- Option projecthelp =
- obuilder
- .withShortName("projecthelp")
- .withDescription("print project help information")
- .create();
- Option version =
- obuilder
- .withShortName("version")
- .withDescription("print the version information and
exit")
- .create();
- Option diagnostics =
- obuilder
- .withShortName("diagnostics")
- .withDescription("print information that might be
helpful to diagnose or report problems.")
- .create();
- Option quiet =
- obuilder
- .withShortName("quiet")
- .withShortName("q")
- .withDescription("be extra quiet")
- .create();
- Option verbose =
- obuilder
- .withShortName("verbose")
- .withShortName("v")
- .withDescription("be extra verbose")
- .create();
- Option debug =
- obuilder
- .withShortName("debug")
- .withDescription("print debugging information")
- .create();
- Option emacs =
- obuilder
- .withShortName("emacs")
- .withDescription("produce logging information without
adornments")
- .create();
- Option logfile =
- obuilder
- .withShortName("logfile")
- .withShortName("l")
- .withDescription("produce logging information without
adornments")
- .withArgument(
- abuilder
- .withName("file")
- .withMinimum(1)
- .withMaximum(1)
- .create())
- .create();
- Option logger =
- obuilder
- .withShortName("logger")
- .withDescription("the class which is to perform
logging")
- .withArgument(
- abuilder
- .withName("classname")
- .withMinimum(1)
- .withMaximum(1)
- .create())
- .create();
- Option listener =
- obuilder
- .withShortName("listener")
- .withDescription("add an instance of class as a
project listener")
- .withArgument(
- abuilder
- .withName("classname")
- .withMinimum(1)
- .withMaximum(1)
- .create())
- .create();
- Option buildfile =
- obuilder
- .withShortName("buildfile")
- .withShortName("b")
- .withShortName("file")
- .withDescription("use given buildfile")
- .withArgument(
- abuilder
- .withName("file")
- .withMinimum(1)
- .withMaximum(1)
- .create())
- .create();
- Option property = new PropertyOption();
- Option propertyfile =
- obuilder
- .withShortName("propertyfile")
- .withDescription("load all properties from file with
-D properties taking precedence")
- .withArgument(
- abuilder
- .withName("name")
- .withMinimum(1)
- .withMaximum(1)
- .create())
- .create();
- Option inputhandler =
- obuilder
- .withShortName("inputhandler")
- .withDescription("the class which will handle input
requests")
- .withArgument(
- abuilder
- .withName("class")
- .withMinimum(1)
- .withMaximum(1)
- .create())
- .create();
- Option find =
- obuilder
- .withShortName("find")
- .withDescription("search for buildfile towards the
root of the filesystem and use it")
- .withArgument(
- abuilder
- .withName("file")
- .withMinimum(1)
- .withMaximum(1)
- .create())
- .create();
- Option targets = abuilder.withName("target").create();
-
- Group options =
- gbuilder
- .withName("options")
- .withOption(help)
- .withOption(projecthelp)
- .withOption(version)
- .withOption(diagnostics)
- .withOption(quiet)
- .withOption(verbose)
- .withOption(debug)
- .withOption(emacs)
- .withOption(logfile)
- .withOption(logger)
- .withOption(listener)
- .withOption(buildfile)
- .withOption(property)
- .withOption(propertyfile)
- .withOption(inputhandler)
- .withOption(find)
- .withOption(targets)
- .create();
-
- HelpFormatter hf = new HelpFormatter();
- hf.setShellCommand("ant");
- hf.getFullUsageSettings().add(HelpSetting.DISPLAY_GROUP_NAME);
- hf.getFullUsageSettings().add(HelpSetting.DISPLAY_GROUP_ARGUMENT);
- hf.getFullUsageSettings().remove(HelpSetting.DISPLAY_GROUP_EXPANDED);
-
- hf.getLineUsageSettings().add(HelpSetting.DISPLAY_PROPERTY_OPTION);
- hf.getLineUsageSettings().add(HelpSetting.DISPLAY_PARENT_ARGUMENT);
-
- hf.getDisplaySettings().remove(HelpSetting.DISPLAY_GROUP_ARGUMENT);
-
- hf.setGroup(options);
- hf.print(new PrintWriter(new OutputStreamWriter(System.out)));
-
- }
-
-}
+/*
+ * Created on 10-Aug-2003
+ *
+ * To change the template for this generated file go to
+ * Window - Preferences - Java - Code Generation - Code and Comments
+ */
+package org.apache.commons.cli2;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Rob
+ *
+ * To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Generation - Code and Comments
+ */
+public class DocumentationTest extends TestCase {
+
+ public void testBasicUsage() throws IOException, OptionException {
+ HelpFormatter helpFormatter = new HelpFormatter();
+ //ignore all printed
+ helpFormatter.setPrintWriter(new PrintWriter(new StringWriter()));
+
+ /*
+ * --version
+ * -? -h --help
+ * -log file
+ * -s|-q|-v|-d
+ * Bursting
+ * File/Num/Date validation
+ * Switches
+ * Commands
+ * Auto help
+ * Auto exception help
+ *
+ */
+ DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
+ Option version =
+ obuilder
+ .withLongName("version")
+ .withDescription("Displays version information and
then exits")
+ .create();
+
+ Option help =
+ obuilder
+ .withShortName("h")
+ .withShortName("?")
+ .withLongName("help")
+ .withDescription("Displays help on usage and then
exits")
+ .create();
+
+ ArgumentBuilder abuilder = new ArgumentBuilder();
+ Argument logFile =
+ abuilder
+ .withDescription("The log file to write to")
+ .withName("file")
+ .withMinimum(1)
+ .withMaximum(1)
+ .create();
+ Option log =
+ obuilder
+ .withArgument(logFile)
+ .withShortName("log")
+ .withDescription("Log progress information to a file")
+ .create();
+
+ GroupBuilder gbuilder = new GroupBuilder();
+ Group outputQuality =
+ gbuilder
+ .withName("quality")
+ .withDescription("Controls the quality of console
output")
+ .withMaximum(1)
+ .withOption(
+ obuilder
+ .withShortName("s")
+ .withDescription("Silent")
+ .create())
+ .withOption(
+ obuilder
+ .withShortName("q")
+ .withDescription("Quiet")
+ .create())
+ .withOption(
+ obuilder
+ .withShortName("n")
+ .withDescription("Normal")
+ .create())
+ .withOption(
+ obuilder
+ .withShortName("v")
+ .withDescription("Verbose")
+ .create())
+ .withOption(
+ obuilder
+ .withShortName("d")
+ .withDescription("Debug")
+ .create())
+ .create();
+
+ Group options =
+ new GroupBuilder()
+ .withName("options")
+ .withOption(version)
+ .withOption(help)
+ .withOption(log)
+ .withOption(outputQuality)
+ .create();
+
+ final String[] args = new String[] { "--bad-option" };
+
+ CommandLineParser parser = new CommandLineParser();
+ parser.setHelpFormatter(helpFormatter);
+ parser.setGroup(options);
+ parser.setHelpOption(help);
+ CommandLine commandLine = parser.parseAndHelp(args);
+ if (commandLine != null) {
+ if (commandLine.hasOption(version)) {
+ System.out.println("MyApp ver 1.0");
+ return;
+ }
+ if (commandLine.hasOption("-log")) {
+ String filename = (String)
commandLine.getValue("-log");
+ //...
+ }
+ }
+
+ try {
+ commandLine = parser.parse(args);
+ fail("Unexpected Option!");
+ } catch (UnexpectedOptionException uoe) {
+ assertEquals(
+ "Unexpected --bad-option while processing options",
+ uoe.getMessage());
+ }
+ }
+
+ public void testExampleAnt() throws IOException {
+ // Based on Ant 1.5.3
+
+ final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
+ final ArgumentBuilder abuilder = new ArgumentBuilder();
+ final GroupBuilder gbuilder = new GroupBuilder();
+
+ Option help =
+ obuilder
+ .withShortName("help")
+ .withDescription("print this message")
+ .create();
+ Option projecthelp =
+ obuilder
+ .withShortName("projecthelp")
+ .withDescription("print project help information")
+ .create();
+ Option version =
+ obuilder
+ .withShortName("version")
+ .withDescription("print the version information and
exit")
+ .create();
+ Option diagnostics =
+ obuilder
+ .withShortName("diagnostics")
+ .withDescription("print information that might be
helpful to diagnose or report problems.")
+ .create();
+ Option quiet =
+ obuilder
+ .withShortName("quiet")
+ .withShortName("q")
+ .withDescription("be extra quiet")
+ .create();
+ Option verbose =
+ obuilder
+ .withShortName("verbose")
+ .withShortName("v")
+ .withDescription("be extra verbose")
+ .create();
+ Option debug =
+ obuilder
+ .withShortName("debug")
+ .withDescription("print debugging information")
+ .create();
+ Option emacs =
+ obuilder
+ .withShortName("emacs")
+ .withDescription("produce logging information without
adornments")
+ .create();
+ Option logfile =
+ obuilder
+ .withShortName("logfile")
+ .withShortName("l")
+ .withDescription("produce logging information without
adornments")
+ .withArgument(
+ abuilder
+ .withName("file")
+ .withMinimum(1)
+ .withMaximum(1)
+ .create())
+ .create();
+ Option logger =
+ obuilder
+ .withShortName("logger")
+ .withDescription("the class which is to perform
logging")
+ .withArgument(
+ abuilder
+ .withName("classname")
+ .withMinimum(1)
+ .withMaximum(1)
+ .create())
+ .create();
+ Option listener =
+ obuilder
+ .withShortName("listener")
+ .withDescription("add an instance of class as a
project listener")
+ .withArgument(
+ abuilder
+ .withName("classname")
+ .withMinimum(1)
+ .withMaximum(1)
+ .create())
+ .create();
+ Option buildfile =
+ obuilder
+ .withShortName("buildfile")
+ .withShortName("b")
+ .withShortName("file")
+ .withDescription("use given buildfile")
+ .withArgument(
+ abuilder
+ .withName("file")
+ .withMinimum(1)
+ .withMaximum(1)
+ .create())
+ .create();
+ Option property = new PropertyOption();
+ Option propertyfile =
+ obuilder
+ .withShortName("propertyfile")
+ .withDescription("load all properties from file with
-D properties taking precedence")
+ .withArgument(
+ abuilder
+ .withName("name")
+ .withMinimum(1)
+ .withMaximum(1)
+ .create())
+ .create();
+ Option inputhandler =
+ obuilder
+ .withShortName("inputhandler")
+ .withDescription("the class which will handle input
requests")
+ .withArgument(
+ abuilder
+ .withName("class")
+ .withMinimum(1)
+ .withMaximum(1)
+ .create())
+ .create();
+ Option find =
+ obuilder
+ .withShortName("find")
+ .withDescription("search for buildfile towards the
root of the filesystem and use it")
+ .withArgument(
+ abuilder
+ .withName("file")
+ .withMinimum(1)
+ .withMaximum(1)
+ .create())
+ .create();
+ Option targets = abuilder.withName("target").create();
+
+ Group options =
+ gbuilder
+ .withName("options")
+ .withOption(help)
+ .withOption(projecthelp)
+ .withOption(version)
+ .withOption(diagnostics)
+ .withOption(quiet)
+ .withOption(verbose)
+ .withOption(debug)
+ .withOption(emacs)
+ .withOption(logfile)
+ .withOption(logger)
+ .withOption(listener)
+ .withOption(buildfile)
+ .withOption(property)
+ .withOption(propertyfile)
+ .withOption(inputhandler)
+ .withOption(find)
+ .withOption(targets)
+ .create();
+
+ HelpFormatter hf = new HelpFormatter();
+ hf.setShellCommand("ant");
+ hf.getFullUsageSettings().add(HelpSetting.DISPLAY_GROUP_NAME);
+ hf.getFullUsageSettings().add(HelpSetting.DISPLAY_GROUP_ARGUMENT);
+ hf.getFullUsageSettings().remove(HelpSetting.DISPLAY_GROUP_EXPANDED);
+
+ hf.getLineUsageSettings().add(HelpSetting.DISPLAY_PROPERTY_OPTION);
+ hf.getLineUsageSettings().add(HelpSetting.DISPLAY_PARENT_ARGUMENT);
+
+ hf.getDisplaySettings().remove(HelpSetting.DISPLAY_GROUP_ARGUMENT);
+
+ hf.setGroup(options);
+ // redirect printed stuff to a string
+ hf.setPrintWriter(new PrintWriter(new StringWriter()));
+ hf.print();
+
+ }
+
+}
1.3 +2 -1
jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/HelpFormatterTest.java
Index: HelpFormatterTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli2/HelpFormatterTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HelpFormatterTest.java 22 Oct 2003 16:32:14 -0000 1.2
+++ HelpFormatterTest.java 22 Oct 2003 17:01:06 -0000 1.3
@@ -60,7 +60,8 @@
public void testPrint() throws IOException {
final StringWriter writer = new StringWriter();
- helpFormatter.print(new PrintWriter(writer));
+ helpFormatter.setPrintWriter(new PrintWriter(writer));
+ helpFormatter.print();
//System.out.println(writer.toString());
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]