Search in sources :

Example 1 with Command

use of org.springframework.boot.cli.command.Command in project spring-boot by spring-projects.

the class Shell method getCommands.

private Iterable<Command> getCommands() {
    List<Command> commands = new ArrayList<>();
    ServiceLoader<CommandFactory> factories = ServiceLoader.load(CommandFactory.class, getClass().getClassLoader());
    for (CommandFactory factory : factories) {
        for (Command command : factory.getCommands()) {
            commands.add(convertToForkCommand(command));
        }
    }
    commands.add(new PromptCommand(this.prompts));
    commands.add(new ClearCommand(this.consoleReader));
    commands.add(new ExitCommand());
    return commands;
}
Also used : Command(org.springframework.boot.cli.command.Command) VersionCommand(org.springframework.boot.cli.command.core.VersionCommand) HelpCommand(org.springframework.boot.cli.command.core.HelpCommand) ArrayList(java.util.ArrayList) CommandFactory(org.springframework.boot.cli.command.CommandFactory)

Example 2 with Command

use of org.springframework.boot.cli.command.Command in project spring-boot by spring-projects.

the class HelpCommand method run.

@Override
public ExitStatus run(String... args) throws Exception {
    if (args.length == 0) {
        throw new NoHelpCommandArgumentsException();
    }
    String commandName = args[0];
    for (Command command : this.commandRunner) {
        if (command.getName().equals(commandName)) {
            Log.info(this.commandRunner.getName() + command.getName() + " - " + command.getDescription());
            Log.info("");
            if (command.getUsageHelp() != null) {
                Log.info("usage: " + this.commandRunner.getName() + command.getName() + " " + command.getUsageHelp());
                Log.info("");
            }
            if (command.getHelp() != null) {
                Log.info(command.getHelp());
            }
            Collection<HelpExample> examples = command.getExamples();
            if (examples != null) {
                Log.info((examples.size() != 1) ? "examples:" : "example:");
                Log.info("");
                for (HelpExample example : examples) {
                    Log.info("    " + example.getDescription() + ":");
                    Log.info("        $ " + example.getExample());
                    Log.info("");
                }
                Log.info("");
            }
            return ExitStatus.OK;
        }
    }
    throw new NoSuchCommandException(commandName);
}
Also used : Command(org.springframework.boot.cli.command.Command) AbstractCommand(org.springframework.boot.cli.command.AbstractCommand) HelpExample(org.springframework.boot.cli.command.HelpExample) NoSuchCommandException(org.springframework.boot.cli.command.NoSuchCommandException) NoHelpCommandArgumentsException(org.springframework.boot.cli.command.NoHelpCommandArgumentsException)

Example 3 with Command

use of org.springframework.boot.cli.command.Command in project spring-boot by spring-projects.

the class CommandCompleter method complete.

@Override
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
    int completionIndex = super.complete(buffer, cursor, candidates);
    int spaceIndex = buffer.indexOf(' ');
    String commandName = ((spaceIndex != -1) ? buffer.substring(0, spaceIndex) : "");
    if (StringUtils.hasText(commandName)) {
        for (Command command : this.commands) {
            if (command.getName().equals(commandName)) {
                if (cursor == buffer.length() && buffer.endsWith(" ")) {
                    printUsage(command);
                    break;
                }
                Completer completer = this.commandCompleters.get(command.getName());
                if (completer != null) {
                    completionIndex = completer.complete(buffer, cursor, candidates);
                    break;
                }
            }
        }
    }
    return completionIndex;
}
Also used : Command(org.springframework.boot.cli.command.Command) Completer(jline.console.completer.Completer) StringsCompleter(jline.console.completer.StringsCompleter) AggregateCompleter(jline.console.completer.AggregateCompleter) FileNameCompleter(jline.console.completer.FileNameCompleter) ArgumentCompleter(jline.console.completer.ArgumentCompleter)

Aggregations

Command (org.springframework.boot.cli.command.Command)3 ArrayList (java.util.ArrayList)1 AggregateCompleter (jline.console.completer.AggregateCompleter)1 ArgumentCompleter (jline.console.completer.ArgumentCompleter)1 Completer (jline.console.completer.Completer)1 FileNameCompleter (jline.console.completer.FileNameCompleter)1 StringsCompleter (jline.console.completer.StringsCompleter)1 AbstractCommand (org.springframework.boot.cli.command.AbstractCommand)1 CommandFactory (org.springframework.boot.cli.command.CommandFactory)1 HelpExample (org.springframework.boot.cli.command.HelpExample)1 NoHelpCommandArgumentsException (org.springframework.boot.cli.command.NoHelpCommandArgumentsException)1 NoSuchCommandException (org.springframework.boot.cli.command.NoSuchCommandException)1 HelpCommand (org.springframework.boot.cli.command.core.HelpCommand)1 VersionCommand (org.springframework.boot.cli.command.core.VersionCommand)1