You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/05/17 13:44:33 UTC
cvs commit: jakarta-commons-sandbox/cli/src/conf MANIFEST.MF
jstrachan 02/05/17 04:44:33
Modified: cli/src/java/org/apache/commons/cli HelpFormatter.java
Option.java overview.html
AlreadySelectedException.java package.html
CommandLine.java Options.java
cli/src/test/org/apache/commons/cli TestHelpFormatter.java
HelpFormatterExamples.java AllTest.java
cli/src/conf MANIFEST.MF
Added: cli/src/test/org/apache/commons/cli ValuesTest.java
Log:
Applied John Keyes' patches to optional Options and multiple value support
Revision Changes Path
1.2 +337 -337 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/HelpFormatter.java
Index: HelpFormatter.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/HelpFormatter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HelpFormatter.java 1 Feb 2002 16:28:34 -0000 1.1
+++ HelpFormatter.java 17 May 2002 11:44:32 -0000 1.2
@@ -1,337 +1,337 @@
-/*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
- *
- * This software is published under the terms of the Apache Software License
- * version 1.1, a copy of which has been included with this distribution in
- * the LICENSE file.
- *
- * $Id: HelpFormatter.java,v 1.1 2002/02/01 16:28:34 jstrachan Exp $
- */
-package org.apache.commons.cli;
-
-import java.io.PrintWriter;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-
-/**
- * A formatter of help messages for the current command line options
- *
- * @author Slawek Zachcial
- **/
-public class HelpFormatter
-{
- // --------------------------------------------------------------- Constants
-
- public static final int DEFAULT_WIDTH = 80;
- public static final int DEFAULT_LEFT_PAD = 1;
- public static final int DEFAULT_DESC_PAD = 3;
- public static final String DEFAULT_SYNTAX_PREFIX = "usage: ";
- public static final String DEFAULT_OPT_PREFIX = "-";
- public static final String DEFAULT_LONG_OPT_PREFIX = "--";
- public static final String DEFAULT_ARG_NAME = "arg";
-
- // ------------------------------------------------------------------ Static
-
- // -------------------------------------------------------------- Attributes
-
- public int defaultWidth;
- public int defaultLeftPad;
- public int defaultDescPad;
- public String defaultSyntaxPrefix;
- public String defaultNewLine;
- public String defaultOptPrefix;
- public String defaultLongOptPrefix;
- public String defaultArgName;
-
- // ------------------------------------------------------------ Constructors
- public HelpFormatter()
- {
- defaultWidth = DEFAULT_WIDTH;
- defaultLeftPad = DEFAULT_LEFT_PAD;
- defaultDescPad = DEFAULT_DESC_PAD;
- defaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX;
- defaultNewLine = System.getProperty("line.separator");
- defaultOptPrefix = DEFAULT_OPT_PREFIX;
- defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX;
- defaultArgName = DEFAULT_ARG_NAME;
- }
-
- // ------------------------------------------------------------------ Public
-
- public void printHelp( String cmdLineSyntax,
- Options options )
- {
- printHelp( defaultWidth, cmdLineSyntax, null, options, null );
- }
-
- public void printHelp( String cmdLineSyntax,
- String header,
- Options options,
- String footer )
- {
- printHelp(defaultWidth, cmdLineSyntax, header, options, footer);
- }
-
- public void printHelp( int width,
- String cmdLineSyntax,
- String header,
- Options options,
- String footer )
- {
- PrintWriter pw = new PrintWriter(System.out);
- printHelp( pw, width, cmdLineSyntax, header,
- options, defaultLeftPad, defaultDescPad, footer );
- pw.flush();
- }
-
- public void printHelp( PrintWriter pw,
- int width,
- String cmdLineSyntax,
- String header,
- Options options,
- int leftPad,
- int descPad,
- String footer )
- throws IllegalArgumentException
- {
- if ( cmdLineSyntax == null || cmdLineSyntax.length() == 0 )
- {
- throw new IllegalArgumentException("cmdLineSyntax not provided");
- }
-
- printUsage( pw, width, cmdLineSyntax );
- if ( header != null && header.trim().length() > 0 )
- {
- printWrapped( pw, width, header );
- }
- printOptions( pw, width, options, leftPad, descPad );
- if ( footer != null && footer.trim().length() > 0 )
- {
- printWrapped( pw, width, footer );
- }
- }
-
- public void printUsage( PrintWriter pw, int width, String cmdLineSyntax )
- {
- int argPos = cmdLineSyntax.indexOf(' ') + 1;
- printWrapped(pw, width, defaultSyntaxPrefix.length() + argPos,
- defaultSyntaxPrefix + cmdLineSyntax);
- }
-
- public void printOptions( PrintWriter pw, int width, Options options, int leftPad, int descPad )
- {
- StringBuffer sb = new StringBuffer();
- renderOptions(sb, width, options, leftPad, descPad);
- pw.println(sb.toString());
- }
-
- public void printWrapped( PrintWriter pw, int width, String text )
- {
- printWrapped(pw, width, 0, text);
- }
-
- public void printWrapped( PrintWriter pw, int width, int nextLineTabStop, String text )
- {
- StringBuffer sb = new StringBuffer(text.length());
- renderWrappedText(sb, width, nextLineTabStop, text);
- pw.println(sb.toString());
- }
-
- // --------------------------------------------------------------- Protected
-
- protected StringBuffer renderOptions( StringBuffer sb,
- int width,
- Options options,
- int leftPad,
- int descPad )
- {
- final String lpad = createPadding(leftPad);
- final String dpad = createPadding(descPad);
-
- //first create list containing only <lpad>-a,--aaa where -a is opt and --aaa is
- //long opt; in parallel look for the longest opt string
- //this list will be then used to sort options ascending
- int max = 0;
- StringBuffer optBuf;
- List prefixList = new ArrayList();
- Option option;
- for ( Iterator i = options.getOptions().iterator(); i.hasNext(); )
- {
- option = (Option) i.next();
- optBuf = new StringBuffer(8);
- optBuf.append(lpad).append(defaultOptPrefix).append(option.getOpt());
- if ( option.hasLongOpt() )
- {
- optBuf.append(',').append(defaultLongOptPrefix).append(option.getLongOpt());
- }
- if ( option.hasArg() )
- {
- //FIXME - should have a way to specify arg name per option
- optBuf.append(' ').append(defaultArgName);
- }
- prefixList.add(optBuf);
- max = optBuf.length() > max ? optBuf.length() : max;
- }
-
- //right pad the prefixes
- for ( Iterator i = prefixList.iterator(); i.hasNext(); )
- {
- optBuf = (StringBuffer) i.next();
- if ( optBuf.length() < max )
- {
- optBuf.append(createPadding(max-optBuf.length()));
- }
- optBuf.append(dpad);
- }
-
- //sort this list ascending
- Collections.sort(prefixList, new StringBufferComparator());
-
- //finally render options
- int nextLineTabStop = max + descPad;
- char opt;
- int optOffset = leftPad + defaultOptPrefix.length();
-
- for ( Iterator i = prefixList.iterator(); i.hasNext(); )
- {
- optBuf = (StringBuffer) i.next();
- opt = optBuf.charAt(optOffset);
- option = options.getOption(opt);
- renderWrappedText(sb, width, nextLineTabStop,
- optBuf.append(option.getDescription()).toString());
- if ( i.hasNext() )
- {
- sb.append(defaultNewLine);
- }
- }
-
- return sb;
- }
-
- protected StringBuffer renderWrappedText( StringBuffer sb,
- int width,
- int nextLineTabStop,
- String text )
- {
- int pos = findWrapPos( text, width, 0);
- if ( pos == -1 )
- {
- sb.append(rtrim(text));
- return sb;
- }
- else
- {
- sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
- }
-
- //all following lines must be padded with nextLineTabStop space characters
- final String padding = createPadding(nextLineTabStop);
-
- while ( true )
- {
- text = padding + text.substring(pos).trim();
- pos = findWrapPos( text, width, nextLineTabStop );
- if ( pos == -1 )
- {
- sb.append(text);
- return sb;
- }
-
- sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
- }
-
- }
-
- /**
- * Finds the next text wrap position after <code>startPos</code> for the text
- * in <code>sb</code> with the column width <code>width</code>.
- * The wrap point is the last postion before startPos+width having a whitespace
- * character (space, \n, \r).
- *
- * @param sb text to be analyzed
- * @param width width of the wrapped text
- * @param startPos position from which to start the lookup whitespace character
- * @return postion on which the text must be wrapped or -1 if the wrap position is at the end
- * of the text
- */
- protected int findWrapPos( String text, int width, int startPos )
- {
- int pos = -1;
- // the line ends before the max wrap pos or a new line char found
- if ( ((pos = text.indexOf('\n', startPos)) != -1 && pos <= width) ||
- ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width) )
- {
- return pos;
- }
- else if ( (startPos + width) >= text.length() )
- {
- return -1;
- }
-
- //look for the last whitespace character before startPos+width
- pos = startPos + width;
- char c;
- while ( pos >= startPos && (c = text.charAt(pos)) != ' ' && c != '\n' && c != '\r' )
- {
- --pos;
- }
- //if we found it - just return
- if ( pos > startPos )
- {
- return pos;
- }
- else
- {
- //must look for the first whitespace chearacter after startPos + width
- pos = startPos + width;
- while ( pos <= text.length() && (c = text.charAt(pos)) != ' ' && c != '\n' && c != '\r' )
- {
- ++pos;
- }
- return pos == text.length() ? -1 : pos;
- }
- }
-
- protected String createPadding(int len)
- {
- StringBuffer sb = new StringBuffer(len);
- for ( int i = 0; i < len; ++i )
- {
- sb.append(' ');
- }
- return sb.toString();
- }
-
- protected String rtrim( String s )
- {
- if ( s == null || s.length() == 0 )
- {
- return s;
- }
-
- int pos = s.length();
- while ( pos >= 0 && Character.isWhitespace(s.charAt(pos-1)) )
- {
- --pos;
- }
- return s.substring(0, pos);
- }
-
- // ------------------------------------------------------- Package protected
-
- // ----------------------------------------------------------------- Private
-
- // ----------------------------------------------------------- Inner classes
-
- private static class StringBufferComparator
- implements Comparator
- {
- public int compare( Object o1, Object o2 )
- {
- return ((StringBuffer) o1).toString().compareTo(((StringBuffer) o2).toString());
- }
- }
-}
+/*
+ * Copyright (C) The Apache Software Foundation. All rights reserved.
+ *
+ * This software is published under the terms of the Apache Software License
+ * version 1.1, a copy of which has been included with this distribution in
+ * the LICENSE file.
+ *
+ * $Id: HelpFormatter.java,v 1.2 2002/05/17 11:44:32 jstrachan Exp $
+ */
+package org.apache.commons.cli;
+
+import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+
+/**
+ * A formatter of help messages for the current command line options
+ *
+ * @author Slawek Zachcial
+ **/
+public class HelpFormatter
+{
+ // --------------------------------------------------------------- Constants
+
+ public static final int DEFAULT_WIDTH = 80;
+ public static final int DEFAULT_LEFT_PAD = 1;
+ public static final int DEFAULT_DESC_PAD = 3;
+ public static final String DEFAULT_SYNTAX_PREFIX = "usage: ";
+ public static final String DEFAULT_OPT_PREFIX = "-";
+ public static final String DEFAULT_LONG_OPT_PREFIX = "--";
+ public static final String DEFAULT_ARG_NAME = "arg";
+
+ // ------------------------------------------------------------------ Static
+
+ // -------------------------------------------------------------- Attributes
+
+ public int defaultWidth;
+ public int defaultLeftPad;
+ public int defaultDescPad;
+ public String defaultSyntaxPrefix;
+ public String defaultNewLine;
+ public String defaultOptPrefix;
+ public String defaultLongOptPrefix;
+ public String defaultArgName;
+
+ // ------------------------------------------------------------ Constructors
+ public HelpFormatter()
+ {
+ defaultWidth = DEFAULT_WIDTH;
+ defaultLeftPad = DEFAULT_LEFT_PAD;
+ defaultDescPad = DEFAULT_DESC_PAD;
+ defaultSyntaxPrefix = DEFAULT_SYNTAX_PREFIX;
+ defaultNewLine = System.getProperty("line.separator");
+ defaultOptPrefix = DEFAULT_OPT_PREFIX;
+ defaultLongOptPrefix = DEFAULT_LONG_OPT_PREFIX;
+ defaultArgName = DEFAULT_ARG_NAME;
+ }
+
+ // ------------------------------------------------------------------ Public
+
+ public void printHelp( String cmdLineSyntax,
+ Options options )
+ {
+ printHelp( defaultWidth, cmdLineSyntax, null, options, null );
+ }
+
+ public void printHelp( String cmdLineSyntax,
+ String header,
+ Options options,
+ String footer )
+ {
+ printHelp(defaultWidth, cmdLineSyntax, header, options, footer);
+ }
+
+ public void printHelp( int width,
+ String cmdLineSyntax,
+ String header,
+ Options options,
+ String footer )
+ {
+ PrintWriter pw = new PrintWriter(System.out);
+ printHelp( pw, width, cmdLineSyntax, header,
+ options, defaultLeftPad, defaultDescPad, footer );
+ pw.flush();
+ }
+
+ public void printHelp( PrintWriter pw,
+ int width,
+ String cmdLineSyntax,
+ String header,
+ Options options,
+ int leftPad,
+ int descPad,
+ String footer )
+ throws IllegalArgumentException
+ {
+ if ( cmdLineSyntax == null || cmdLineSyntax.length() == 0 )
+ {
+ throw new IllegalArgumentException("cmdLineSyntax not provided");
+ }
+
+ printUsage( pw, width, cmdLineSyntax );
+ if ( header != null && header.trim().length() > 0 )
+ {
+ printWrapped( pw, width, header );
+ }
+ printOptions( pw, width, options, leftPad, descPad );
+ if ( footer != null && footer.trim().length() > 0 )
+ {
+ printWrapped( pw, width, footer );
+ }
+ }
+
+ public void printUsage( PrintWriter pw, int width, String cmdLineSyntax )
+ {
+ int argPos = cmdLineSyntax.indexOf(' ') + 1;
+ printWrapped(pw, width, defaultSyntaxPrefix.length() + argPos,
+ defaultSyntaxPrefix + cmdLineSyntax);
+ }
+
+ public void printOptions( PrintWriter pw, int width, Options options, int leftPad, int descPad )
+ {
+ StringBuffer sb = new StringBuffer();
+ renderOptions(sb, width, options, leftPad, descPad);
+ pw.println(sb.toString());
+ }
+
+ public void printWrapped( PrintWriter pw, int width, String text )
+ {
+ printWrapped(pw, width, 0, text);
+ }
+
+ public void printWrapped( PrintWriter pw, int width, int nextLineTabStop, String text )
+ {
+ StringBuffer sb = new StringBuffer(text.length());
+ renderWrappedText(sb, width, nextLineTabStop, text);
+ pw.println(sb.toString());
+ }
+
+ // --------------------------------------------------------------- Protected
+
+ protected StringBuffer renderOptions( StringBuffer sb,
+ int width,
+ Options options,
+ int leftPad,
+ int descPad )
+ {
+ final String lpad = createPadding(leftPad);
+ final String dpad = createPadding(descPad);
+
+ //first create list containing only <lpad>-a,--aaa where -a is opt and --aaa is
+ //long opt; in parallel look for the longest opt string
+ //this list will be then used to sort options ascending
+ int max = 0;
+ StringBuffer optBuf;
+ List prefixList = new ArrayList();
+ Option option;
+ for ( Iterator i = options.getOptions().iterator(); i.hasNext(); )
+ {
+ option = (Option) i.next();
+ optBuf = new StringBuffer(8);
+ optBuf.append(lpad).append(defaultOptPrefix).append(option.getOpt());
+ if ( option.hasLongOpt() )
+ {
+ optBuf.append(',').append(defaultLongOptPrefix).append(option.getLongOpt());
+ }
+ if ( option.hasArg() )
+ {
+ //FIXME - should have a way to specify arg name per option
+ optBuf.append(' ').append(defaultArgName);
+ }
+ prefixList.add(optBuf);
+ max = optBuf.length() > max ? optBuf.length() : max;
+ }
+
+ //right pad the prefixes
+ for ( Iterator i = prefixList.iterator(); i.hasNext(); )
+ {
+ optBuf = (StringBuffer) i.next();
+ if ( optBuf.length() < max )
+ {
+ optBuf.append(createPadding(max-optBuf.length()));
+ }
+ optBuf.append(dpad);
+ }
+
+ //sort this list ascending
+ Collections.sort(prefixList, new StringBufferComparator());
+
+ //finally render options
+ int nextLineTabStop = max + descPad;
+ char opt;
+ int optOffset = leftPad + defaultOptPrefix.length();
+
+ for ( Iterator i = prefixList.iterator(); i.hasNext(); )
+ {
+ optBuf = (StringBuffer) i.next();
+ opt = optBuf.charAt(optOffset);
+ option = options.getOption(opt);
+ renderWrappedText(sb, width, nextLineTabStop,
+ optBuf.append(option.getDescription()).toString());
+ if ( i.hasNext() )
+ {
+ sb.append(defaultNewLine);
+ }
+ }
+
+ return sb;
+ }
+
+ protected StringBuffer renderWrappedText( StringBuffer sb,
+ int width,
+ int nextLineTabStop,
+ String text )
+ {
+ int pos = findWrapPos( text, width, 0);
+ if ( pos == -1 )
+ {
+ sb.append(rtrim(text));
+ return sb;
+ }
+ else
+ {
+ sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
+ }
+
+ //all following lines must be padded with nextLineTabStop space characters
+ final String padding = createPadding(nextLineTabStop);
+
+ while ( true )
+ {
+ text = padding + text.substring(pos).trim();
+ pos = findWrapPos( text, width, nextLineTabStop );
+ if ( pos == -1 )
+ {
+ sb.append(text);
+ return sb;
+ }
+
+ sb.append(rtrim(text.substring(0, pos))).append(defaultNewLine);
+ }
+
+ }
+
+ /**
+ * Finds the next text wrap position after <code>startPos</code> for the text
+ * in <code>sb</code> with the column width <code>width</code>.
+ * The wrap point is the last postion before startPos+width having a whitespace
+ * character (space, \n, \r).
+ *
+ * @param sb text to be analyzed
+ * @param width width of the wrapped text
+ * @param startPos position from which to start the lookup whitespace character
+ * @return postion on which the text must be wrapped or -1 if the wrap position is at the end
+ * of the text
+ */
+ protected int findWrapPos( String text, int width, int startPos )
+ {
+ int pos = -1;
+ // the line ends before the max wrap pos or a new line char found
+ if ( ((pos = text.indexOf('\n', startPos)) != -1 && pos <= width) ||
+ ((pos = text.indexOf('\t', startPos)) != -1 && pos <= width) )
+ {
+ return pos;
+ }
+ else if ( (startPos + width) >= text.length() )
+ {
+ return -1;
+ }
+
+ //look for the last whitespace character before startPos+width
+ pos = startPos + width;
+ char c;
+ while ( pos >= startPos && (c = text.charAt(pos)) != ' ' && c != '\n' && c != '\r' )
+ {
+ --pos;
+ }
+ //if we found it - just return
+ if ( pos > startPos )
+ {
+ return pos;
+ }
+ else
+ {
+ //must look for the first whitespace chearacter after startPos + width
+ pos = startPos + width;
+ while ( pos <= text.length() && (c = text.charAt(pos)) != ' ' && c != '\n' && c != '\r' )
+ {
+ ++pos;
+ }
+ return pos == text.length() ? -1 : pos;
+ }
+ }
+
+ protected String createPadding(int len)
+ {
+ StringBuffer sb = new StringBuffer(len);
+ for ( int i = 0; i < len; ++i )
+ {
+ sb.append(' ');
+ }
+ return sb.toString();
+ }
+
+ protected String rtrim( String s )
+ {
+ if ( s == null || s.length() == 0 )
+ {
+ return s;
+ }
+
+ int pos = s.length();
+ while ( pos >= 0 && Character.isWhitespace(s.charAt(pos-1)) )
+ {
+ --pos;
+ }
+ return s.substring(0, pos);
+ }
+
+ // ------------------------------------------------------- Package protected
+
+ // ----------------------------------------------------------------- Private
+
+ // ----------------------------------------------------------- Inner classes
+
+ private static class StringBufferComparator
+ implements Comparator
+ {
+ public int compare( Object o1, Object o2 )
+ {
+ return ((StringBuffer) o1).toString().compareTo(((StringBuffer) o2).toString());
+ }
+ }
+}
1.3 +46 -15 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Option.java
Index: Option.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Option.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Option.java 23 Apr 2002 16:08:02 -0000 1.2
+++ Option.java 17 May 2002 11:44:32 -0000 1.3
@@ -5,11 +5,13 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: Option.java,v 1.2 2002/04/23 16:08:02 jstrachan Exp $
+ * $Id: Option.java,v 1.3 2002/05/17 11:44:32 jstrachan Exp $
*/
package org.apache.commons.cli;
+import java.util.ArrayList;
+
/** <p>Describes a single command-line option. It maintains
* information regarding the short-name of the option, the long-name,
* if any exists, a flag indicating if an argument is required for
@@ -23,32 +25,41 @@
*
* @author bob mcwhirter (bob @ werken.com)
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class Option {
- private Character _opt = null;
- private String _longOpt = null;
- private boolean _hasArg = false;
- private String _description = null;
- private boolean _required = false;
+ private Character _opt = null;
+ private String _longOpt = null;
+ private boolean _hasArg = false;
+ private String _description = null;
+ private boolean _required = false;
+ private boolean _multipleArgs = false;
+ private ArrayList _values = new ArrayList();
+
public Option(char opt, boolean hasArg, String description) {
- this(opt, null, hasArg, description, false);
+ this(opt, null, hasArg, description, false, false);
}
public Option(char opt, String longOpt, boolean hasArg, String description) {
- this(opt, longOpt, hasArg, description, false);
+ this(opt, longOpt, hasArg, description, false, false );
+ }
+
+ public Option(char opt, String longOpt, boolean hasArg, String description,
+ boolean required ) {
+ this(opt, longOpt, hasArg, description, required, false );
}
public Option(char opt, String longOpt, boolean hasArg, String description,
- boolean required) {
- _opt = new Character( opt );
- _longOpt = longOpt;
- _hasArg = hasArg;
- _description = description;
- _required = required;
+ boolean required, boolean multipleArgs ) {
+ _opt = new Character( opt );
+ _longOpt = longOpt;
+ _hasArg = hasArg;
+ _description = description;
+ _required = required;
+ _multipleArgs = multipleArgs;
}
/** <p>Retrieve the single-character name of this Option</p>
@@ -104,6 +115,14 @@
return _required;
}
+ /** <p>Query to see if this Option can take multiple values</p>
+ *
+ * @return boolean flag indicating if multiple values are allowed
+ */
+ public boolean hasMultipleArgs() {
+ return _multipleArgs;
+ }
+
/** <p>Dump state, suitable for debugging.</p>
*
* @return Stringified form of this object
@@ -129,5 +148,17 @@
.append(" ]");
return buf.toString();
+ }
+
+ public void addValue( String value ) {
+ _values.add( value );
+ }
+
+ public String getValue() {
+ return _values.size()==0 ? null : (String)_values.get( 0 );
+ }
+
+ public String[] getValues() {
+ return _values.size()==0 ? null : (String[])_values.toArray(new String[]{});
}
}
1.2 +28 -28 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/overview.html
Index: overview.html
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/overview.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- overview.html 19 Dec 2001 18:16:25 -0000 1.1
+++ overview.html 17 May 2002 11:44:32 -0000 1.2
@@ -1,28 +1,28 @@
-
-<body>
-
- <p>Commons CLI -- version ##VERSION## (##QUALITY##)</p>
-
- <p>The commons-cli package aides in parsing command-line arguments.</p>
-
- <p>Allow command-line arguments to be parsed against a descriptor of
- valid options (long and short), potentially with arguments.</p>
-
- <p>command-line arguments may be of the typical <code>String[]</code>
- form, but also may be a <code>java.util.List</code>. Indexes allow
- for parsing only a portion of the command-line. Also, functionality
- for parsing the command-line in phases is built in, allowing for
- 'cvs-style' command-lines, where some global options are specified
- before a 'command' argument, and command-specific options are
- specified after the command argument:
-
- <code>
- <pre>
- myApp -p <port> command -p <printer>
- </pre>
- </code>
-
-
- <p>The homepage for the project is
- <a href="http://jakarta.apache.org/commons/">jakarta commons/</a>
-</body>
+
+<body>
+
+ <p>Commons CLI -- version ##VERSION## (##QUALITY##)</p>
+
+ <p>The commons-cli package aides in parsing command-line arguments.</p>
+
+ <p>Allow command-line arguments to be parsed against a descriptor of
+ valid options (long and short), potentially with arguments.</p>
+
+ <p>command-line arguments may be of the typical <code>String[]</code>
+ form, but also may be a <code>java.util.List</code>. Indexes allow
+ for parsing only a portion of the command-line. Also, functionality
+ for parsing the command-line in phases is built in, allowing for
+ 'cvs-style' command-lines, where some global options are specified
+ before a 'command' argument, and command-specific options are
+ specified after the command argument:
+
+ <code>
+ <pre>
+ myApp -p <port> command -p <printer>
+ </pre>
+ </code>
+
+
+ <p>The homepage for the project is
+ <a href="http://jakarta.apache.org/commons/">jakarta commons/</a>
+</body>
1.2 +6 -7 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/AlreadySelectedException.java
Index: AlreadySelectedException.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/AlreadySelectedException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- AlreadySelectedException.java 23 Apr 2002 16:08:02 -0000 1.1
+++ AlreadySelectedException.java 17 May 2002 11:44:32 -0000 1.2
@@ -5,24 +5,23 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: AlreadySelectedException.java,v 1.1 2002/04/23 16:08:02 jstrachan Exp $
+ * $Id: AlreadySelectedException.java,v 1.2 2002/05/17 11:44:32 jstrachan Exp $
*/
-
package org.apache.commons.cli;
/** <p>Exception thrown when more than one option in an option group
* has been provided.</p>
*
* @author John Keyes (john @ integralsource.com)
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
class AlreadySelectedException extends ParseException
{
- /** Construct a new Exception with a message
- *
- * @param msg Explanation of the exception
- */
+ /** Construct a new Exception with a message
+ *
+ * @param msg Explanation of the exception
+ */
public AlreadySelectedException( String message )
{
super( message );
1.2 +6 -6 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/package.html
Index: package.html
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/package.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- package.html 19 Dec 2001 18:16:25 -0000 1.1
+++ package.html 17 May 2002 11:44:32 -0000 1.2
@@ -1,6 +1,6 @@
-
-<body>
-
- <p>Commons CLI -- version ##VERSION## (##QUALITY##)</p>
-
-</body>
+
+<body>
+
+ <p>Commons CLI -- version ##VERSION## (##QUALITY##)</p>
+
+</body>
1.2 +17 -8 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/CommandLine.java
Index: CommandLine.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/CommandLine.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- CommandLine.java 19 Dec 2001 18:16:25 -0000 1.1
+++ CommandLine.java 17 May 2002 11:44:32 -0000 1.2
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: CommandLine.java,v 1.1 2001/12/19 18:16:25 jstrachan Exp $
+ * $Id: CommandLine.java,v 1.2 2002/05/17 11:44:32 jstrachan Exp $
*/
package org.apache.commons.cli;
@@ -29,7 +29,7 @@
*
* @author bob mcwhirter (bob @ werken.com)
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.1 $
+ * @version $Revision: 1.2 $
*/
public class CommandLine{
@@ -55,7 +55,13 @@
* @return Value of the argument if option is set, and has an argument, else null.
*/
public String getOptionValue(char opt) {
- return (String) _options.get( new Character(opt) );
+ String[] result = (String[])_options.get( new Character(opt) );
+ return result == null ? null : result[0];
+ }
+
+ public String[] getOptionValues(char opt) {
+ String[] result = (String[])_options.get( new Character(opt) );
+ return result == null ? null : result;
}
/** <p>Retrieve the argument, if any, of an option.</p>
@@ -108,12 +114,15 @@
}
void setOpt(char opt) {
- _options.put( new Character(opt),
- null );
+ _options.put( new Character(opt), null );
}
void setOpt(char opt, String value) {
- _options.put( new Character(opt),
- value );
- }
+ _options.put( new Character(opt), value );
+ }
+
+ void setOpt(Option opt) {
+ _options.put( new Character( opt.getOpt() ),
+ opt.getValues() );
+ }
}
1.3 +90 -77 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Options.java
Index: Options.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Options.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Options.java 23 Apr 2002 16:08:02 -0000 1.2
+++ Options.java 17 May 2002 11:44:32 -0000 1.3
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: Options.java,v 1.2 2002/04/23 16:08:02 jstrachan Exp $
+ * $Id: Options.java,v 1.3 2002/05/17 11:44:32 jstrachan Exp $
*/
package org.apache.commons.cli;
@@ -14,6 +14,7 @@
import java.util.Set;
import java.util.HashMap;
import java.util.List;
+import java.util.ListIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
@@ -33,7 +34,7 @@
*
* @author bob mcwhirter (bob @ werken.com)
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class Options {
@@ -98,6 +99,12 @@
return this;
}
+ public Options addOption(char opt, String longOpt, boolean hasArg, String description,
+ boolean required, boolean multipleArgs) {
+ addOption( new Option(opt, longOpt, hasArg, description, required, multipleArgs) );
+ return this;
+ }
+
/** <p>Parse the given list of arguments against this descriptor<p>
*
* @param args Args to parse
@@ -232,17 +239,17 @@
throws MissingArgumentException, UnrecognizedOptionException, MissingOptionException, AlreadySelectedException {
CommandLine cl = new CommandLine();
- List args = burst( inArgs,
- stopAtNonOption );
+ List args = burst( inArgs, stopAtNonOption );
- Iterator argIter = args.iterator();
+ ListIterator argIter = args.listIterator();
String eachArg = null;
Option eachOpt = null;
boolean eatTheRest = false;
-
+
while ( argIter.hasNext() ) {
+
eachArg = (String) argIter.next();
-
+
if ( eachArg.equals("--") ) {
// signalled end-of-opts. Eat the rest
@@ -250,40 +257,7 @@
}
else if ( eachArg.startsWith("--") ) {
eachOpt = (Option) _longOpts.get( eachArg );
-
- if ( eachOpt == null ) {
- throw new UnrecognizedOptionException("Unrecognized option: " + eachArg);
- // maybe someone will parse these args later
- // cl.addArg( eachArg );
- }
- else {
-
- if ( _optionGroups.get( eachOpt ) != null ) {
- ( (OptionGroup)( _optionGroups.get( eachOpt ) ) ).setSelected( eachOpt );
- }
-
- // if required remove from list
- if ( eachOpt.isRequired() ) {
- _requiredOpts.remove( "-" + eachOpt.getOpt() );
- }
-
- if ( eachOpt.hasArg() ) {
- if ( argIter.hasNext() ) {
- eachArg = (String) argIter.next();
-
- cl.setOpt( eachOpt.getOpt(),
- eachArg );
- }
- else {
- throw new MissingArgumentException( eachArg + " requires an argument.");
- }
-
- }
- else {
- cl.setOpt( eachOpt.getOpt() );
- }
- }
-
+ processOption( eachArg, eachOpt, argIter, cl );
}
else if ( eachArg.equals("-") ) {
// Just-another-argument
@@ -297,40 +271,10 @@
}
else if ( eachArg.startsWith("-") ) {
eachOpt = (Option) _shortOpts.get( eachArg );
-
- if ( _optionGroups.get( eachOpt ) != null ) {
- ( (OptionGroup)( _optionGroups.get( eachOpt ) ) ).setSelected( eachOpt );
- }
-
- if ( eachOpt == null ) {
- throw new UnrecognizedOptionException("Unrecognized option: " + eachArg);
- // maybe someone will parse these args later
- // cl.addArg( eachArg );
- }
- else {
- // if required remove from list
- if ( eachOpt.isRequired() ) {
- _requiredOpts.remove( "-" + eachOpt.getOpt() );
- }
-
- if ( eachOpt.hasArg() ) {
- if ( argIter.hasNext() ) {
- eachArg = (String) argIter.next();
-
- cl.setOpt( eachOpt.getOpt(), eachArg );
-
- }
- else {
- throw new MissingArgumentException( eachArg + " requires an argument.");
- }
-
- }
- else {
- cl.setOpt( eachOpt.getOpt() );
- }
- }
- }
- else {
+ processOption( eachArg, eachOpt, argIter, cl );
+ }
+ else
+ {
cl.addArg( eachArg );
if ( stopAtNonOption ) {
eatTheRest = true;
@@ -345,6 +289,15 @@
}
}
+ // this will throw a MissingOptionException
+ checkRequiredOptions();
+
+ return cl;
+ }
+
+ private void checkRequiredOptions()
+ throws MissingOptionException
+ {
if( _requiredOpts.size() > 0 )
{
Set optKeys = _requiredOpts.keySet();
@@ -364,9 +317,69 @@
throw new MissingOptionException( buff.toString() );
}
- return cl;
}
-
+
+ private void processOption( String eachArg, Option option, ListIterator argIter, CommandLine cl )
+ throws UnrecognizedOptionException, AlreadySelectedException, MissingArgumentException
+ {
+ if ( option == null ) {
+ throw new UnrecognizedOptionException("Unrecognized option: " + eachArg);
+ }
+ else {
+
+ if ( _optionGroups.get( option ) != null ) {
+ ( (OptionGroup)( _optionGroups.get( option ) ) ).setSelected( option );
+ }
+
+ // if required remove from list
+ if ( option.isRequired() ) {
+ _requiredOpts.remove( "-" + option.getOpt() );
+ }
+
+ if ( option.hasArg() ) {
+ if ( argIter.hasNext() ) {
+ eachArg = (String) argIter.next();
+ option.addValue( eachArg );
+
+ if( option.hasMultipleArgs() )
+ {
+ while( argIter.hasNext() )
+ {
+ eachArg = (String)argIter.next();
+ if( eachArg.startsWith("-") )
+ {
+ argIter.previous();
+ cl.setOpt( option );
+ break;
+ }
+ else
+ {
+ option.addValue( eachArg );
+ }
+ }
+ }
+ else
+ {
+ cl.setOpt( option );
+ return;
+ }
+ if( !argIter.hasNext() )
+ {
+ cl.setOpt( option );
+ }
+ }
+ else {
+ throw new MissingArgumentException( eachArg + " requires an argument.");
+ }
+
+ }
+ else {
+ //option.addValue( null );
+ cl.setOpt( option );
+ }
+ }
+ }
+
private List burst(List inArgs, boolean stopAtNonOption) {
List args = new LinkedList();
1.2 +163 -163 jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/TestHelpFormatter.java
Index: TestHelpFormatter.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/TestHelpFormatter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestHelpFormatter.java 1 Feb 2002 16:28:34 -0000 1.1
+++ TestHelpFormatter.java 17 May 2002 11:44:32 -0000 1.2
@@ -1,163 +1,163 @@
-/*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
- *
- * This software is published under the terms of the Apache Software License
- * version 1.1, a copy of which has been included with this distribution in
- * the LICENSE file.
- *
- * $Id: TestHelpFormatter.java,v 1.1 2002/02/01 16:28:34 jstrachan Exp $
- */
-package org.apache.commons.cli;
-
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-import java.io.StringWriter;
-import java.io.PrintWriter;
-
-/**
- * Test case for the HelpFormatter class
- *
- * @author Slawek Zachcial
- **/
-public class TestHelpFormatter
-extends TestCase
-{
- // --------------------------------------------------------------- Constants
-
- // ------------------------------------------------------------------ Static
-
- public static void main( String[] args )
- {
- String[] testName = { TestHelpFormatter.class.getName() };
- junit.textui.TestRunner.main(testName);
- }
-
- public static TestSuite suite()
- {
- return new TestSuite(TestHelpFormatter.class);
- }
-
- // -------------------------------------------------------------- Attributes
-
- // ------------------------------------------------------------ Constructors
- public TestHelpFormatter( String s )
- {
- super( s );
- }
- // ------------------------------------------------------------------ Public
-
- public void testFindWrapPos()
- throws Exception
- {
- HelpFormatter hf = new HelpFormatter();
-
- String text = "This is a test.";
- //text width should be max 8; the wrap postition is 7
- assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0));
- //starting from 8 must give -1 - the wrap pos is after end
- assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8));
- //if there is no a good position before width to make a wrapping look for the next one
- text = "aaaa aa";
- assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0));
- }
-
- public void testPrintWrapped()
- throws Exception
- {
- StringBuffer sb = new StringBuffer();
- HelpFormatter hf = new HelpFormatter();
-
- String text = "This is a test.";
- String expected;
-
- expected = "This is a" + hf.defaultNewLine + "test.";
- hf.renderWrappedText(sb, 12, 0, text);
- assertEquals("single line text", expected, sb.toString());
-
- sb.setLength(0);
- expected = "This is a" + hf.defaultNewLine + " test.";
- hf.renderWrappedText(sb, 12, 4, text);
- assertEquals("single line padded text", expected, sb.toString());
-
- text =
- "aaaa aaaa aaaa" + hf.defaultNewLine +
- "aaaaaa" + hf.defaultNewLine +
- "aaaaa";
-
- expected = text;
- sb.setLength(0);
- hf.renderWrappedText(sb, 16, 0, text);
- assertEquals("multi line text", expected, sb.toString());
-
- expected =
- "aaaa aaaa aaaa" + hf.defaultNewLine +
- " aaaaaa" + hf.defaultNewLine +
- " aaaaa";
- sb.setLength(0);
- hf.renderWrappedText(sb, 16, 4, text);
- assertEquals("multi-line padded text", expected, sb.toString());
- }
-
- public void testPrintOptions()
- throws Exception
- {
- StringBuffer sb = new StringBuffer();
- HelpFormatter hf = new HelpFormatter();
- final int leftPad = 1;
- final int descPad = 3;
- final String lpad = hf.createPadding(leftPad);
- final String dpad = hf.createPadding(descPad);
- Options options = null;
- String expected = null;
-
- options = new Options().addOption('a', false, "aaaa aaaa aaaa aaaa aaaa");
- expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";
- hf.renderOptions(sb, 60, options, leftPad, descPad);
- assertEquals("simple non-wrapped option", expected, sb.toString());
-
- int nextLineTabStop = leftPad+descPad+"-a".length();
- expected =
- lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.defaultNewLine +
- hf.createPadding(nextLineTabStop) + "aaaa aaaa";
- sb.setLength(0);
- hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad);
- assertEquals("simple wrapped option", expected, sb.toString());
-
-
- options = new Options().addOption('a', "aaa", false, "dddd dddd dddd dddd");
- expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
- sb.setLength(0);
- hf.renderOptions(sb, 60, options, leftPad, descPad);
- assertEquals("long non-wrapped option", expected, sb.toString());
-
- nextLineTabStop = leftPad+descPad+"-a,--aaa".length();
- expected =
- lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
- hf.createPadding(nextLineTabStop) + "dddd dddd";
- sb.setLength(0);
- hf.renderOptions(sb, 25, options, leftPad, descPad);
- assertEquals("long wrapped option", expected, sb.toString());
-
- options = new Options().
- addOption('a', "aaa", false, "dddd dddd dddd dddd").
- addOption('b', false, "feeee eeee eeee eeee");
- expected =
- lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
- hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.defaultNewLine +
- lpad + "-b " + dpad + "feeee eeee" + hf.defaultNewLine +
- hf.createPadding(nextLineTabStop) + "eeee eeee";
- sb.setLength(0);
- hf.renderOptions(sb, 25, options, leftPad, descPad);
- assertEquals("multiple wrapped options", expected, sb.toString());
- }
-
- // --------------------------------------------------------------- Protected
-
- // ------------------------------------------------------- Package protected
-
- // ----------------------------------------------------------------- Private
-
- // ----------------------------------------------------------- Inner classes
-
-}
+/*
+ * Copyright (C) The Apache Software Foundation. All rights reserved.
+ *
+ * This software is published under the terms of the Apache Software License
+ * version 1.1, a copy of which has been included with this distribution in
+ * the LICENSE file.
+ *
+ * $Id: TestHelpFormatter.java,v 1.2 2002/05/17 11:44:32 jstrachan Exp $
+ */
+package org.apache.commons.cli;
+
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import java.io.StringWriter;
+import java.io.PrintWriter;
+
+/**
+ * Test case for the HelpFormatter class
+ *
+ * @author Slawek Zachcial
+ **/
+public class TestHelpFormatter
+extends TestCase
+{
+ // --------------------------------------------------------------- Constants
+
+ // ------------------------------------------------------------------ Static
+
+ public static void main( String[] args )
+ {
+ String[] testName = { TestHelpFormatter.class.getName() };
+ junit.textui.TestRunner.main(testName);
+ }
+
+ public static TestSuite suite()
+ {
+ return new TestSuite(TestHelpFormatter.class);
+ }
+
+ // -------------------------------------------------------------- Attributes
+
+ // ------------------------------------------------------------ Constructors
+ public TestHelpFormatter( String s )
+ {
+ super( s );
+ }
+ // ------------------------------------------------------------------ Public
+
+ public void testFindWrapPos()
+ throws Exception
+ {
+ HelpFormatter hf = new HelpFormatter();
+
+ String text = "This is a test.";
+ //text width should be max 8; the wrap postition is 7
+ assertEquals("wrap position", 7, hf.findWrapPos(text, 8, 0));
+ //starting from 8 must give -1 - the wrap pos is after end
+ assertEquals("wrap position 2", -1, hf.findWrapPos(text, 8, 8));
+ //if there is no a good position before width to make a wrapping look for the next one
+ text = "aaaa aa";
+ assertEquals("wrap position 3", 4, hf.findWrapPos(text, 3, 0));
+ }
+
+ public void testPrintWrapped()
+ throws Exception
+ {
+ StringBuffer sb = new StringBuffer();
+ HelpFormatter hf = new HelpFormatter();
+
+ String text = "This is a test.";
+ String expected;
+
+ expected = "This is a" + hf.defaultNewLine + "test.";
+ hf.renderWrappedText(sb, 12, 0, text);
+ assertEquals("single line text", expected, sb.toString());
+
+ sb.setLength(0);
+ expected = "This is a" + hf.defaultNewLine + " test.";
+ hf.renderWrappedText(sb, 12, 4, text);
+ assertEquals("single line padded text", expected, sb.toString());
+
+ text =
+ "aaaa aaaa aaaa" + hf.defaultNewLine +
+ "aaaaaa" + hf.defaultNewLine +
+ "aaaaa";
+
+ expected = text;
+ sb.setLength(0);
+ hf.renderWrappedText(sb, 16, 0, text);
+ assertEquals("multi line text", expected, sb.toString());
+
+ expected =
+ "aaaa aaaa aaaa" + hf.defaultNewLine +
+ " aaaaaa" + hf.defaultNewLine +
+ " aaaaa";
+ sb.setLength(0);
+ hf.renderWrappedText(sb, 16, 4, text);
+ assertEquals("multi-line padded text", expected, sb.toString());
+ }
+
+ public void testPrintOptions()
+ throws Exception
+ {
+ StringBuffer sb = new StringBuffer();
+ HelpFormatter hf = new HelpFormatter();
+ final int leftPad = 1;
+ final int descPad = 3;
+ final String lpad = hf.createPadding(leftPad);
+ final String dpad = hf.createPadding(descPad);
+ Options options = null;
+ String expected = null;
+
+ options = new Options().addOption('a', false, "aaaa aaaa aaaa aaaa aaaa");
+ expected = lpad + "-a" + dpad + "aaaa aaaa aaaa aaaa aaaa";
+ hf.renderOptions(sb, 60, options, leftPad, descPad);
+ assertEquals("simple non-wrapped option", expected, sb.toString());
+
+ int nextLineTabStop = leftPad+descPad+"-a".length();
+ expected =
+ lpad + "-a" + dpad + "aaaa aaaa aaaa" + hf.defaultNewLine +
+ hf.createPadding(nextLineTabStop) + "aaaa aaaa";
+ sb.setLength(0);
+ hf.renderOptions(sb, nextLineTabStop+17, options, leftPad, descPad);
+ assertEquals("simple wrapped option", expected, sb.toString());
+
+
+ options = new Options().addOption('a', "aaa", false, "dddd dddd dddd dddd");
+ expected = lpad + "-a,--aaa" + dpad + "dddd dddd dddd dddd";
+ sb.setLength(0);
+ hf.renderOptions(sb, 60, options, leftPad, descPad);
+ assertEquals("long non-wrapped option", expected, sb.toString());
+
+ nextLineTabStop = leftPad+descPad+"-a,--aaa".length();
+ expected =
+ lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
+ hf.createPadding(nextLineTabStop) + "dddd dddd";
+ sb.setLength(0);
+ hf.renderOptions(sb, 25, options, leftPad, descPad);
+ assertEquals("long wrapped option", expected, sb.toString());
+
+ options = new Options().
+ addOption('a', "aaa", false, "dddd dddd dddd dddd").
+ addOption('b', false, "feeee eeee eeee eeee");
+ expected =
+ lpad + "-a,--aaa" + dpad + "dddd dddd" + hf.defaultNewLine +
+ hf.createPadding(nextLineTabStop) + "dddd dddd" + hf.defaultNewLine +
+ lpad + "-b " + dpad + "feeee eeee" + hf.defaultNewLine +
+ hf.createPadding(nextLineTabStop) + "eeee eeee";
+ sb.setLength(0);
+ hf.renderOptions(sb, 25, options, leftPad, descPad);
+ assertEquals("multiple wrapped options", expected, sb.toString());
+ }
+
+ // --------------------------------------------------------------- Protected
+
+ // ------------------------------------------------------- Package protected
+
+ // ----------------------------------------------------------------- Private
+
+ // ----------------------------------------------------------- Inner classes
+
+}
1.2 +106 -106 jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/HelpFormatterExamples.java
Index: HelpFormatterExamples.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/HelpFormatterExamples.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- HelpFormatterExamples.java 1 Feb 2002 16:28:34 -0000 1.1
+++ HelpFormatterExamples.java 17 May 2002 11:44:32 -0000 1.2
@@ -1,106 +1,106 @@
-/*
- * Copyright (C) The Apache Software Foundation. All rights reserved.
- *
- * This software is published under the terms of the Apache Software License
- * version 1.1, a copy of which has been included with this distribution in
- * the LICENSE file.
- *
- * $Id: HelpFormatterExamples.java,v 1.1 2002/02/01 16:28:34 jstrachan Exp $
- */
-package org.apache.commons.cli;
-
-/**
- * A sample program shpwing the use of Options and the HelpFormatter class
- *
- * @author Slawek Zachcial
- **/
-public class HelpFormatterExamples
-{
- // --------------------------------------------------------------- Constants
-
- // ------------------------------------------------------------------ Static
-
- public static void main( String[] args )
- {
- System.out.println("\n#\n# 'man' example\n#");
- manExample();
-/*
- System.out.println("\n#\n# 'bzip2' example\n#");
- bzip2Example();
- System.out.println("\n#\n# 'ls' example\n#");
- lsExample();
-*/
- }
-
- static void manExample()
- {
- String cmdLine =
- "man [-c|-f|-k|-w|-tZT device] [-adlhu7V] [-Mpath] [-Ppager] [-Slist] " +
- "[-msystem] [-pstring] [-Llocale] [-eextension] [section] page ...";
- Options opts =
- new Options().
- addOption('a', "all", false, "find all matching manual pages.").
- addOption('d', "debug", false, "emit debugging messages.").
- addOption('e', "extension", false, "limit search to extension type 'extension'.").
- addOption('f', "whatis", false, "equivalent to whatis.").
- addOption('k', "apropos", false, "equivalent to apropos.").
- addOption('w', "location", false, "print physical location of man page(s).").
- addOption('l', "local-file", false, "interpret 'page' argument(s) as local filename(s)").
- addOption('u', "update", false, "force a cache consistency check.").
- //FIXME - should generate -r,--prompt string
- addOption('r', "prompt", true, "provide 'less' pager with prompt.").
- addOption('c', "catman", false, "used by catman to reformat out of date cat pages.").
- addOption('7', "ascii", false, "display ASCII translation or certain latin1 chars.").
- addOption('t', "troff", false, "use troff format pages.").
- //FIXME - should generate -T,--troff-device device
- addOption('T', "troff-device", true, "use groff with selected device.").
- addOption('Z', "ditroff", false, "use groff with selected device.").
- addOption('D', "default", false, "reset all options to their default values.").
- //FIXME - should generate -M,--manpath path
- addOption('M', "manpath", true, "set search path for manual pages to 'path'.").
- //FIXME - should generate -P,--pager pager
- addOption('P', "pager", true, "use program 'pager' to display output.").
- //FIXME - should generate -S,--sections list
- addOption('S', "sections", true, "use colon separated section list.").
- //FIXME - should generate -m,--systems system
- addOption('m', "systems", true, "search for man pages from other unix system(s).").
- //FIXME - should generate -L,--locale locale
- addOption('L', "locale", true, "defaine the locale for this particular man search.").
- //FIXME - should generate -p,--preprocessor string
- addOption('p', "preprocessor", true, "string indicates which preprocessor to run.\n" +
- " e - [n]eqn p - pic t - tbl\n" +
- " g - grap r - refer v - vgrind").
- addOption('V', "version", false, "show version.").
- addOption('h', "help", false, "show this usage message.");
-
- HelpFormatter hf = new HelpFormatter();
- //hf.printHelp(cmdLine, opts);
- hf.printHelp(60, cmdLine, null, opts, null);
- }
-
- static void bzip2Example()
- {
- System.out.println( "Coming soon" );
- }
-
- static void lsExample()
- {
- System.out.println( "Coming soon" );
- }
-
-
- // -------------------------------------------------------------- Attributes
-
- // ------------------------------------------------------------ Constructors
-
- // ------------------------------------------------------------------ Public
-
- // --------------------------------------------------------------- Protected
-
- // ------------------------------------------------------- Package protected
-
- // ----------------------------------------------------------------- Private
-
- // ----------------------------------------------------------- Inner classes
-
-}
+/*
+ * Copyright (C) The Apache Software Foundation. All rights reserved.
+ *
+ * This software is published under the terms of the Apache Software License
+ * version 1.1, a copy of which has been included with this distribution in
+ * the LICENSE file.
+ *
+ * $Id: HelpFormatterExamples.java,v 1.2 2002/05/17 11:44:32 jstrachan Exp $
+ */
+package org.apache.commons.cli;
+
+/**
+ * A sample program shpwing the use of Options and the HelpFormatter class
+ *
+ * @author Slawek Zachcial
+ **/
+public class HelpFormatterExamples
+{
+ // --------------------------------------------------------------- Constants
+
+ // ------------------------------------------------------------------ Static
+
+ public static void main( String[] args )
+ {
+ System.out.println("\n#\n# 'man' example\n#");
+ manExample();
+/*
+ System.out.println("\n#\n# 'bzip2' example\n#");
+ bzip2Example();
+ System.out.println("\n#\n# 'ls' example\n#");
+ lsExample();
+*/
+ }
+
+ static void manExample()
+ {
+ String cmdLine =
+ "man [-c|-f|-k|-w|-tZT device] [-adlhu7V] [-Mpath] [-Ppager] [-Slist] " +
+ "[-msystem] [-pstring] [-Llocale] [-eextension] [section] page ...";
+ Options opts =
+ new Options().
+ addOption('a', "all", false, "find all matching manual pages.").
+ addOption('d', "debug", false, "emit debugging messages.").
+ addOption('e', "extension", false, "limit search to extension type 'extension'.").
+ addOption('f', "whatis", false, "equivalent to whatis.").
+ addOption('k', "apropos", false, "equivalent to apropos.").
+ addOption('w', "location", false, "print physical location of man page(s).").
+ addOption('l', "local-file", false, "interpret 'page' argument(s) as local filename(s)").
+ addOption('u', "update", false, "force a cache consistency check.").
+ //FIXME - should generate -r,--prompt string
+ addOption('r', "prompt", true, "provide 'less' pager with prompt.").
+ addOption('c', "catman", false, "used by catman to reformat out of date cat pages.").
+ addOption('7', "ascii", false, "display ASCII translation or certain latin1 chars.").
+ addOption('t', "troff", false, "use troff format pages.").
+ //FIXME - should generate -T,--troff-device device
+ addOption('T', "troff-device", true, "use groff with selected device.").
+ addOption('Z', "ditroff", false, "use groff with selected device.").
+ addOption('D', "default", false, "reset all options to their default values.").
+ //FIXME - should generate -M,--manpath path
+ addOption('M', "manpath", true, "set search path for manual pages to 'path'.").
+ //FIXME - should generate -P,--pager pager
+ addOption('P', "pager", true, "use program 'pager' to display output.").
+ //FIXME - should generate -S,--sections list
+ addOption('S', "sections", true, "use colon separated section list.").
+ //FIXME - should generate -m,--systems system
+ addOption('m', "systems", true, "search for man pages from other unix system(s).").
+ //FIXME - should generate -L,--locale locale
+ addOption('L', "locale", true, "defaine the locale for this particular man search.").
+ //FIXME - should generate -p,--preprocessor string
+ addOption('p', "preprocessor", true, "string indicates which preprocessor to run.\n" +
+ " e - [n]eqn p - pic t - tbl\n" +
+ " g - grap r - refer v - vgrind").
+ addOption('V', "version", false, "show version.").
+ addOption('h', "help", false, "show this usage message.");
+
+ HelpFormatter hf = new HelpFormatter();
+ //hf.printHelp(cmdLine, opts);
+ hf.printHelp(60, cmdLine, null, opts, null);
+ }
+
+ static void bzip2Example()
+ {
+ System.out.println( "Coming soon" );
+ }
+
+ static void lsExample()
+ {
+ System.out.println( "Coming soon" );
+ }
+
+
+ // -------------------------------------------------------------- Attributes
+
+ // ------------------------------------------------------------ Constructors
+
+ // ------------------------------------------------------------------ Public
+
+ // --------------------------------------------------------------- Protected
+
+ // ------------------------------------------------------- Package protected
+
+ // ----------------------------------------------------------------- Private
+
+ // ----------------------------------------------------------- Inner classes
+
+}
1.4 +2 -1 jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/AllTest.java
Index: AllTest.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/AllTest.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- AllTest.java 23 Apr 2002 16:08:02 -0000 1.3
+++ AllTest.java 17 May 2002 11:44:32 -0000 1.4
@@ -5,7 +5,7 @@
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
- * $Id: AllTest.java,v 1.3 2002/04/23 16:08:02 jstrachan Exp $
+ * $Id: AllTest.java,v 1.4 2002/05/17 11:44:32 jstrachan Exp $
*/
package org.apache.commons.cli;
@@ -26,6 +26,7 @@
suite.addTest(BuildTest.suite());
suite.addTest(ValueTest.suite());
+ suite.addTest(ValuesTest.suite());
suite.addTest(ParseTest.suite());
suite.addTest(ParseRequiredTest.suite());
suite.addTest(OptionGroupTest.suite());
1.1 jakarta-commons-sandbox/cli/src/test/org/apache/commons/cli/ValuesTest.java
Index: ValuesTest.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*
* $Id: ValueTest.java,v 1.1 2001/12/19 18:16:25 jstrachan Exp $
*/
package org.apache.commons.cli;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class ValuesTest extends TestCase
{
public static Test suite() {
return new TestSuite(ValuesTest.class);
/*
TestSuite suite = new TestSuite();
suite.addTest( new ValueTest("testLongNoArg") );
return suite;
*/
}
private CommandLine _cl = null;
public ValuesTest(String name)
{
super(name);
}
public void setUp()
{
Options opts = new Options();
opts.addOption('a',
false,
"toggle -a");
opts.addOption('b',
true,
"set -b");
opts.addOption('c',
"c",
false,
"toggle -c");
opts.addOption('d',
"d",
true,
"set -d");
opts.addOption('e',
"e",
true,
"set -e",
false,
true);
opts.addOption('f',
"f",
false,
"jk");
String[] args = new String[] { "-a",
"-b", "foo",
"--c",
"--d", "bar",
"-e", "one", "two",
"-f",
"arg1", "arg2" };
try
{
_cl = opts.parse(args);
}
catch (ParseException e)
{
fail("Cannot setUp() CommandLine: " + e.toString());
}
}
public void tearDown()
{
}
public void testShortArgs()
{
assertTrue( _cl.hasOption('a') );
assertTrue( _cl.hasOption('c') );
assertNull( _cl.getOptionValues('a') );
assertNull( _cl.getOptionValues('c') );
}
public void testShortArgsWithValue()
{
assertTrue( _cl.hasOption('b') );
assertTrue( _cl.getOptionValue('b').equals("foo"));
assertTrue( _cl.getOptionValues('b').length == 1);
assertTrue( _cl.hasOption('d') );
assertTrue( _cl.getOptionValue('d').equals("bar"));
assertTrue( _cl.getOptionValues('d').length == 1);
}
public void testMultipleArgValues()
{
String[] result = _cl.getOptionValues('e');
String[] values = new String[] { "one", "two" };
assertTrue( _cl.hasOption('e') );
assertTrue( _cl.getOptionValues('e').length == 2);
assertTrue( java.util.Arrays.equals( values, _cl.getOptionValues('e') ) );
}
public void testExtraArgs()
{
String[] args = new String[] { "arg1", "arg2" };
assertTrue( _cl.getArgs().length == 2);
assertTrue( java.util.Arrays.equals( args, _cl.getArgs() ) );
}
}
1.2 +6 -6 jakarta-commons-sandbox/cli/src/conf/MANIFEST.MF
Index: MANIFEST.MF
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/cli/src/conf/MANIFEST.MF,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MANIFEST.MF 19 Dec 2001 18:16:25 -0000 1.1
+++ MANIFEST.MF 17 May 2002 11:44:32 -0000 1.2
@@ -1,6 +1,6 @@
-Extension-Name: org.apache.commons.cli
-Specification-Vendor: Apache Software Foundation
-Specification-Version: 1.0
-Implementation-Vendor: Apache Software Foundation
-Implementation-Version: 1.0-dev
-
+Extension-Name: org.apache.commons.cli
+Specification-Vendor: Apache Software Foundation
+Specification-Version: 1.0
+Implementation-Vendor: Apache Software Foundation
+Implementation-Version: 1.0-dev
+
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>