public CommandCompleter(ConsoleReader consoleReader, ArgumentDelimiter argumentDelimiter, Iterable<Command> commands) { this.console = consoleReader; List<String> names = new ArrayList<String>(); for (Command command : commands) { this.commands.add(command); names.add(command.getName()); List<String> options = new ArrayList<String>(); for (OptionHelp optionHelp : command.getOptionsHelp()) { options.addAll(optionHelp.getOptions()); } AggregateCompleter argumentCompleters = new AggregateCompleter( new StringsCompleter(options), new FileNameCompleter()); ArgumentCompleter argumentCompleter = new ArgumentCompleter(argumentDelimiter, argumentCompleters); argumentCompleter.setStrict(false); this.commandCompleters.put(command.getName(), argumentCompleter); } getStrings().addAll(names); }
@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 (!"".equals(commandName.trim())) { 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; }
@Override public Collection<OptionHelp> getOptionsHelp() { List<OptionHelp> help = new ArrayList<OptionHelp>(); for (final Command command : this.commandRunner) { if (isHelpShown(command)) { help.add(new OptionHelp() { @Override public Set<String> getOptions() { return Collections.singleton(command.getName()); } @Override public String getUsageHelp() { return command.getDescription(); } }); } } return help; }
public CommandCompleter(ConsoleReader consoleReader, ArgumentDelimiter argumentDelimiter, Iterable<Command> commands) { this.console = consoleReader; List<String> names = new ArrayList<String>(); for (Command command : commands) { this.commands.add(command); names.add(command.getName()); List<String> options = new ArrayList<String>(); for (OptionHelp optionHelp : command.getOptionsHelp()) { options.addAll(optionHelp.getOptions()); } AggregateCompleter arguementCompleters = new AggregateCompleter( new StringsCompleter(options), new FileNameCompleter()); ArgumentCompleter argumentCompleter = new ArgumentCompleter(argumentDelimiter, arguementCompleters); argumentCompleter.setStrict(false); this.commandCompleters.put(command.getName(), argumentCompleter); } getStrings().addAll(names); }
private Iterable<Command> getCommands() { List<Command> commands = new ArrayList<Command>(); 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; }
private Command convertToForkCommand(Command command) { for (Class<?> nonForked : NON_FORKED_COMMANDS) { if (nonForked.isInstance(command)) { return command; } } return new ForkProcessCommand(command); }
@Override public Command findCommand(String name) { if (name.startsWith("!")) { return new RunProcessCommand(name.substring(1)); } if (this.aliases.containsKey(name)) { name = this.aliases.get(name); } return super.findCommand(name); }
public boolean handleSigInt() { Command command = this.lastCommand; if (command != null && command instanceof RunProcessCommand) { return ((RunProcessCommand) command).handleSigInt(); } return false; }
private void showCommandHints(String starting) { for (Command command : this.commandRunner) { if (isHintMatch(command, starting)) { Log.info(command.getName() + " " + command.getDescription()); } } }
private boolean isHintMatch(Command command, String starting) { if (command instanceof HintCommand) { return false; } return command.getName().startsWith(starting) || (this.commandRunner.isOptionCommand(command) && ("--" + command.getName()).startsWith(starting)); }
private void showCommandOptionHints(String commandName, List<String> specifiedArguments, String starting) { Command command = this.commandRunner.findCommand(commandName); if (command != null) { for (OptionHelp help : command.getOptionsHelp()) { if (!alreadyUsed(help, specifiedArguments)) { for (String option : help.getOptions()) { if (option.startsWith(starting)) { Log.info(option + " " + help.getUsageHelp()); } } } } } }
@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 ? "example:" : "examples:"); 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); }
@Override public Collection<Command> getCommands() { return DEFAULT_COMMANDS; }
ForkProcessCommand(Command command) { super(new JavaExecutable().toString()); this.command = command; }
@Override protected void beforeRun(Command command) { this.lastCommand = command; }
@Override protected void afterRun(Command command) { }
private boolean isHelpShown(Command command) { if (command instanceof HelpCommand || command instanceof HintCommand) { return false; } return true; }
@Override public Collection<Command> getCommands() { return Collections.<Command>singleton(new CustomCommand()); }