use of picocli.CommandLine.Model.OptionSpec in project keycloak by keycloak.
the class PropertyMapperParameterConsumer method validateOption.
private void validateOption(Stack<String> args, ArgSpec argSpec, CommandSpec commandSpec) {
OptionSpec option = (OptionSpec) argSpec;
String name = String.join(", ", option.names());
CommandLine commandLine = commandSpec.commandLine();
if (args.isEmpty() || !isOptionValue(args.peek())) {
throw new ParameterException(commandLine, "Missing required value for option '" + name + "' (" + argSpec.paramLabel() + ")." + getExpectedValuesMessage(argSpec, option));
}
// consumes the value
String value = args.pop();
if (!args.isEmpty() && isOptionValue(args.peek())) {
throw new ParameterException(commandLine, "Option '" + name + "' expects a single value (" + argSpec.paramLabel() + ")" + getExpectedValuesMessage(argSpec, option));
}
if (isExpectedValue(option, value)) {
return;
}
throw new ParameterException(commandLine, "Invalid value for option '" + name + "': " + value + "." + getExpectedValuesMessage(argSpec, option));
}
use of picocli.CommandLine.Model.OptionSpec in project checkstyle by checkstyle.
the class CliOptionsXdocsSyncTest method validateCliUsageSection.
@Test
public void validateCliUsageSection() throws Exception {
final NodeList sections = getSectionsFromXdoc("src/xdocs/cmdline.xml.vm");
final Node usageSource = XmlUtil.getFirstChildElement(sections.item(2));
final String usageText = XmlUtil.getFirstChildElement(usageSource).getTextContent();
final Set<String> shortParamsXdoc = getParameters(usageText, "-[a-zA-CE-X]\\b");
final Set<String> longParamsXdoc = getParameters(usageText, "-(-\\w+)+");
final Class<?> cliOptions = Class.forName("com.puppycrawl.tools.checkstyle" + ".Main$CliOptions");
final CommandLine commandLine = new CommandLine(cliOptions);
final Set<String> shortParamsMain = commandLine.getCommandSpec().options().stream().map(OptionSpec::shortestName).collect(Collectors.toSet());
final Set<String> longParamsMain = commandLine.getCommandSpec().options().stream().map(OptionSpec::longestName).filter(names -> names.length() != 2).collect(Collectors.toSet());
assertWithMessage("Short parameters in Main.java and cmdline" + ".xml.vm should match").that(shortParamsMain).isEqualTo(shortParamsXdoc);
assertWithMessage("Long parameters in Main.java and cmdline" + ".xml.vm should match").that(longParamsMain).isEqualTo(longParamsXdoc);
}
use of picocli.CommandLine.Model.OptionSpec in project checkstyle by checkstyle.
the class CliOptionsXdocsSyncTest method validateCliDocSections.
@Test
public void validateCliDocSections() throws Exception {
final Map<String, String> cmdDesc = new HashMap<>();
final NodeList sections = getSectionsFromXdoc("src/xdocs/cmdline.xml.vm");
final Set<String> cmdOptions = getListById(sections.item(2), "CLI_Options");
for (String option : cmdOptions) {
final String text = option.trim().replaceAll("\\s+", " ");
cmdDesc.put(text.substring(0, 2), text.substring(text.indexOf(" - ") + 3));
}
final Class<?> cliOptions = Class.forName("com.puppycrawl.tools.checkstyle" + ".Main$CliOptions");
final CommandLine commandLine = new CommandLine(cliOptions);
final List<OptionSpec> optionSpecList = commandLine.getCommandSpec().options();
for (OptionSpec opt : optionSpecList) {
final String option = opt.names()[0];
if ("-h".equals(option) || "-V".equals(option)) {
cmdDesc.remove(option);
continue;
}
final String descXdoc = cmdDesc.get(option);
final String descMain = opt.description()[0];
assertWithMessage("CLI Option: " + option + " present in " + "Main.java but not documented in cmdline.xml.vm").that(descXdoc).isNotNull();
assertWithMessage("CLI options descriptions in xdoc: " + " should match that of in Main.java").that(descMain).isEqualTo(descXdoc);
cmdDesc.remove(option);
}
assertWithMessage("CLI Options: %s present in cmdline.xml.vm, not documented in Main.java", cmdDesc).that(cmdDesc).isEmpty();
}
Aggregations