Search in sources :

Example 1 with OptionSpec

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));
}
Also used : OptionSpec(picocli.CommandLine.Model.OptionSpec) CommandLine(picocli.CommandLine) ParameterException(picocli.CommandLine.ParameterException)

Example 2 with OptionSpec

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);
}
Also used : OptionSpec(picocli.CommandLine.Model.OptionSpec) NodeList(org.w3c.dom.NodeList) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Files(java.nio.file.Files) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) List(java.util.List) Matcher(java.util.regex.Matcher) Paths(java.nio.file.Paths) Document(org.w3c.dom.Document) Map(java.util.Map) Node(org.w3c.dom.Node) Pattern(java.util.regex.Pattern) OptionSpec(picocli.CommandLine.Model.OptionSpec) Path(java.nio.file.Path) XmlUtil(com.puppycrawl.tools.checkstyle.internal.utils.XmlUtil) CommandLine(picocli.CommandLine) CommandLine(picocli.CommandLine) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Test(org.junit.jupiter.api.Test)

Example 3 with OptionSpec

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();
}
Also used : OptionSpec(picocli.CommandLine.Model.OptionSpec) CommandLine(picocli.CommandLine) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Test(org.junit.jupiter.api.Test)

Aggregations

CommandLine (picocli.CommandLine)3 OptionSpec (picocli.CommandLine.Model.OptionSpec)3 HashMap (java.util.HashMap)2 Test (org.junit.jupiter.api.Test)2 NodeList (org.w3c.dom.NodeList)2 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)1 XmlUtil (com.puppycrawl.tools.checkstyle.internal.utils.XmlUtil)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1 ParameterException (picocli.CommandLine.ParameterException)1