Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@apache.org Received: (qmail 14133 invoked from network); 6 Jun 2002 22:32:47 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 6 Jun 2002 22:32:47 -0000 Received: (qmail 5991 invoked by uid 97); 6 Jun 2002 22:32:49 -0000 Delivered-To: qmlist-jakarta-archive-commons-dev@jakarta.apache.org Received: (qmail 5973 invoked by uid 97); 6 Jun 2002 22:32:48 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 5877 invoked by uid 97); 6 Jun 2002 22:32:48 -0000 X-Antivirus: nagoya (v4198 created Apr 24 2002) Date: 6 Jun 2002 22:32:38 -0000 Message-ID: <20020606223238.59276.qmail@icarus.apache.org> From: bayard@apache.org To: jakarta-commons-sandbox-cvs@apache.org Subject: cvs commit: jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli TypeHandler.java PatternOptionBuilder.java Option.java Options.java CommandLine.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N bayard 2002/06/06 15:32:38 Modified: cli/src/java/org/apache/commons/cli Option.java Options.java CommandLine.java Added: cli/src/java/org/apache/commons/cli TypeHandler.java PatternOptionBuilder.java Log: Added the concept of a type to an Option so it will do auto-conversion for the user. Also added a pattern builder a la perl so we don't have to do the whole object tree thing. This does mean people won't do descriptions as much though, which could be a bad thing. These additions are not expected to be perfect, I'd like to redesign a touch to use an OptionType class instead of just having the Class 'constants' in PatternOptionBuilder, I'd like the OptionType concept to be pluggable, both in the pattern concept and in the main Option code and I'd like to have a plug-in that handles Globbing of files from ORO. It also needs to switch to use ConvertUtils from Commons.Beans to handle conversions in TypeHandler and needs to add support for converting to Dates. Obtained from: GenerationJava com.generationjava.util.MapCArgs. Revision Changes Path 1.5 +50 -33 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.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- Option.java 6 Jun 2002 09:37:26 -0000 1.4 +++ Option.java 6 Jun 2002 22:32:37 -0000 1.5 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Option.java,v 1.4 2002/06/06 09:37:26 jstrachan Exp $ - * $Revision: 1.4 $ - * $Date: 2002/06/06 09:37:26 $ + * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Option.java,v 1.5 2002/06/06 22:32:37 bayard Exp $ + * $Revision: 1.5 $ + * $Date: 2002/06/06 22:32:37 $ * * ==================================================================== * @@ -66,7 +66,7 @@ * version 1.1, a copy of which has been included with this distribution in * the LICENSE file. * - * $Id: Option.java,v 1.4 2002/06/06 09:37:26 jstrachan Exp $ + * $Id: Option.java,v 1.5 2002/06/06 22:32:37 bayard Exp $ */ package org.apache.commons.cli; @@ -86,30 +86,33 @@ * * @author bob mcwhirter (bob @ werken.com) * @author James Strachan - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ */ public class Option { /** opt the single character representation of the option */ - private Character _opt = null; + private Character opt = null; /** longOpt is the long representation of the option */ - private String _longOpt = null; + private String longOpt = null; /** hasArg specifies whether this option has an associated argument */ - private boolean _hasArg = false; + private boolean hasArg = false; /** description of the option */ - private String _description = null; + private String description = null; /** required specifies whether this option is required to be present */ - private boolean _required = false; + private boolean required = false; /** multipleArgs specifies whether this option has multiple argument values */ - private boolean _multipleArgs = false; + private boolean multipleArgs = false; - /** ?? */ + /** the type of this Option */ + private Object type = null; + + /** ?? **/ private ArrayList values = new ArrayList(); @@ -163,12 +166,17 @@ */ public Option(char opt, String longOpt, boolean hasArg, String description, boolean required, boolean multipleArgs ) { - this._opt = new Character( opt ); - this._longOpt = longOpt; - this._hasArg = hasArg; - this._description = description; - this._required = required; - this._multipleArgs = multipleArgs; + this(opt, longOpt, hasArg, description, required, false, null ); + } + public Option(char opt, String longOpt, boolean hasArg, String description, + boolean required, boolean multipleArgs, Object type ) { + this.opt = new Character( opt ); + this.longOpt = longOpt; + this.hasArg = hasArg; + this.description = description; + this.required = required; + this.multipleArgs = multipleArgs; + this.type = type; } /**

