org.kohsuke.args4j.spi.OptionHandler Java Examples

The following examples show how to use org.kohsuke.args4j.spi.OptionHandler. 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: PartialCmdLineParser.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private OptionHandler<?> findOptionHandler(String name) {
	// Look for key/value pair first.
	int pos = name.indexOf(getProperties().getOptionValueDelimiter());
	if (pos < 0) {
		pos = name.indexOf('='); // historical compatibility fallback
	}
	if (pos > 0) {
		name = name.substring(0, pos);
	}
	return findOptionByName(name);
}
 
Example #2
Source File: PartialCmdLineParser.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private OptionHandler<?> findOptionByName(String name) {
	for (OptionHandler<?> h : getOptions()) {
		NamedOptionDef option = (NamedOptionDef) h.option;
		if (name.equals(option.name())) {
			return h;
		}
		for (String alias : option.aliases()) {
			if (name.equals(alias)) {
				return h;
			}
		}
	}
	return null;
}
 
Example #3
Source File: PartialCmdLineParser.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean isHandlerHasHisOptions(NamedOptionDef option,
		Set<OptionHandler<?>> present) {
	for (String depend : option.depends()) {
		if (!present.contains(findOptionHandler(depend)))
			return false;
	}
	return true;
}
 
Example #4
Source File: PartialCmdLineParser.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private boolean isHandlerAllowOtherOptions(NamedOptionDef option,
		Set<OptionHandler<?>> present) {
	for (String forbid : option.forbids()) {
		if (present.contains(findOptionHandler(forbid)))
			return false;
	}
	return true;
}
 
Example #5
Source File: CmdLineParserWithPrintInformation.java    From buck with Apache License 2.0 5 votes vote down vote up
private int getPrefixLen(OptionHandler<?> h) {
  if (h.option.usage().isEmpty()) {
    return 0;
  }

  return h.getNameAndMeta(null, getProperties()).length();
}
 
Example #6
Source File: GlobalCliOptions.java    From buck with Apache License 2.0 5 votes vote down vote up
public static boolean isGlobalOption(OptionHandler<?> optionHandler) {
  OptionDef option = optionHandler.option;
  if (option instanceof NamedOptionDef) {
    NamedOptionDef namedOption = (NamedOptionDef) option;
    return GLOBAL_OPTIONS.contains(namedOption.name());
  }
  return false;
}
 
Example #7
Source File: PartialCmdLineParser.java    From jsonix-schema-compiler with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public int parseArgument(final String[] args, final int position)
		throws CmdLineException {
	Validate.noNullElements(args);
	currentOptionHandler = null;

	CmdLineImpl cmdLine = new CmdLineImpl(args, position);

	Set<OptionHandler<?>> present = new HashSet<OptionHandler<?>>();
	int argIndex = position;
	int consumed = 0;

	while (cmdLine.hasMore()) {
		String arg = cmdLine.getCurrentToken();
		if (isOption(arg)) {
			// '=' is for historical compatibility fallback
			boolean isKeyValuePair = arg.contains(getProperties()
					.getOptionValueDelimiter()) || arg.indexOf('=') != -1;

			// parse this as an option.
			currentOptionHandler = isKeyValuePair ? findOptionHandler(arg)
					: findOptionByName(arg);

			if (currentOptionHandler == null) {
				return consumed;
			}

			// known option; skip its name
			if (isKeyValuePair) {
				cmdLine.splitToken();
			} else {
				cmdLine.proceed(1);
				consumed++;
			}
		} else {
			if (argIndex >= getArguments().size()) {
				return consumed;
			}

			// known argument
			currentOptionHandler = getArguments().get(argIndex);
			if (currentOptionHandler == null) // this is a programmer error.
												// arg index should be
												// continuous
				throw new IllegalStateException("@Argument with index="
						+ argIndex + " is undefined");

			if (!currentOptionHandler.option.isMultiValued())
				argIndex++;
		}
		int diff = currentOptionHandler.parseArguments(cmdLine);
		cmdLine.proceed(diff);
		consumed += diff;
		present.add(currentOptionHandler);
	}

	// check whether a help option is set
	boolean helpSet = false;
	for (OptionHandler<?> handler : getOptions()) {
		if (handler.option.help() && present.contains(handler)) {
			helpSet = true;
		}
	}

	if (!helpSet) {
		checkRequiredOptionsAndArguments(present);
	}

	return consumed;
}