Java Code Examples for org.apache.commons.cli.Option#hasArg()

The following examples show how to use org.apache.commons.cli.Option#hasArg() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PublicMethodsCliObjectFactory.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
  }
}
 
Example 2
Source File: CliUtil.java    From termsuite-core with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example 3
Source File: ExhibitorCLI.java    From exhibitor with Apache License 2.0 6 votes vote down vote up
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());
            }
        }
    }
}
 
Example 4
Source File: ArgsManager.java    From InflatableDonkey with MIT License 5 votes vote down vote up
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();
}
 
Example 5
Source File: SolrClient.java    From yarn-proto with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 6
Source File: OptionsHelper.java    From Kylin with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: OptionsHelper.java    From kylin with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: OptionsHelper.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 9
Source File: GfxdHelpFormatter.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 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(']');
  }
}
 
Example 10
Source File: ArgumentParserTest.java    From herd with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 11
Source File: CommandLineArguments.java    From ia-rcade with Apache License 2.0 5 votes vote down vote up
/**
 * 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()]);

}
 
Example 12
Source File: ParametersHelper.java    From vsphere-automation-sdk-java with MIT License 5 votes vote down vote up
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;
}
 
Example 13
Source File: GfxdHelpFormatter.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 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(']');
  }
}
 
Example 14
Source File: CommandlineOption.java    From marklogic-contentpump with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 15
Source File: CLIHelpFormatter.java    From kieker with Apache License 2.0 4 votes vote down vote up
@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;
}
 
Example 16
Source File: CommandLineArguments.java    From ia-rcade with Apache License 2.0 4 votes vote down vote up
/**
 * 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()]);
}
 
Example 17
Source File: CommandLinePropertiesFactory.java    From LiquidDonkey with MIT License 4 votes vote down vote up
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;
}
 
Example 18
Source File: MarkdownExtensions.java    From sarl with Apache License 2.0 4 votes vote down vote up
/** 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("\\<", "&lt;");  //$NON-NLS-1$//$NON-NLS-2$
			text = text.replaceAll("\\>", "&gt;");  //$NON-NLS-1$//$NON-NLS-2$
			buffer.append(text);
		}

		buffer.append(" |\n"); //$NON-NLS-1$
	}

	return buffer.toString();
}