下面列出了org.apache.commons.cli.Option#getLongOpt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
protected static int compareOptions(Option a, Option b) {
String aShort = a.getOpt();
String bShort = b.getOpt();
String aLong = a.getLongOpt();
String bLong = b.getLongOpt();
if (aShort != null && aShort.length() > 0 && bShort != null && bShort.length() > 0) {
return aShort.compareToIgnoreCase(bShort);
} else if (aLong != null && aLong.length() > 0 && bLong != null && bLong.length() > 0) {
return aLong.compareToIgnoreCase(bLong);
} else if (aShort != null && aShort.length() > 0 && bLong != null && bLong.length() > 0) {
return aShort.compareToIgnoreCase(bLong);
} else if (aLong != null && aLong.length() > 0 && bShort != null && bShort.length() > 0) {
return aLong.compareToIgnoreCase(bShort);
} else {
return 0;
}
}
public static Properties commandLine2Properties(final CommandLine commandLine) {
Properties properties = new Properties();
Option[] opts = commandLine.getOptions();
if (opts != null) {
for (Option opt : opts) {
String name = opt.getLongOpt();
String value = commandLine.getOptionValue(name);
if (value != null) {
properties.setProperty(name, value);
}
}
}
return properties;
}
/**
* 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);
}
}
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!");
}
}
}
}
/**
* Oh yeah? It's not as bad as this!
*
* @param o Option to be formatted.
* @return Markdown formatted documentation string.
*/
protected static String formatOption(Option o) {
String shortString = "";
String shortOpt = o.getOpt();
if (shortOpt != null && shortOpt.length() > 0) {
shortString = "-" + shortOpt;
}
String longString = "";
String longOpt = o.getLongOpt();
if (longOpt != null && longOpt.length() > 0) {
longString = "--" + longOpt;
}
String joinString = "";
if (shortString.length() > 0 && longString.length() > 0) {
joinString = ", ";
}
String argName = o.getArgName();
if (argName != null && argName.length() > 0) {
if (shortString.length() > 0) {
shortString += " " + argName;
}
if (longString.length() > 0) {
longString += "=" + argName;
}
}
if (shortString.length() > 0) {
shortString = "`" + shortString + "`";
}
if (longString.length() > 0) {
longString = "`" + longString + "`";
}
// This formatting uses the MkDocs def_list markdown extension
return String.format("%s%s%s\n: %s\n\n", shortString, joinString, longString, o.getDescription());
}
public static void processOptions(@Nullable CommandLine cl,
@Nonnull Map<String, String> options) {
if (cl == null) {
return;
}
for (Option opt : cl.getOptions()) {
String optName = opt.getLongOpt();
if (optName == null) {
optName = opt.getOpt();
}
options.put(optName, opt.getValue());
}
}
/**
* Dump state of the Option object instance, suitable for debugging and comparing Options as Strings.
*
* @param option the Option object instance to dump the state of
*
* @return Stringified form of this Option object instance
*/
private String optionToString(Option option)
{
StringBuilder buf = new StringBuilder();
buf.append("[ option: ");
buf.append(option.getOpt());
if (option.getLongOpt() != null)
{
buf.append(" ").append(option.getLongOpt());
}
buf.append(" ");
if (option.hasArg())
{
buf.append(" [ARG]");
}
buf.append(" :: ").append(option.getDescription());
if (option.isRequired())
{
buf.append(" [REQUIRED]");
}
buf.append(" ]");
return buf.toString();
}
private static String getKey(Option opt) {
final String val = opt.getLongOpt();
if (val == null) {
return opt.getOpt();
}
return val;
}
public CommandCompleter(Map<String, Command<StratosCommandContext>> commands) {
if (logger.isDebugEnabled()) {
logger.debug("Creating auto complete for {} commands", commands.size());
}
fileNameCompleter = new StratosFileNameCompleter();
argumentMap = new HashMap<String, Collection<String>>();
defaultCommandCompleter = new StringsCompleter(commands.keySet());
helpCommandCompleter = new ArgumentCompleter(new StringsCompleter(CliConstants.HELP_ACTION),
defaultCommandCompleter);
for (String action : commands.keySet()) {
Command<StratosCommandContext> command = commands.get(action);
Options commandOptions = command.getOptions();
if (commandOptions != null) {
if (logger.isDebugEnabled()) {
logger.debug("Creating argument completer for command: {}", action);
}
List<String> arguments = new ArrayList<String>();
Collection<?> allOptions = commandOptions.getOptions();
for (Object o : allOptions) {
Option option = (Option) o;
String longOpt = option.getLongOpt();
String opt = option.getOpt();
if (StringUtils.isNotBlank(longOpt)) {
arguments.add("--" + longOpt);
} else if (StringUtils.isNotBlank(opt)) {
arguments.add("-" + opt);
}
}
argumentMap.put(action, arguments);
}
}
}
@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);
}
}
public String opt(Option option) {
return option.hasLongOpt()
? option.getLongOpt()
: option.getOpt();
}
private void requireOption(CommandLine cmdLine, Option option) {
if (!cmdLine.hasOption(option.getOpt())) {
throw new IllegalStateException(option.getLongOpt() + " is a mandatory parameter");
}
}