📄 abstractcommand.java
字号:
public long getArgumentLong(String key) { String valString = getExistingArgument(key); try { return Long.parseLong(valString); } catch (NumberFormatException e) { illegalPropertyArgument("Required integer.", key); // required for compiler; doen't know catch always throws return -1l; } } /** * Returns the value of the argument with the specified key * converted to a double value. * * @param key Name of argument to convert to a double and return. * @return Double value of command-line argument. * @throws IllegalArgumentException If there is a number format * exception converting the argument to a double, or if there is * no value supplied. */ public double getArgumentDouble(String key) { String valString = getArgument(key); if (valString == null) throw new IllegalArgumentException("No value found for argument=" + key); try { return Double.parseDouble(valString); } catch (NumberFormatException e) { throw new IllegalArgumentException("Required double value for arg=" + key + " Found=" + valString); } } /** * Returns the value of the argument with the specified key * converted to a file. The argument must be specified. * * @param key Name of argument to convert to a file and return. * @return File specified by value of argument specified by key. * @throws IllegalArgumentException If the argument is not * specified. */ public File getArgumentFile(String key) { String fileName = getExistingArgument(key); return new File(fileName); } /** * Returns the existing normal file named by the value of * the specified key. The argument must be specified. * * @param key Name of argument to convert to file, check for * existence, and return. * @return File named by the value of the specified key. * @throws IllegalArgumentException If the argument has no value * or is not an existing file. */ public File getArgumentExistingNormalFile(String key) { File file = getArgumentFile(key); if (!file.isFile()) illegalPropertyArgument("Require existing normal file.",key); return file; } /** * Returns the value of the argument with the specified key * converted to a directory. The directory must exist and * be a directory. * * @param key Name of argument to convert to a directory and return. * @return Directory specified by value of argument specified by key. * @throws IllegalArgumentException If the argument is not * specified, or is not an existing directory. */ public File getArgumentDirectory(String key) { File dir = getArgumentFile(key); try { if (!dir.isDirectory()) illegalPropertyArgument("Require existing directory.", key); } catch (SecurityException e) { illegalPropertyArgument("Security exception accessing directory.", key); } return dir; } /** * Returns the value of the argument with the specified key * converted to a directory. The directory must either exist * or be creatable. The return value will be an existing * directory. * * @param key Name of argument to convert to a directory and return. * @return Directory specified by value of argument specified by key. * @throws IllegalArgumentException If the argument is not * specified, and is not an existing or creatable directory. */ public File getOrCreateArgumentDirectory(String key) { File dir = getArgumentFile(key); try { if (dir.isFile()) illegalPropertyArgument("Must be existing or creatable directory.", key); if (!dir.isDirectory() && !dir.mkdirs()) illegalPropertyArgument("Could not create directory.", key); } catch (SecurityException e) { illegalPropertyArgument("Security exception inspecting or creating directory.", key); } return dir; } /** * Returns the model file specified by the command line * after guaranteeing that the file can be created. * * @return Normal file containing the model. * @throws IllegalArgumentException If the model is not specified * on the command line or is not a file that can be created. */ protected File getArgumentCreatableFile(String fileParam) { File file = getArgumentFile(fileParam); if (file.isDirectory()) illegalPropertyArgument("File must be normal. Found directory=",fileParam); File parentDir = file.getParentFile(); if (parentDir == null) parentDir = new File("."); if (parentDir.isFile()) illegalPropertyArgument("Parent cannot be ordinary file.", fileParam); if (!parentDir.isDirectory()) { System.out.println("Creating model parent directory=" + parentDir); parentDir.mkdirs(); } return file; } /** * Parses a sequence of command-line argument, adding them to * these arguments. * * @param args Command-line arguments. * @throws IllegalArgumentException If the arguments are not well formed. */ private final void parse(String[] args) { for (int i = 0; i < args.length; ++i) parseSingleArg(args[i]); } /** * Parses a single command-line argument, adding the result to these * arguments. * * @param arg Command-line argument. * @throws IllegalArgumentException If the arguments are not well formed. */ private final void parseSingleArg(String arg) { if (arg.length() < 1) return; else if (arg.charAt(0) == '-') parseSingleBody(arg.substring(1)); else mProperties.setProperty(bareArgumentProperty(mBareArgCount++), arg); } /** * Parses the body of a single argument that was provided * beginning with a <code>-</code>, adding it to these arguments. * The argument may contain an <code>=</code> sign or not. The * argument may be empty, in which case the empty string is returned. * * @param arg Command-line argument. * @throws IllegalArgumentException If the arguments are not well formed. */ private void parseSingleBody(String arg) { if (arg.length() < 1) return; int pos = arg.indexOf('='); if (pos < 0) { mProperties.setProperty(arg,HAS_PROPERTY_VALUE); return; } String property = arg.substring(0,pos); String value = arg.substring(pos+1); if (property.length() <= 0) illegalArgument("Property must have non-zero-length.", '-'+arg+'='+value); // made this OK after 2.2.1 // if (value.length() <= 0) // illegalArgument("Value must have non-zero length.", // '-'+arg+'='+value); mProperties.setProperty(property,value); } /** * Throw an illegal argument message with the specified message * and a report of the specified key and its value. * * @param msg Message to display. * @param key Key required or found on the command line. * @throws IllegalArgumentException Always, with specified message and * a report of the found argument. */ protected void illegalPropertyArgument(String msg, String key) { throw new IllegalArgumentException(msg + " Found -" + key + "=" + getArgument(key)); } /** * Throw an illegal argument exception with the specified message * and a report of the argument found. * * @param msg Message to display. * @param arg The argument found on the command line. * @throws IllegalArgumentException Always, with specified message and * a report of the found argument. */ protected void illegalArgument(String msg, String arg) { illegalArgument(msg + "Found:" + arg); } /** * Throw an illegal argument exception with the specified message * with a source provided by the specified exception. * * @param msg Message to display. * @param e Exception causing the illegal argument exception. * @throws IllegalArgumentException Always, with specified message and * a report of embedded exception. */ protected void illegalArgument(String msg, Exception e) { illegalArgument(msg + " Contained exception =" + e); } /** * Throw an illegal argument exception with the specified message. * * @param msg Message to include in the exception. * @throws IllegalArgumentException Always, with the specified message. */ protected void illegalArgument(String msg) { throw new IllegalArgumentException(msg); } /** * Throws an illegal argument exception if the first parameter is * defined in the command the second parameter is not. This is * used to check implicational constraints between parameters. * * @param ifParam Parameter to test for definedness. * @param thenParam Parameter to test for definedness. * @throws IllegalArgumentException If the first parameter is * defined and the second is not. */ public void checkParameterImplication(String ifParam, String thenParam) { String ifVal = getArgument(ifParam); String thenVal = getArgument(thenParam); if (ifVal != null && thenVal == null) illegalArgument("If param=" + ifParam + " is defined, then param=" + thenParam + " should be defined."); } /** * Returns the name of the property for the <code>n</code>th * bare argument. * * @param n Index of bare property. * @return Name of bare argument property. */ private static String bareArgumentProperty(int n) { return BARE_ARG_PREFIX + n; } /** * The value assigned to arguments beginning with <code>'-'</code> * and not containing an <code>'='</code>. */ public static final String HAS_PROPERTY_VALUE = "*HAS_PROPERTY_VALUE*"; /** * The string prefixed before a number to indicate bare arguments. * For instance, the fifth bare argument will be the value of the * property <code>{@link #BARE_ARG_PREFIX} + 5</code>. */ public static final String BARE_ARG_PREFIX = "BARE_ARG_";}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -