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(); }); }
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 -> { // ... }); }
@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); } } }; }
@Override public CLI cli() { // CLI does not support variable arguments yet return null; }
/** * @return the command line interface, can be null */ default CLI cli() { return null; }
public CommandBuilderImpl(String name, CLI cli) { this.name = name; this.cli = cli; }
/** * @return the command line interface, can be null */ public CLI cli() { return null; }
@Override public CLI define() { CLI cli = super.define(); cli.getArgument(0).setArgName("test-verticle"); return cli; }
/** * 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); }