io.vertx.core.cli.CLI Java Examples

The following examples show how to use io.vertx.core.cli.CLI. 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: ShellExamples.java    From vertx-shell with Apache License 2.0 6 votes vote down vote up
public void cliCommand() {
  CLI cli = CLI.create("my-command").
    addArgument(new Argument().setArgName("my-arg")).
    addOption(new Option().setShortName("m").setLongName("my-option"));
  CommandBuilder command = CommandBuilder.command(cli);
  command.processHandler(process -> {

    CommandLine commandLine = process.commandLine();

    String argValue = commandLine.getArgumentValue(0);
    String optValue = commandLine.getOptionValue("my-option");
    process.write("The argument is " + argValue + " and the option is " + optValue);

    process.end();
  });
}
 
Example #2
Source File: Main.java    From vertx-maven-plugin with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    CLI cli = CLI.create("copy")
        .setSummary("A command line interface to copy files")
        .addOption(new Option()
            .setLongName("directory")
            .setShortName("R")
            .setDescription("enables directory support")
            .setFlag(true))
        .addArgument(new Argument()
            .setDescription("The source")
            .setArgName("source"))
        .addArgument(new Argument()
            .setDescription("The target")
            .setArgName("target"));

    StringBuilder builder = new StringBuilder();
    cli.usage(builder);
    if (args.length < 3) {
        System.out.println(builder.toString());
        System.exit(0);
    }

    CommandLine commandLine = cli.parse(Arrays.asList(args));

    String sourceDir = commandLine.allArguments().get(0);
    String targetDir = commandLine.allArguments().get(1);

    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(SimpleVerticle.class.getName(), new DeploymentOptions()
        .setConfig(new JsonObject()
            .put("sourceDir", sourceDir)
            .put("targetDir", targetDir)), event -> {
        vertx.close();
        System.exit(0);
    });

}
 
Example #3
Source File: Main.java    From vertx-maven-plugin with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    CLI cli = CLI.create("copy")
        .setSummary("A command line interface to copy files")
        .addOption(new Option()
            .setLongName("directory")
            .setShortName("R")
            .setDescription("enables directory support")
            .setFlag(true))
        .addArgument(new Argument()
            .setDescription("The source")
            .setArgName("source"))
        .addArgument(new Argument()
            .setDescription("The target")
            .setArgName("target"));

    StringBuilder builder = new StringBuilder();
    cli.usage(builder);
    if (args.length < 3) {
        System.out.println(builder.toString());
        System.exit(0);
    }

    CommandLine commandLine = cli.parse(Arrays.asList(args));

    String sourceDir = commandLine.allArguments().get(0);
    String targetDir = commandLine.allArguments().get(1);

    Vertx vertx = Vertx.vertx();
    vertx.deployVerticle(SimpleVerticle.class.getName(), new DeploymentOptions()
        .setConfig(new JsonObject()
            .put("sourceDir", sourceDir)
            .put("targetDir", targetDir)), event -> {
        vertx.close();
        System.exit(0);
    });

}
 
Example #4
Source File: Cli.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private boolean parserArgs(String[] args) {

        CLI parser = new DefaultCLI().setName("broker-cli.jar");
        parser.addOption(new Option()
            .setLongName("command")
            .setShortName("c")
            .setRequired(true)
            .addChoice("purgeQueue")
            .addChoice("getMessageCount")
            .setDescription("Command to run on broker"));
        parser.addOption(new Option()
            .setLongName("queue")
            .setShortName("q")
            .setRequired(true)
            .setDescription("Queue to run command against"));

        StringBuilder builder = new StringBuilder();
        parser.usage(builder);

        try {
            CommandLine cl = parser.parse(Arrays.asList(args));
            this.command = cl.getOptionValue("c");
            this.queue = cl.getOptionValue("q");
            return true;
        } catch (Exception e) {
            log.info("\n" + builder.toString());
        }
        return false;
    }
 
Example #5
Source File: ShellExamples.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
public void cliCommandWithHelp() {
  CLI cli = CLI.create("my-command").
    addArgument(new Argument().setArgName("my-arg")).
    addOption(new Option().setArgName("help").setShortName("h").setLongName("help"));
  CommandBuilder command = CommandBuilder.command(cli);
  command.processHandler(process -> {
    // ...
  });
}
 
Example #6
Source File: CommandBuilderImpl.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Override
public Command build(Vertx vertx) {
  Context context = vertx.getOrCreateContext();
  return new Command() {
    @Override
    public String name() {
      return name;
    }
    @Override
    public CLI cli() {
      return cli;
    }

    @Override
    public Process createProcess(List<CliToken> args) {
      return new ProcessImpl(vertx, context, this, args, processHandler);
    }

    @Override
    public void complete(Completion completion) {
      if (completeHandler != null) {
        context.runOnContext(v -> {
          try {
            completeHandler.handle(completion);
          } catch (Throwable t) {
            completion.complete(Collections.emptyList());
            throw t;
          }
        });
      } else {
        Command.super.complete(completion);
      }
    }
  };
}
 
Example #7
Source File: Echo.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
@Override
public CLI cli() {
  // CLI does not support variable arguments yet
  return null;
}
 
Example #8
Source File: Command.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
/**
 * @return the command line interface, can be null
 */
default CLI cli() {
  return null;
}
 
Example #9
Source File: CommandBuilderImpl.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
public CommandBuilderImpl(String name, CLI cli) {
  this.name = name;
  this.cli = cli;
}
 
Example #10
Source File: AnnotatedCommand.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
/**
 * @return the command line interface, can be null
 */
public CLI cli() {
  return null;
}
 
Example #11
Source File: TestCommandFactory.java    From vertx-unit with Apache License 2.0 4 votes vote down vote up
@Override
public CLI define() {
  CLI cli = super.define();
  cli.getArgument(0).setArgName("test-verticle");
  return cli;
}
 
Example #12
Source File: CommandBuilder.java    From vertx-shell with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new commmand with its {@link io.vertx.core.cli.CLI} descriptor. This command can then retrieve the parsed
 * {@link CommandProcess#commandLine()} when it executes to know get the command arguments and options.
 *
 * @param cli the cli to use
 * @return the command
 */
static CommandBuilder command(CLI cli) {
  return new CommandBuilderImpl(cli.getName(), cli);
}