Update of /cvsroot/metamorphosis/krysalis-version/src/java/org/krysalis/version/util/cli
In directory sc8-pr-cvs1:/tmp/cvs-serv25982/src/java/org/krysalis/version/util/cli
Added Files:
CommandLineParser.java CommandLine.java ParseException.java
Options.java Option.java
Log Message:
More docs
Command Line Tool
Removed CLI dependency (can't afford a core dependency for such little gain).
--- NEW FILE: CommandLineParser.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01
Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
This Licence is compatible with the BSD licence as described and
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:
"This product includes software developed for project
Krysalis (http://www.krysalis.org/)."
Alternately, this acknowledgment may appear in the software itself,
if and wherever such third-party acknowledgments normally appear.
4. The names "Krysalis" and "Nicola Ken Barozzi" and
"Krysalis Centipede" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact [email protected].
5. Products derived from this software may not be called "Krysalis",
"Krysalis Centipede", nor may "Krysalis" appear in their name, without
prior written permission of Nicola Ken Barozzi.
6. This software may contain voluntary contributions made by many
individuals, who decided to donate the code to this project in respect
of this licence.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.util.cli;
/**
*
* @author $Author: arb_jack $
* @version $Revision: 1.1 $
*
*/
public class CommandLineParser {
public CommandLine parse(Options options, String args[])
throws ParseException {
CommandLine cl = new CommandLine();
if (null != args)
for (int i = 0; i < args.length; ++i) {
String opt = args[i];
if (opt.startsWith("/") || opt.startsWith("-"))
opt = opt.substring(1);
else
throw new ParseException("Not a recognizable option[" + opt + "]. Options look like -a or /a");
Option option = options.getOption(opt);
if (null == option)
throw new ParseException("Unknown option [" + opt + "]. Not in " + options.getOptionsString());
if (option.isHasArg()) {
i++;
if (i >= args.length)
throw new ParseException(
"Missing argument for [" + option + "]");
String arg = args[i];
cl.addOption(option, arg);
}
else
cl.addOption(option);
}
return cl;
}
}
--- NEW FILE: CommandLine.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01
Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
This Licence is compatible with the BSD licence as described and
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:
"This product includes software developed for project
Krysalis (http://www.krysalis.org/)."
Alternately, this acknowledgment may appear in the software itself,
if and wherever such third-party acknowledgments normally appear.
4. The names "Krysalis" and "Nicola Ken Barozzi" and
"Krysalis Centipede" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact [email protected].
5. Products derived from this software may not be called "Krysalis",
"Krysalis Centipede", nor may "Krysalis" appear in their name, without
prior written permission of Nicola Ken Barozzi.
6. This software may contain voluntary contributions made by many
individuals, who decided to donate the code to this project in respect
of this licence.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.util.cli;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* @author $Author: arb_jack $
* @version $Revision: 1.1 $
*
*/
public class CommandLine {
private static final String EMPTY_ARRAY[] = new String[0];
private Map m_options = null;
public CommandLine() {
m_options = new HashMap();
}
public void addOption(Option option) {
addOption(option, "");
}
public void addOption(Option option, String argument) {
List values = (List) m_options.get(option);
if (null == values) {
values = new ArrayList();
m_options.put(option, values);
}
values.add(argument);
}
public String[] getOptionValues(Option option) {
String array[] = null;
List values = (List) m_options.get(option);
if (null != values) {
Object stuff[] = values.toArray();
String sarray[] = new String[stuff.length];
for (int i = 0; i < stuff.length; ++i) {
sarray[i] = (String) stuff[i];
}
array = sarray;
}
else
array = EMPTY_ARRAY;
return array;
}
public boolean isSet(Option option) {
return (null != m_options.get(option));
}
public boolean hasArguments() {
return !(m_options.isEmpty());
}
}
--- NEW FILE: ParseException.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01
Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
This Licence is compatible with the BSD licence as described and
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:
"This product includes software developed for project
Krysalis (http://www.krysalis.org/)."
Alternately, this acknowledgment may appear in the software itself,
if and wherever such third-party acknowledgments normally appear.
4. The names "Krysalis" and "Nicola Ken Barozzi" and
"Krysalis Centipede" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact [email protected].
5. Products derived from this software may not be called "Krysalis",
"Krysalis Centipede", nor may "Krysalis" appear in their name, without
prior written permission of Nicola Ken Barozzi.
6. This software may contain voluntary contributions made by many
individuals, who decided to donate the code to this project in respect
of this licence.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.util.cli;
import org.krysalis.version.exception.VersionException;
/**
*
* @author $Author: arb_jack $
* @version $Revision: 1.1 $
*
*/
public class ParseException extends VersionException {
/**
* Constructor for ParseException.
*/
public ParseException() {
super();
}
/**
* Constructor for ParseException.
* @param message
*/
public ParseException(String message) {
super(message);
}
/**
* Constructor for ParseException.
* @param message
* @param cause
*/
public ParseException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructor for ParseException.
* @param cause
*/
public ParseException(Throwable cause) {
super(cause);
}
}
--- NEW FILE: Options.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01
Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
This Licence is compatible with the BSD licence as described and
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:
"This product includes software developed for project
Krysalis (http://www.krysalis.org/)."
Alternately, this acknowledgment may appear in the software itself,
if and wherever such third-party acknowledgments normally appear.
4. The names "Krysalis" and "Nicola Ken Barozzi" and
"Krysalis Centipede" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact [email protected].
5. Products derived from this software may not be called "Krysalis",
"Krysalis Centipede", nor may "Krysalis" appear in their name, without
prior written permission of Nicola Ken Barozzi.
6. This software may contain voluntary contributions made by many
individuals, who decided to donate the code to this project in respect
of this licence.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.util.cli;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
*
* @author $Author: arb_jack $
* @version $Revision: 1.1 $
*
*/
public class Options {
private List m_options = null;
private Map m_optKey = null;
private Map m_longOptKey = null;
public Options() {
m_options = new ArrayList();
m_optKey = new HashMap();
m_longOptKey = new HashMap();
}
public void addOption(String opt, boolean hasArg, String desc) {
addOption(new Option(opt, hasArg, desc));
}
public void addOption(
String opt,
String logOpt,
boolean hasArg,
String desc) {
addOption(new Option(opt, logOpt, hasArg, desc));
}
public void addOption(Option option) {
m_options.add(option);
if (m_optKey.containsKey(option.getOpt()))
throw new Error("Duplicate CommandLine Option [" + option + "]");
m_optKey.put(option.getOpt(), option);
if (null != option.getLongOpt())
m_longOptKey.put(option.getLongOpt(), option);
}
public Option getOption(String opt) {
Option option = (Option) m_optKey.get(opt);
if (null == option)
option = (Option) m_longOptKey.get(opt);
return option;
}
public String getOptionsString() {
StringBuffer list = new StringBuffer();
for (Iterator i = m_options.iterator(); i.hasNext();) {
Option option = (Option) i.next();
list.append(option.getOpt());
if (null != option.getLongOpt()) {
list.append("(");
list.append(option.getLongOpt());
list.append(")");
}
if (i.hasNext())
list.append(" ");
}
return list.toString();
}
}
--- NEW FILE: Option.java ---
/*
The Krysalis Patchy Software License, Version 1.1_01
Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
This Licence is compatible with the BSD licence as described and
approved by http://www.opensource.org/, and is based on the
Apache Software Licence Version 1.1.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:
"This product includes software developed for project
Krysalis (http://www.krysalis.org/)."
Alternately, this acknowledgment may appear in the software itself,
if and wherever such third-party acknowledgments normally appear.
4. The names "Krysalis" and "Nicola Ken Barozzi" and
"Krysalis Centipede" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact [email protected].
5. Products derived from this software may not be called "Krysalis",
"Krysalis Centipede", nor may "Krysalis" appear in their name, without
prior written permission of Nicola Ken Barozzi.
6. This software may contain voluntary contributions made by many
individuals, who decided to donate the code to this project in respect
of this licence.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
====================================================================
*/
package org.krysalis.version.util.cli;
/**
*
* @author $Author: arb_jack $
* @version $Revision: 1.1 $
*
*/
public class Option implements Comparable {
private String m_opt = null;
private String m_longOpt = null;
private boolean m_hasArg = false;
private String m_desc = null;
public Option(String opt, boolean hasArg, String desc) {
m_opt = opt;
m_hasArg = hasArg;
m_desc = desc;
}
public Option(String opt, String longOpt, boolean hasArg, String desc) {
m_opt = opt;
m_longOpt = longOpt;
m_hasArg = hasArg;
m_desc = desc;
}
/**
* Returns the desc.
* @return String
*/
public String getDesc() {
return m_desc;
}
/**
* Returns the hasArg.
* @return boolean
*/
public boolean isHasArg() {
return m_hasArg;
}
/**
* Returns the longOpt.
* @return String
*/
public String getLongOpt() {
return m_longOpt;
}
/**
* Returns the opt.
* @return String
*/
public String getOpt() {
return m_opt;
}
/**
* Sets the desc.
* @param desc The desc to set
*/
public void setDesc(String desc) {
m_desc = desc;
}
/**
* Sets the hasArg.
* @param hasArg The hasArg to set
*/
public void setHasArg(boolean hasArg) {
m_hasArg = hasArg;
}
/**
* Sets the longOpt.
* @param longOpt The longOpt to set
*/
public void setLongOpt(String longOpt) {
m_longOpt = longOpt;
}
/**
* Sets the opt.
* @param opt The opt to set
*/
public void setOpt(String opt) {
m_opt = opt;
}
public String toString() {
return m_opt
+ ":"
+ ((null != m_longOpt) ? m_longOpt : "")
+ ":"
+ m_hasArg
+ ":"
+ m_desc;
}
public int compareTo(Object other) {
int comparison = -1;
if (null == other) {
comparison = -1;
}
else if (other instanceof Option) {
Option otherOption = (Option) other;
comparison = m_opt.compareTo(otherOption.m_opt);
if (0 == comparison)
if (null != m_longOpt)
if (null != otherOption.m_longOpt)
comparison = m_longOpt.compareTo(otherOption.m_longOpt);
else
comparison = -1;
else if (null != otherOption.m_longOpt)
comparison = -1;
if ( 0 == comparison )
comparison = (m_opt == otherOption.m_opt) ? 0 : -1;
if ( 0 == comparison )
comparison = (m_desc.compareTo(otherOption.m_desc));
}
else
throw new IllegalArgumentException(
"Not an 'Option':"
+ other.getClass().getName()
+ ":"
+ other.toString());
return comparison;
}
}
-------------------------------------------------------
This SF.NET email is sponsored by: FREE SSL Guide from Thawte
are you planning your Web Server Security? Click here to get a FREE
Thawte SSL guide and find the answers to all your SSL security issues.
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.