001 package org.crsh.cmdline.matcher.impl;
002
003 import org.crsh.cmdline.CommandDescriptor;
004 import org.crsh.cmdline.matcher.CmdCompletionException;
005 import org.crsh.cmdline.matcher.tokenizer.Token;
006
007 import java.util.HashMap;
008 import java.util.Map;
009 import java.util.Set;
010
011 /**
012 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
013 */
014 class OptionCompletion<T> extends Completion {
015
016 /** . */
017 private final CommandDescriptor<T, ?> descriptor;
018
019 /** . */
020 private final Token.Literal.Option prefix;
021
022 OptionCompletion(CommandDescriptor<T, ?> descriptor, Token.Literal.Option prefix) {
023 this.descriptor = descriptor;
024 this.prefix = prefix;
025 }
026
027 @Override
028 protected Map<String, String> complete() throws CmdCompletionException {
029 Map<String, String> completions = new HashMap<String, String>();
030 Set<String> optionNames = prefix instanceof Token.Literal.Option.Short ? descriptor.getShortOptionNames() : descriptor.getLongOptionNames();
031 for (String optionName : optionNames) {
032 if (optionName.startsWith(prefix.getValue())) {
033 completions.put(optionName.substring(prefix.getValue().length()), " ");
034 }
035 }
036 return completions;
037 }
038 }