All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rvesse.airline.help.suggester.SuggestCommand Maven / Gradle / Ivy

Go to download

Java library provided an annotation-based framework for parsing Git like command line structures

There is a newer version: 3.0.0
Show newest version
/**
 * Copyright (C) 2010-15 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.rvesse.airline.help.suggester;

import com.github.rvesse.airline.Context;
import com.github.rvesse.airline.annotations.Arguments;
import com.github.rvesse.airline.annotations.Command;
import com.github.rvesse.airline.model.CommandGroupMetadata;
import com.github.rvesse.airline.model.CommandMetadata;
import com.github.rvesse.airline.model.GlobalMetadata;
import com.github.rvesse.airline.model.MetadataLoader;
import com.github.rvesse.airline.model.OptionMetadata;
import com.github.rvesse.airline.model.SuggesterMetadata;
import com.github.rvesse.airline.parser.ParseState;
import com.github.rvesse.airline.parser.suggester.SuggestionParser;
import com.github.rvesse.airline.utils.AirlineUtils;

import javax.inject.Inject;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

import static com.github.rvesse.airline.parser.ParserUtil.createInstance;

@Command(name = "suggest")
public class SuggestCommand implements Runnable, Callable {
    private static final Map> BUILTIN_SUGGESTERS = new HashMap<>();

    static {
        BUILTIN_SUGGESTERS.put(Context.GLOBAL, GlobalSuggester.class);
        BUILTIN_SUGGESTERS.put(Context.GROUP, GroupSuggester.class);
        BUILTIN_SUGGESTERS.put(Context.COMMAND, CommandSuggester.class);
    }

    @Inject
    public GlobalMetadata metadata;

    @Arguments
    public List arguments = new ArrayList<>();

    public Iterable generateSuggestions() {
        SuggestionParser parser = new SuggestionParser();
        ParseState state = parser.parse(metadata, arguments);

        Class suggesterClass = BUILTIN_SUGGESTERS.get(state.getLocation());
        if (suggesterClass != null) {
            SuggesterMetadata suggesterMetadata = MetadataLoader.loadSuggester(suggesterClass);

            if (suggesterMetadata != null) {
                Map, Object> bindings = new HashMap, Object>();
                bindings.put(GlobalMetadata.class, metadata);

                if (state.getGroup() != null) {
                    bindings.put(CommandGroupMetadata.class, state.getGroup());
                }

                if (state.getCommand() != null) {
                    bindings.put(CommandMetadata.class, state.getCommand());
                }

                Suggester suggester = createInstance(suggesterMetadata.getSuggesterClass(),
                        Collections. emptyList(), null, null, null,
                        suggesterMetadata.getMetadataInjections(), AirlineUtils.unmodifiableMapCopy(bindings));

                return suggester.suggest();
            }
        }

        return Collections.emptyList();
    }

    @Override
    public void run() {
        System.out.println(StringUtils.join(generateSuggestions(), '\n'));
    }

    @Override
    public Void call() {
        run();
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy