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