下面列出了org.apache.commons.cli.Option#getValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Displays all command line options in log messages.
* @param line
*/
public static void logCommandLineOptions(Logger logger, CommandLine line) {
for (Option myOption : line.getOptions()) {
String message;
String opt = "";
if(myOption.getOpt() != null) {
opt+="-"+myOption.getOpt();
if(myOption.getLongOpt() != null)
opt+=" (--"+myOption.getLongOpt()+")";
} else
opt+="--"+myOption.getLongOpt()+"";
if(myOption.hasArg())
message = opt + " " + myOption.getValue();
else
message = opt;
logger.info("with option: " + message);
}
}
/**
* Builds an instance of T using the selected constructor getting the constructor
* parameters from the {@link CommandLine}.
*
* Note: this method will also automatically call {@link #applyCommandLineOptions(CommandLine, T)} on
* the constructed object.
*/
private T buildInstance(CommandLine cli) {
String[] constructorArgs = new String[this.constructor.getParameterTypes().length];
for (Option option : cli.getOptions()) {
if (this.constructoArgumentsMap.containsKey(option.getOpt())) {
int idx = this.constructoArgumentsMap.get(option.getOpt());
constructorArgs[idx] = option.getValue();
}
}
T embeddedGobblin;
try {
embeddedGobblin = this.constructor.newInstance((Object[]) constructorArgs);
return embeddedGobblin;
} catch (IllegalAccessException | InvocationTargetException | InstantiationException exc) {
throw new RuntimeException("Could not instantiate " + this.klazz.getName(), exc);
}
}
private BlurObject createBlurObject(CommandLine commandLine, CommandDescriptor descriptor) throws CommandException {
Map<String, ArgumentDescriptor> arguments = new TreeMap<String, ArgumentDescriptor>(
descriptor.getOptionalArguments());
arguments.putAll(descriptor.getRequiredArguments());
BlurObject blurObject = new BlurObject();
if (commandLine == null) {
return null;
}
Option[] options = commandLine.getOptions();
for (Option option : options) {
String name = option.getOpt();
String value = option.getValue();
ArgumentDescriptor argumentDescriptor = arguments.get(name);
String type = argumentDescriptor.getType();
blurObject.put(name, convertIfNeeded(value, type));
}
return blurObject;
}
protected void checkOptions(Collection<Option> options) {
for (Option option : options) {
if (option.isRequired()) {
String value = option.getValue();
if (StringUtils.isBlank(value)) {
throw new IllegalStateException("option: key =[" + option.getLongOpt() + "], required=["
+ option.isRequired() + "] is blank!");
}
}
}
}
private String getToolName() throws ParseException {
for (Option option : cmdLine.getOptions()) {
if (option.equals(OPT_TOOL)) {
return option.getValue();
}
}
// We should never get here as the parser of the command line would
// have thrown an exception before for missing required argument.
Preconditions.checkState(false);
throw new ParseException("Metric tool name not found");
}
@Override
public void configure(final Configuration config, final Option... options) {
if (null != config) {
// Get the UID-specific options
final Map<String,Option> uidOptions;
if (null != options) {
uidOptions = new HashMap<>(4);
for (final Option option : options) {
if (null != option) {
// Look for one of the 4 types of UID options
final String key = option.getLongOpt();
final String value;
if (UIDConstants.UID_TYPE_OPT.equals(key)) {
value = option.getValue(HashUID.class.getSimpleName());
} else if (UIDConstants.HOST_INDEX_OPT.equals(key)) {
value = option.getValue();
} else if (UIDConstants.PROCESS_INDEX_OPT.equals(key)) {
value = option.getValue();
} else if (UIDConstants.THREAD_INDEX_OPT.equals(key)) {
value = option.getValue();
} else {
value = null;
}
// Check for null
if (null != value) {
// Put the key and value into the map
uidOptions.put(key, option);
// Stop looping if we've got everything we need
if (uidOptions.size() >= 4) {
break;
} else if (UIDConstants.UID_TYPE_OPT.equals(key) && HashUID.class.getSimpleName().equals(value)) {
break;
}
}
}
}
} else {
uidOptions = Collections.emptyMap();
}
// Configure with the UID-specific options
configure(config, uidOptions);
}
}
@Override
public final R execute(final CommandLine commandLine) throws CommandException {
try {
final Properties properties = new Properties();
// start by loading the properties file if it was specified
if (commandLine.hasOption(CommandOption.PROPERTIES.getLongName())) {
final String propertiesFile = commandLine.getOptionValue(CommandOption.PROPERTIES.getLongName());
if (!StringUtils.isBlank(propertiesFile)) {
try (final InputStream in = new FileInputStream(propertiesFile)) {
properties.load(in);
}
}
} else {
// no properties file was specified so see if there is anything in the session
final SessionVariable sessionVariable = getPropertiesSessionVariable();
if (sessionVariable != null) {
final Session session = getContext().getSession();
final String sessionPropsFiles = session.get(sessionVariable.getVariableName());
if (!StringUtils.isBlank(sessionPropsFiles)) {
try (final InputStream in = new FileInputStream(sessionPropsFiles)) {
properties.load(in);
}
}
}
}
// add in anything specified on command line, and override anything that was already there
for (final Option option : commandLine.getOptions()) {
final String optValue = option.getValue() == null ? "" : option.getValue();
properties.setProperty(option.getLongOpt(), optValue);
}
// delegate to sub-classes
return doExecute(properties);
} catch (CommandException ce) {
throw ce;
} catch (Exception e) {
throw new CommandException("Error executing command '" + getName() + "' : " + e.getMessage(), e);
}
}