/** * Format the current value of the {@link Option} as a human readable {@link String}. * * @param optionName The name of the {@link Option} the value of which to describe. * @return A human readable representation of the requested object. */ public static String describeDefault(String optionName) { // Ensure that the options stash is created init(); String returnValue = null; if (isArgument(optionName)) { returnValue = getString(optionName); } else if (isSwitch(optionName)) { returnValue = String.valueOf(getBoolean(optionName)); } else if (isEnum(optionName)) { Option<?> opt = get(optionName); if (opt != null && opt.getValue() != null && !opt.getValue().isEmpty()) { int count = 0; StringBuilder ret = new StringBuilder(); for (Object next : opt.getValue()) { if (count == 0) { ret.append(next.toString()); } else { ret.append(COMMA); ret.append(next.toString()); } count++; } returnValue = ret.toString(); } } return returnValue; }
/** * Get the description registered for the given option. * * @param optionName The name of the option the description of which to fetch. * @return The description of the given option. */ public static String getDescription(String optionName) { // Ensure that the options stash is created init(); Option<?> opt = get(optionName); if (opt != null && opt.getDescription() != null) { return opt.getDescription(); } return null; }
/** * Convenience routine for finding out if an {@link Option} is a multi-value type or not. * * @param optionName The name of the {@link Option} to check. * @return True if the {@link Option} is a multi-value, false otherwise. */ public static boolean isMultiValue(String optionName) { // Ensure that the options stash is created init(); Option<?> opt = get(optionName); if (opt != null) { return opt.isMultiValue(); } else { return false; } }
/** * Convenience routine for getting the value of an {@link Option} which is known to be a {@link * Boolean}. * * @param optionName The name of the {@link Option} to fetch. * @return A {@link Boolean} if the option holds a {@link Boolean}, null otherwise. */ public static Boolean getBoolean(String optionName) { Option<?> opt = get(optionName); if (opt != null && isSwitch(optionName)) { if (opt.getValue().size() == 1) { String s = opt.getValue().get(0); if (s != null) { return Boolean.valueOf(s); } } } return null; }
/** * Get the value associated with a particular option. * * @param optionName The name of the option to return the value for. * @return The value associated with the given option name. */ private static Option<?> get(String optionName) { if (optionName != null) { init(); if (isLegal(optionName)) { Option<?> ret = options.get(optionName); if (ret != null) { DBC.ASSERT(optionName.equals(ret.getName())); return ret; } } } return null; }
/** * Set the value of a particular option in the stash. * * @param optionName The name of the option to set. * @param optionValue The value of the option to set. */ public static void set(String optionName, String optionValue) { // Ensure that the options stash is created init(); // Make sure that the option being set is an allowed one if (isLegal(optionName)) { Option<?> optionToSet = get(optionName); List<String> vals = null; if (isEnum(optionName)) { // The string is a list of comma separated values String[] rawValues = optionValue.split(String.valueOf(COMMA)); vals = new ArrayList<String>(rawValues.length); for (String rawV : rawValues) { String rawValue; if (optionToSet.isCaseSensitive()) { rawValue = rawV; } else { rawValue = rawV.toLowerCase(); } Map<String, String> enumValueMap; enumValueMap = Enum.class.cast(optionToSet).getEnumValues(); if (enumValueMap.containsKey(rawValue)) { vals.add(rawValue); } } } else if (isSwitch(optionName)) { vals = new ArrayList<String>(1); vals.add(String.valueOf(Boolean.valueOf(optionValue))); } else if (isArgument(optionName)) { vals = new ArrayList<String>(1); if (optionToSet.isCaseSensitive()) { vals.add(optionValue); } else { vals.add(optionValue.toLowerCase()); } } if (vals != null) { optionToSet.setValue(vals); } options.put(optionName, optionToSet); // Handle the special case of debug Boolean debugValue = getBoolean("debug"); // NON-NLS if (debugValue != null) { debug = debugValue; } } }
/** * Check that all of the mandatory options have been set. * * @return True if all of the mandatory options have been given a value, false otherwise. */ public static boolean allMandatoryOptionsSet() { // Ensure that the options stash is created init(); for (String optionName : options.keySet()) { if (isMandatory(optionName)) { Option<?> opt = get(optionName); if (opt == null || opt.getValue() == null || opt.getValue().isEmpty()) { Log.warning(Message.MISSING_MANDATORY_OPTION(optionName)); return false; } } } return true; }
/** * Convenience routine for testing whether or not a particular value in an {@link Enum} is set. * * @param enumName The name of the {@link Enum} * @param enumValue The value to query * @return true if the value is present in the set of values for that {@link Enum}, false if it is * not, throws an exception for all other conditions. */ public static boolean isSet(String enumName, String enumValue) { // Ensure that the options stash is created init(); Option<?> opt = get(enumName); if (opt != null && isEnum(enumName)) { for (Object val : opt.getValue()) { if (val instanceof String) { if (val.equals(enumValue)) { return true; } } } } return false; }
/** * Convenience routine for getting the value of an {@link Option} which is known to be a {@link * String}. * * @param optionName The name of the {@link Option} to fetch. * @return A {@link String} if the option holds a {@link String}, returns null otherwise. */ public static String getString(String optionName) { // Ensure that the options stash is created init(); String returnValue = null; Option<?> opt = get(optionName); if (opt != null) { if (isArgument(optionName)) { if (opt.getValue().size() == 1) { Object obj = opt.getValue().get(0); if (obj != null && obj instanceof String) { returnValue = (String) obj; } } } else if (isEnum(optionName)) { if (!opt.isMultiValue()) { if (opt.getValue().size() == 1) { Object obj = opt.getValue().get(0); if (obj != null && obj instanceof String) { returnValue = (String) obj; } } } } } return returnValue; }