下面列出了org.apache.commons.cli.Options#getOptions ( ) 实例代码,或者点击链接到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());
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Options options = EQTLInteractionAnalyser.OPTIONS;
System.out.println("| Short | Long | Description |");
System.out.println("|-------|------|-------------|");
for(Object optionO : options.getOptions()){
Option option = (Option) optionO;
System.out.print("| -");
System.out.print(option.getOpt());
System.out.print(" | --");
System.out.print(option.getLongOpt());
System.out.print(" | ");
System.out.print(option.getDescription());
System.out.println(" | ");
}
}
static Options buildGeneralOptions(Options opts) {
Options r = new Options();
for (Object o : opts.getOptions())
r.addOption((Option) o);
Option libjars =
OptionBuilder.withArgName("paths").hasArg().withDescription("comma separated jars to be used by the submitted topology").create("libjars");
r.addOption(libjars);
optionProcessors.put("libjars", new LibjarsProcessor());
Option conf = OptionBuilder.withArgName("configuration file").hasArg().withDescription("an application configuration file").create("conf");
r.addOption(conf);
optionProcessors.put("conf", new ConfFileProcessor());
// Must come after `conf': this option is of higher priority
Option extraConfig = OptionBuilder.withArgName("D").hasArg().withDescription("extra configurations (preserving types)").create("D");
r.addOption(extraConfig);
optionProcessors.put("D", new ExtraConfigProcessor());
return r;
}
private static Options buildGeneralOptions(Options opts) {
Options r = new Options();
for (Object o : opts.getOptions())
r.addOption((Option) o);
Option workerNum = OptionBuilder.withArgName("worker num per batch").hasArg()
.withDescription("number of workers to upgrade").create("n");
r.addOption(workerNum);
Option comp = OptionBuilder.withArgName("component to upgrade").hasArg()
.withDescription("component id to upgrade, note that only one component is allowed at a time")
.create("p");
r.addOption(comp);
Option workers = OptionBuilder.withArgName("workers to upgrade").hasArg()
.withDescription("workers to upgrade, in the format: host1:port1,host2:port2,...").create("w");
r.addOption(workers);
return r;
}
protected Collection<Option> getOptions(SubCommand subCommand) {
Options options = new Options();
subCommand.buildCommandlineOptions(options);
@SuppressWarnings("unchecked")
Collection<Option> col = options.getOptions();
return col;
}
private void addAll(String sectionName, Options adding)
{
//noinspection unchecked
for ( Option o : (Iterable<? extends Option>)adding.getOptions() )
{
options.addOption(o);
}
if ( sectionName != null )
{
sections.add(new OptionSection(sectionName, adding));
}
}
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);
}
}
}
public static void mergeOptionsInto(Options opt, Options opts) {
if (opts == null) {
return;
}
Collection<Option> all = opts.getOptions();
if (all != null && !all.isEmpty()) {
for (Option o : all) {
opt.addOption(o);
}
}
}
public static void mergeOptionsInto(Options opt, Options opts) {
if (opts == null) {
return;
}
Collection<Option> all = opts.getOptions();
if (all != null && !all.isEmpty()) {
for (Option o : all) {
opt.addOption(o);
}
}
}
private void printUsage() {
System.err.println("Analyzer base options are");
Options options = buildOptions();
for (Object obj : options.getOptions()) {
Option option = (Option) obj;
System.err.println(option.getArgName() + " : " + option.getDescription());
}
}
public static Options buildOptions(Options subClassOptions) {
/* Add the options from subClass */
Options options = new Options();
for (Object subClassOption : subClassOptions.getOptions()) {
Option option = (Option) subClassOption;
options.addOption(option);
}
/* Add the general options */
OptionBuilder.withArgName(OPTION_HELP);
OptionBuilder.withLongOpt(OPTION_HELP);
OptionBuilder.isRequired(false);
OptionBuilder.withDescription("Print help message and exit");
options.addOption(OptionBuilder.create('h'));
OptionBuilder.withArgName(OPTION_INPUT_FILE);
OptionBuilder.withLongOpt(OPTION_INPUT_FILE);
OptionBuilder.hasArg();
OptionBuilder.isRequired(false);
OptionBuilder.withDescription("Input file name or 'stdin'");
options.addOption(OptionBuilder.create('f'));
OptionBuilder.withArgName(OPTION_INPUT_DIR);
OptionBuilder.withLongOpt(OPTION_INPUT_DIR);
OptionBuilder.hasArg();
OptionBuilder.isRequired(false);
OptionBuilder.withDescription("Input dir name");
options.addOption(OptionBuilder.create('d'));
OptionBuilder.withArgName(OPTION_SOURCES);
OptionBuilder.withLongOpt(OPTION_SOURCES);
OptionBuilder.hasArg();
OptionBuilder.isRequired(false);
OptionBuilder.withDescription("Source Names.");
options.addOption(OptionBuilder.create('s'));
OptionBuilder.withArgName(OPTION_OS_TYPE);
OptionBuilder.withLongOpt(OPTION_OS_TYPE);
OptionBuilder.hasArg();
OptionBuilder.isRequired(false);
OptionBuilder.withDescription("The OS Type."
+ "If this option is omitted, the default is Linux");
options.addOption(OptionBuilder.create('o'));
OptionBuilder.withArgName(OPTION_GMT_OFFSET);
OptionBuilder.withLongOpt(OPTION_GMT_OFFSET);
OptionBuilder.hasArg();
OptionBuilder.isRequired(false);
OptionBuilder.withDescription("hours offset from GMT");
options.addOption(OptionBuilder.create('g'));
return options;
}
@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;
}
/** 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();
}