下面列出了org.apache.commons.cli.Option#hasArg ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void logOptions(String sectionName, String prefix, Options options)
{
if ( sectionName != null )
{
log.info("== " + sectionName + " ==");
}
//noinspection unchecked
for ( Option option : (Iterable<? extends Option>)options.getOptions() )
{
if ( option.hasLongOpt() )
{
if ( option.hasArg() )
{
log.info(prefix + option.getLongOpt() + " <arg> - " + option.getDescription());
}
else
{
log.info(prefix + option.getLongOpt() + " - " + option.getDescription());
}
}
}
}
/**
* 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);
}
}
/**
* For each method for which the helper created an {@link Option} and for which the input {@link CommandLine} contains
* that option, this method will automatically call the method on the input object with the correct
* arguments.
*/
public void applyCommandLineOptions(CommandLine cli, T embeddedGobblin) {
try {
for (Option option : cli.getOptions()) {
if (!this.methodsMap.containsKey(option.getOpt())) {
// Option added by cli driver itself.
continue;
}
if (option.hasArg()) {
this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin, option.getValue());
} else {
this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin);
}
}
} catch (IllegalAccessException | InvocationTargetException exc) {
throw new RuntimeException("Could not apply options to " + embeddedGobblin.getClass().getName(), exc);
}
}
public String getOptionsAsString() {
StringBuilder buf = new StringBuilder();
for (Option option : commandLine.getOptions()) {
buf.append(" ");
buf.append(option.getOpt());
if (option.hasArg()) {
buf.append("=");
buf.append(option.getValue());
}
}
return buf.toString();
}
public CommandlineOption(Option opt)
throws IllegalArgumentException {
super(opt.getOpt(), opt.hasArg(), opt.getDescription());
this.setLongOpt(opt.getLongOpt());
this.setRequired(opt.isRequired());
this.setArgName(opt.getArgName());
this.setArgs(opt.getArgs());
this.setOptionalArg(opt.hasOptionalArg());
this.setType(opt.getType());
this.setValueSeparator(opt.getValueSeparator());
}
/**
* Appends the usage clause for an Option to a StringBuffer.
*
* @param buff
* the StringBuffer to append to
* @param option
* the Option to append
* @param required
* whether the Option is required or not
*/
protected void appendOption(final StringBuilder buff, final Option option,
final boolean required) {
if (!required) {
buff.append('[');
}
if (option.getOpt() != null) {
buff.append(getOptPrefix()).append(option.getOpt());
}
else {
buff.append(getLongOptPrefix()).append(option.getLongOpt());
}
// if the Option has a value
if (option.hasArg() && option.hasArgName()) {
if (option.hasOptionalArg()) {
buff.append('[');
}
if (option.hasValueSeparator()) {
buff.append(option.getValueSeparator());
}
else {
buff.append(' ');
}
buff.append('<').append(option.getArgName()).append('>');
if (option.hasOptionalArg()) {
buff.append(']');
}
}
// if the Option is not a required option
if (!required) {
buff.append(']');
}
}
private Object getOptionValueFromCmdLine(Option option, CommandLine cmd) {
Object optionValue = null;
if(!option.hasArg()) {
if(cmd.hasOption(option.getLongOpt())) {
optionValue = true;
}
}
else {
optionValue = cmd.getOptionValue(option.getLongOpt());
}
return optionValue;
}
/**
* Return Mame only args
*/
public String[] getMameRawArgs () {
List<String> mra = new ArrayList<>();
optionsLoop: for (Option o: this.commandLine.getOptions()) {
// Discard ia-rcade options
for (Option ia: this.iaRcadeOptions.getOptions()) {
if (o.getOpt().equals(ia.getOpt())) {
continue optionsLoop;
}
}
mra.add('-' + o.getOpt());
if (o.hasArg() || o.hasArgs()) {
for (String a: this.commandLine.getOptionValues(o.getOpt())) {
mra.add(a);
}
}
}
// Add non option args
for (String arg: this.commandLine.getArgs()) {
mra.add(arg);
}
return mra.toArray(new String[mra.size()]);
}
/**
* 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();
}
/**
* Appends the usage clause for an Option to a StringBuffer.
*
* @param buff
* the StringBuffer to append to
* @param option
* the Option to append
* @param required
* whether the Option is required or not
*/
protected void appendOption(final StringBuilder buff, final Option option,
final boolean required) {
if (!required) {
buff.append('[');
}
if (option.getOpt() != null) {
buff.append(getOptPrefix()).append(option.getOpt());
}
else {
buff.append(getLongOptPrefix()).append(option.getLongOpt());
}
// if the Option has a value
if (option.hasArg() && option.hasArgName()) {
if (option.hasOptionalArg()) {
buff.append('[');
}
if (option.hasValueSeparator()) {
buff.append(option.getValueSeparator());
}
else {
buff.append(' ');
}
buff.append('<').append(option.getArgName()).append('>');
if (option.hasOptionalArg()) {
buff.append(']');
}
}
// if the Option is not a required option
if (!required) {
buff.append(']');
}
}
String parse(Option option, UnaryOperator<String> parser) {
if (option.hasArgs()) {
return option.getValuesList()
.stream()
.map(u -> parse(option.getLongOpt(), u, parser))
.collect(Collectors.joining(" "));
}
if (option.hasArg()) {
return parse(option.getLongOpt(), option.getValue(), parser);
}
return Boolean.TRUE.toString();
}
public String getOptionsAsString() {
StringBuilder buf = new StringBuilder();
for (Option option : commandLine.getOptions()) {
buf.append(" ");
buf.append(option.getOpt());
if (option.hasArg()) {
buf.append("=");
buf.append(option.getValue());
}
}
return buf.toString();
}
public String getOptionsAsString() {
StringBuilder buf = new StringBuilder();
for (Option option : commandLine.getOptions()) {
buf.append(" ");
buf.append(option.getOpt());
if (option.hasArg()) {
buf.append("=");
buf.append(option.getValue());
}
}
return buf.toString();
}
protected void logCli(CommandLine cli) {
StringBuilder optsDbg = new StringBuilder();
for (Option opt : cli.getOptions()) {
if (opt.hasArg()) {
optsDbg.append(opt.getOpt()).append("=").append(opt.getValue());
} else {
optsDbg.append(opt.getOpt());
}
optsDbg.append("\n");
}
log.info("Starting "+this.getClass().getSimpleName()+" with args: "+optsDbg);
}
/**
* Return non command Mame options args
*/
public String[] getMameOptionsRawArgs () {
List<String> roa = new ArrayList<>();
optionsLoop: for (Option o: this.commandLine.getOptions()) {
// Discard mame commands
for (Option cmd: this.mameOptions.getCommands().getOptions()) {
if (o.getOpt().equals(cmd.getOpt())) {
continue optionsLoop;
}
}
// Discard media types Mame options (ex -cart mario)
for (Option mt: this.mameOptions.getMediaTypes().getOptions()) {
if (o.getOpt().equals(mt.getOpt())) {
continue optionsLoop;
}
}
// Discard ia-rcade options
for (Option ia: this.iaRcadeOptions.getOptions()) {
if (o.getOpt().equals(ia.getOpt())) {
continue optionsLoop;
}
}
roa.add('-' + o.getOpt());
if (o.hasArg() || o.hasArgs()) {
for (String a: this.commandLine.getOptionValues(o.getOpt())) {
roa.add(a);
}
}
}
return roa.toArray(new String[roa.size()]);
}
@SuppressWarnings("unchecked")
@Override
protected StringBuffer renderOptions(final StringBuffer sb, final int width, final Options options, final int leftPad, final int descPad) {
final String lpad = this.createPadding(leftPad);
final String dpad = this.createPadding(8); // we use a fixed value instead of descPad
StringBuilder optBuf;
final List<Option> optList = new ArrayList<Option>(options.getOptions());
Collections.sort(optList, this.getOptionComparator());
for (final Iterator<Option> i = optList.iterator(); i.hasNext();) {
final Option option = i.next();
optBuf = new StringBuilder(8);
if (option.getOpt() == null) {
optBuf.append(lpad).append(" ").append(this.getLongOptPrefix()).append(option.getLongOpt());
} else {
optBuf.append(lpad).append(this.getOptPrefix()).append(option.getOpt());
if (option.hasLongOpt()) {
optBuf.append(',').append(this.getLongOptPrefix()).append(option.getLongOpt());
}
}
if (option.hasArg()) {
if (option.hasArgName()) {
optBuf.append(" <").append(option.getArgName()).append('>');
} else {
optBuf.append(' ');
}
}
sb.append(optBuf.toString()).append(this.getNewLine());
optBuf = new StringBuilder();
optBuf.append(dpad);
if (option.getDescription() != null) {
optBuf.append(option.getDescription());
}
this.renderWrappedText(sb, width, dpad.length(), optBuf.toString());
if (i.hasNext()) {
sb.append(this.getNewLine());
sb.append(this.getNewLine());
}
}
return sb;
}
public Properties from(Properties parent, CommandLineOptions commandLineOptions, String[] args)
throws ParseException {
Properties properties = new Properties(parent);
CommandLineParser parser = new DefaultParser();
Options options = commandLineOptions.options();
CommandLine cmd = parser.parse(options, args);
switch (cmd.getArgList().size()) {
case 0:
// No authentication credentials
break;
case 1:
// Authentication token
properties.put(Property.AUTHENTICATION_TOKEN.name(), cmd.getArgList().get(0));
break;
case 2:
// AppleId/ password pair
properties.put(Property.AUTHENTICATION_APPLEID.name(), cmd.getArgList().get(0));
properties.put(Property.AUTHENTICATION_PASSWORD.name(), cmd.getArgList().get(1));
break;
default:
throw new ParseException(
"Too many non-optional arguments, expected appleid/ password or authentication token only.");
}
Iterator<Option> it = cmd.iterator();
while (it.hasNext()) {
Option option = it.next();
String opt = commandLineOptions.opt(option);
String property = commandLineOptions.property(option).name();
if (option.hasArgs()) {
// String array
properties.put(property, joined(cmd.getOptionValues(opt)));
} else if (option.hasArg()) {
// String value
properties.put(property, cmd.getOptionValue(opt));
} else {
// String boolean
properties.put(property, Boolean.toString(cmd.hasOption(opt)));
}
}
return properties;
}
/** Render the option help to a Markdown table.
*
* @param options the options.
* @return the markdown table.
*/
protected static String _renderToMarkdown(Options options) {
if (options == null) {
return ""; //$NON-NLS-1$
}
final List<Option> optList = new ArrayList<>(options.getOptions());
if (optList.isEmpty()) {
return ""; //$NON-NLS-1$
}
Collections.sort(optList, new OptionComparator());
final StringBuilder buffer = new StringBuilder();
for (final Option option : optList) {
buffer.append("| `"); //$NON-NLS-1$
if (option.getOpt() == null) {
buffer.append(DEFAULT_LONG_OPT_PREFIX).append(option.getLongOpt());
} else {
buffer.append(DEFAULT_OPT_PREFIX).append(option.getOpt());
if (option.hasLongOpt()) {
buffer.append("`, `"); //$NON-NLS-1$
buffer.append(DEFAULT_LONG_OPT_PREFIX).append(option.getLongOpt());
}
}
if (option.hasArg()) {
if (option.hasArgName()) {
buffer.append(" <").append(option.getArgName()).append(">"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
buffer.append("` | "); //$NON-NLS-1$
if (option.getDescription() != null) {
String text = option.getDescription().replaceAll("[ \t\n\r\f]+", " "); //$NON-NLS-1$ //$NON-NLS-2$
text = text.replaceAll("\\<", "<"); //$NON-NLS-1$//$NON-NLS-2$
text = text.replaceAll("\\>", ">"); //$NON-NLS-1$//$NON-NLS-2$
buffer.append(text);
}
buffer.append(" |\n"); //$NON-NLS-1$
}
return buffer.toString();
}