package com.philmcrew.utility; // http://www.urbanophile.com/arenn/coding/download.html#getopt import gnu.getopt.Getopt; import gnu.getopt.LongOpt; /** * Created by IntelliJ IDEA. * User: mfriedma * Date: Sep 16, 2003 * Time: 1:12:15 PM * To change this template use Options | File Templates. */ public class GetOptExtended { private Getopt getopt; public GetOptExtended() { } public GetOptExtended(String caller, String[] args, String params, LongOpt[] longopts) { setGetopt(caller, args, params, longopts); } public void setGetopt(String caller, String[] args, String params, LongOpt[] longopts) { getopt = new Getopt(caller, args, params, longopts); } public void setOpterr(boolean optErr) { getopt.setOpterr(optErr); } public int getopt() { return getopt.getopt(); } public String getOptarg() { return getopt.getOptarg(); } public String getOptarg(String optName) throws MjfUtilException { String arg = getOptarg(); if (null == arg) throw new MjfUtilException("Missing " + optName + " value for -" + getopt.getOptopt() + " parameter."); return arg; } public String getString(String optName) throws MjfUtilException { return getOptarg(optName); } public int getOptopt() { return getopt.getOptopt(); } public boolean getBoolean(String optName) throws MjfUtilException { boolean ret = false; String arg = null; arg = getOptarg(optName); ret = Boolean.getBoolean(arg); return ret; } public byte getByte(String optName) throws MjfUtilException { byte ret = -1; String arg = null; try { arg = getOptarg(optName); ret = Byte.parseByte(arg); } catch (NumberFormatException e) { throw new MjfUtilException("Bad " + optName + " value for - " + (char)getOptopt() + " parameter - " + arg, e); } return ret; } public int getInt(String optName) throws MjfUtilException { int ret = -1; String arg = null; try { arg = getOptarg(optName); ret = Integer.parseInt(arg); } catch (NumberFormatException e) { throw new MjfUtilException("Bad " + optName + " value for - " + (char)getOptopt() + " parameter - " + arg, e); } return ret; } public long getLong(String optName) throws MjfUtilException { long ret = -1; String arg = null; try { arg = getOptarg(optName); ret = Long.parseLong(arg); } catch (NumberFormatException e) { throw new MjfUtilException("Bad " + optName + " value for - " + (char)getOptopt() + " parameter - " + arg, e); } return ret; } public float getFloat(String optName) throws MjfUtilException { float ret = -1; String arg = null; try { arg = getOptarg(optName); ret = Float.parseFloat(arg); } catch (NumberFormatException e) { throw new MjfUtilException("Bad " + optName + " value for - " + (char)getOptopt() + " parameter - " + arg, e); } return ret; } public double getDouble(String optName) throws MjfUtilException { double ret = -1; String arg = null; try { arg = getOptarg(optName); ret = Double.parseDouble(arg); } catch (NumberFormatException e) { throw new MjfUtilException("Bad " + optName + " value for - " + (char)getOptopt() + " parameter - " + arg, e); } return ret; } public long getShort(String optName) throws MjfUtilException { short ret = -1; String arg = null; try { arg = getOptarg(optName); ret = Short.parseShort(arg); } catch (NumberFormatException e) { throw new MjfUtilException("Bad " + optName + " value for - " + (char)getOptopt() + " parameter - " + arg, e); } return ret; } }