Retrieve the single-character name of this Option

@@ -181,7 +189,11 @@ * @return Single character name of this option */ public char getOpt() { - return _opt.charValue(); + return this.opt.charValue(); + } + + public Object getType() { + return this.type; } /**

Retrieve the long name of this Option

@@ -189,7 +201,7 @@ * @return Long name of this option, or null, if there is no long name */ public String getLongOpt() { - return _longOpt; + return this.longOpt; } /**

Query to see if this Option has a long name

@@ -197,7 +209,7 @@ * @return boolean flag indicating existence of a long name */ public boolean hasLongOpt() { - return ( _longOpt != null ); + return ( this.longOpt != null ); } /**

Query to see if this Option requires an argument

@@ -205,7 +217,7 @@ * @return boolean flag indicating if an argument is required */ public boolean hasArg() { - return _hasArg; + return this.hasArg; } /**

Retrieve the self-documenting description of this Option

@@ -213,7 +225,7 @@ * @return The string description of this option */ public String getDescription() { - return _description; + return this.description; } /**

Query to see if this Option requires an argument

@@ -221,7 +233,7 @@ * @return boolean flag indicating if an argument is required */ public boolean isRequired() { - return _required; + return this.required; } /**

Query to see if this Option can take multiple values

@@ -229,7 +241,7 @@ * @return boolean flag indicating if multiple values are allowed */ public boolean hasMultipleArgs() { - return _multipleArgs; + return this.multipleArgs; } /**

Dump state, suitable for debugging.

@@ -239,23 +251,28 @@ public String toString() { StringBuffer buf = new StringBuffer().append("[ option: "); - buf.append( _opt ); + buf.append( this.opt ); - if ( _longOpt != null ) { + if ( this.longOpt != null ) { buf.append(" ") - .append(_longOpt); + .append(this.longOpt); } buf.append(" "); - if ( _hasArg ) { + if ( hasArg ) { buf.append( "+ARG" ); } buf.append(" :: ") - .append( _description ) - .append(" ]"); + .append( this.description ); + if ( this.type != null ) { + buf.append(" :: ") + .append( this.type ); + } + + buf.append(" ]"); return buf.toString(); } @@ -265,7 +282,7 @@ * @param value is a/the value of this Option */ public void addValue( String value ) { - values.add( value ); + this.values.add( value ); } /** @@ -273,7 +290,7 @@ * values */ public String getValue() { - return values.size()==0 ? null : (String)values.get( 0 ); + return this.values.size()==0 ? null : (String)this.values.get( 0 ); } /** @@ -281,6 +298,6 @@ * values */ public String[] getValues() { - return values.size()==0 ? null : (String[])values.toArray(new String[]{}); + return this.values.size()==0 ? null : (String[])this.values.toArray(new String[]{}); } } 1.5 +9 -3 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.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- Options.java 6 Jun 2002 09:37:26 -0000 1.4 +++ Options.java 6 Jun 2002 22:32:37 -0000 1.5 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Options.java,v 1.4 2002/06/06 09:37:26 jstrachan Exp $ - * $Revision: 1.4 $ - * $Date: 2002/06/06 09:37:26 $ + * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/Options.java,v 1.5 2002/06/06 22:32:37 bayard Exp $ + * $Revision: 1.5 $ + * $Date: 2002/06/06 22:32:37 $ * * ==================================================================== * @@ -84,7 +84,7 @@ * * @author bob mcwhirter (bob @ werken.com) * @author James Strachan - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ */ public class Options { @@ -183,6 +183,12 @@ 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; + } + + public Options addOption(char opt, String longOpt, boolean hasArg, String description, + boolean required, boolean multipleArgs, Object type) { + addOption( new Option(opt, longOpt, hasArg, description, required, multipleArgs, type) ); return this; } 1.4 +21 -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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- CommandLine.java 6 Jun 2002 09:37:26 -0000 1.3 +++ CommandLine.java 6 Jun 2002 22:32:37 -0000 1.4 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/CommandLine.java,v 1.3 2002/06/06 09:37:26 jstrachan Exp $ - * $Revision: 1.3 $ - * $Date: 2002/06/06 09:37:26 $ + * $Header: /home/cvs/jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/CommandLine.java,v 1.4 2002/06/06 22:32:37 bayard Exp $ + * $Revision: 1.4 $ + * $Date: 2002/06/06 22:32:37 $ * * ==================================================================== * @@ -78,9 +78,8 @@ * * @author bob mcwhirter (bob @ werken.com) * @author James Strachan - * @version $Revision: 1.3 $ + * @version $Revision: 1.4 $ */ - public class CommandLine { /** the unrecognised options/arguments */ @@ -89,6 +88,9 @@ /** the recognised options/arguments */ private Map options = new HashMap(); + /** the option types */ + private Map types = new HashMap(); + /** *

Creates a command line.

*/ @@ -103,7 +105,17 @@ public boolean hasOption(char opt) { return options.containsKey( new Character(opt) ); } - + + public Object getOptionObject(char opt) { + String[] result = (String[])options.get( new Character(opt) ); + Object type = types.get( new Character(opt) ); + String res = result == null ? null : result[0]; + if(res == null) { + return null; + } + return TypeHandler.createValue(res, type); + } + /**

Retrieve the argument, if any, of an option.

* * @param opt Short single-character name of the option @@ -206,7 +218,8 @@ * @param opt the processed option */ void setOpt(Option opt) { - options.put( new Character( opt.getOpt() ), - opt.getValues() ); + Character chr = new Character( opt.getOpt() ); + options.put( chr, opt.getValues() ); + types.put( chr, opt.getType() ); } } 1.1 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/TypeHandler.java Index: TypeHandler.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: TypeHandler.java,v 1.1 2002/06/06 22:32:37 bayard Exp $ */ package org.apache.commons.cli; import java.io.File; import java.net.URL; import java.net.MalformedURLException; import java.util.Date; import org.apache.commons.lang.Numbers; /** * This is a temporary implementation. TypeHandler will handle the * pluggableness of OptionTypes and it will direct all of these types * of conversion functionalities to ConvertUtils component in Commons * alreayd. BeanUtils I think. * * @author Henri Yandell (bayard @ generationjava.com) * @version $Revision: 1.1 $ */ public class TypeHandler { static public Object createValue(String str, Object obj) { return createValue(str, (Class)obj); } static public Object createValue(String str, Class clazz) { if( PatternOptionBuilder.STRING_VALUE == clazz) { return str; } else if( PatternOptionBuilder.OBJECT_VALUE == clazz) { return createObject(str); } else if( PatternOptionBuilder.NUMBER_VALUE == clazz) { return createNumber(str); } else if( PatternOptionBuilder.DATE_VALUE == clazz) { return createDate(str); } else if( PatternOptionBuilder.CLASS_VALUE == clazz) { return createClass(str); } else if( PatternOptionBuilder.FILE_VALUE == clazz) { return createFile(str); } else if( PatternOptionBuilder.EXISTING_FILE_VALUE == clazz) { return createFile(str); } else if( PatternOptionBuilder.FILES_VALUE == clazz) { return createFiles(str); } else if( PatternOptionBuilder.URL_VALUE == clazz) { return createURL(str); } else { return null; } } /** * Create an Object from the classname and empty constructor. * Returns null if it couldn't create the Object. */ static public Object createObject(String str) { Class cl = null; try { cl = Class.forName(str); } catch (ClassNotFoundException cnfe) { System.err.println("Unable to find: "+str); return null; } Object instance = null; try { instance = cl.newInstance(); } catch (InstantiationException cnfe) { System.err.println("InstantiationException; Unable to create: "+str); return null; } catch (IllegalAccessException cnfe) { System.err.println("IllegalAccessException; Unable to create: "+str); return null; } return instance; } /** * Create a number from a String. */ static public Number createNumber(String str) { // Needs to be able to create try { // do searching for decimal point etc, but atm just make an Integer return Numbers.createNumber(str); } catch (NumberFormatException nfe) { System.err.println(nfe.getMessage()); return null; } } static public Class createClass(String str) { try { return Class.forName(str); } catch (ClassNotFoundException cnfe) { System.err.println("Unable to find: "+str); return null; } } static public Date createDate(String str) { Date date = null; if(date == null) { System.err.println("Unable to parse: "+str); } return date; } static public URL createURL(String str) { try { return new URL(str); } catch (MalformedURLException mue) { System.err.println("Unable to parse: "+str); return null; } } static public File createFile(String str) { return new File(str); } static public File[] createFiles(String str) { // to implement/port: // return FileW.findFiles(str); return null; } } 1.1 jakarta-commons-sandbox/cli/src/java/org/apache/commons/cli/PatternOptionBuilder.java Index: PatternOptionBuilder.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: PatternOptionBuilder.java,v 1.1 2002/06/06 22:32:37 bayard Exp $ */ package org.apache.commons.cli; /** * Allows Options to be created from a single String. * * * @author Henri Yandell (bayard @ generationjava.com) * @version $Revision: 1.1 $ */ public class PatternOptionBuilder { /// TODO: These need to break out to OptionType and also to be pluggable. static public final Class STRING_VALUE = java.lang.String.class; static public final Class OBJECT_VALUE = java.lang.Object.class; static public final Class NUMBER_VALUE = java.lang.Number.class; static public final Class DATE_VALUE = java.util.Date.class; static public final Class CLASS_VALUE = java.lang.Class.class; /// can we do this one?? // is meant to check that the file exists, else it errors. // ie) it's for reading not writing. static public final Class EXISTING_FILE_VALUE = java.io.FileInputStream.class; static public final Class FILE_VALUE = java.io.File.class; static public final Class FILES_VALUE = java.io.File[].class; static public final Class URL_VALUE = java.net.URL.class; static public Object getValueClass(char ch) { if (ch == '@') { return PatternOptionBuilder.OBJECT_VALUE; } else if (ch == ':') { return PatternOptionBuilder.STRING_VALUE; } else if (ch == '%') { return PatternOptionBuilder.NUMBER_VALUE; } else if (ch == '+') { return PatternOptionBuilder.CLASS_VALUE; } else if (ch == '#') { return PatternOptionBuilder.DATE_VALUE; } else if (ch == '<') { return PatternOptionBuilder.EXISTING_FILE_VALUE; } else if (ch == '>') { return PatternOptionBuilder.FILE_VALUE; } else if (ch == '*') { return PatternOptionBuilder.FILES_VALUE; } else if (ch == '/') { return PatternOptionBuilder.URL_VALUE; } return null; } static public boolean isValueCode(char ch) { if( (ch != '@') && (ch != ':') && (ch != '%') && (ch != '+') && (ch != '#') && (ch != '<') && (ch != '>') && (ch != '*') && (ch != '/') ) { return false; } return true; } static public Options parsePattern(String pattern) { int sz = pattern.length(); char opt = ' '; char ch = ' '; boolean required = false; Object type = null; Options options = new Options(); for(int i=0; i For additional commands, e-mail: