Search in sources :

Example 26 with CommandLine

use of picocli.CommandLine in project checkstyle by checkstyle.

the class JavadocPropertiesGenerator method main.

/**
 * TokenTypes.properties generator entry point.
 *
 * @param args the command line arguments
 * @throws CheckstyleException if parser or lexer failed or if there is an IO problem
 */
public static void main(String... args) throws CheckstyleException {
    final CliOptions cliOptions = new CliOptions();
    final CommandLine cmd = new CommandLine(cliOptions).setUsageHelpWidth(USAGE_HELP_WIDTH);
    try {
        final ParseResult parseResult = cmd.parseArgs(args);
        if (parseResult.isUsageHelpRequested()) {
            cmd.usage(System.out);
        } else {
            writePropertiesFile(cliOptions);
        }
    } catch (ParameterException ex) {
        System.err.println(ex.getMessage());
        ex.getCommandLine().usage(System.err);
    }
}
Also used : CommandLine(picocli.CommandLine) ParseResult(picocli.CommandLine.ParseResult) ParameterException(picocli.CommandLine.ParameterException)

Example 27 with CommandLine

use of picocli.CommandLine 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 28 with CommandLine

use of picocli.CommandLine in project snow-owl by b2ihealthcare.

the class SnowOwlCommandProvider method _snowowl.

public void _snowowl(CommandInterpreter interpreter) throws Exception {
    // first read all args into an array
    List<String> args = newArrayList();
    String arg;
    while ((arg = interpreter.nextArgument()) != null) {
        args.add(arg);
    }
    final Environment env = ApplicationContext.getServiceForClass(Environment.class);
    final List<CommandLine> commands = cli(env).parse(args.toArray(new String[] {}));
    try (InterpreterStream out = new InterpreterStream(interpreter)) {
        // print help if requested for any command
        if (CommandLine.printHelpIfRequested(commands, out, out, CommandLine.Help.Ansi.AUTO)) {
            return;
        }
        // get the last command used in the cli
        CommandLine cli = Iterables.getLast(commands, null);
        if (cli == null) {
            return;
        }
        // we should get an executable Snow Owl Command, so execute it
        BaseCommand cmd = (BaseCommand) cli.getCommand();
        final String authorizationToken = ApplicationContext.getServiceForClass(JWTGenerator.class).generate(User.SYSTEM);
        final ServiceProvider context = env.inject().bind(IEventBus.class, new AuthorizedEventBus(ApplicationContext.getServiceForClass(IEventBus.class), ImmutableMap.of(AuthorizedRequest.AUTHORIZATION_HEADER, authorizationToken))).build();
        cmd.setContext(context);
        cmd.run(out);
    } catch (Exception e) {
        interpreter.println("Unknown error occured");
        interpreter.printStackTrace(e);
    }
}
Also used : CommandLine(picocli.CommandLine) JWTGenerator(com.b2international.snowowl.core.identity.JWTGenerator) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) Environment(com.b2international.snowowl.core.setup.Environment) AuthorizedEventBus(com.b2international.snowowl.core.authorization.AuthorizedEventBus) IEventBus(com.b2international.snowowl.eventbus.IEventBus) IOException(java.io.IOException)

Example 29 with CommandLine

use of picocli.CommandLine in project snow-owl by b2ihealthcare.

the class SnowOwlCommandProvider method cli.

private CommandLine cli(ServiceProvider context) {
    final CommandLine cli = new CommandLine(new SnowOwlCommand());
    cli.setUsageHelpWidth(USAGE_WIDTH);
    cli.addSubcommand("help", new CommandLine.HelpCommand());
    context.service(ClassPathScanner.class).getComponentsBySuperclass(Command.class).stream().sorted(Ordering.natural().onResultOf(Command::getCommand)).forEach(cmd -> {
        if (cmd.getClass().isAnnotationPresent(picocli.CommandLine.Command.class)) {
            cli.addSubcommand(cmd.getCommand(), cmd);
        }
    });
    return cli;
}
Also used : CommandLine(picocli.CommandLine)

Example 30 with CommandLine

use of picocli.CommandLine 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)45 Test (org.junit.jupiter.api.Test)22 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)19 Copier (org.neo4j.pushtocloud.PushToCloudCommand.Copier)17 Path (java.nio.file.Path)9 DumpCreator (org.neo4j.pushtocloud.PushToCloudCommand.DumpCreator)5 ParseResult (picocli.CommandLine.ParseResult)5 IOException (java.io.IOException)4 PrintWriter (java.io.PrintWriter)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Set (java.util.Set)3 ParameterException (picocli.CommandLine.ParameterException)3 PrintStream (java.io.PrintStream)2 Files (java.nio.file.Files)2 Map (java.util.Map)2 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 AuthorizedEventBus (com.b2international.snowowl.core.authorization.AuthorizedEventBus)1 JWTGenerator (com.b2international.snowowl.core.identity.JWTGenerator)1 Environment (com.b2international.snowowl.core.setup.Environment)1