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

com.github.rvesse.airline.help.common.AbstractGlobalUsageGenerator Maven / Gradle / Ivy

Go to download

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

The newest version!
/**
 * Copyright (C) 2010-16 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.common;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.github.rvesse.airline.Channels;
import com.github.rvesse.airline.help.GlobalUsageGenerator;
import com.github.rvesse.airline.help.UsageHelper;
import com.github.rvesse.airline.help.sections.HelpHint;
import com.github.rvesse.airline.help.sections.HelpSection;
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.OptionMetadata;
import com.github.rvesse.airline.utils.comparators.HelpSectionComparator;

/**
 * Abstract global usage generator
 */
public abstract class AbstractGlobalUsageGenerator extends AbstractUsageGenerator
        implements GlobalUsageGenerator {

    private final Comparator commandGroupComparator;

    public AbstractGlobalUsageGenerator() {
        this(UsageHelper.DEFAULT_HINT_COMPARATOR, UsageHelper.DEFAULT_OPTION_COMPARATOR,
                UsageHelper.DEFAULT_COMMAND_COMPARATOR, UsageHelper.DEFAULT_COMMAND_GROUP_COMPARATOR, false);
    }

    public AbstractGlobalUsageGenerator(boolean includeHidden) {
        this(UsageHelper.DEFAULT_HINT_COMPARATOR, UsageHelper.DEFAULT_OPTION_COMPARATOR,
                UsageHelper.DEFAULT_COMMAND_COMPARATOR, UsageHelper.DEFAULT_COMMAND_GROUP_COMPARATOR, includeHidden);
    }

    public AbstractGlobalUsageGenerator(Comparator hintComparator,
            Comparator optionComparator, Comparator commandComparator,
            Comparator commandGroupComparator, boolean includeHidden) {
        super(hintComparator, optionComparator, commandComparator, includeHidden);
        this.commandGroupComparator = commandGroupComparator;
    }

    @Override
    public void usage(GlobalMetadata global) throws IOException {
        usage(global, Channels.output());
    }

    /**
     * Sorts the command groups assumign a non-null comparator was provided at
     * instantiation time
     *
     * @param groups
     *            Command groups
     * @return Sorted command groups
     */
    protected List sortCommandGroups(List groups) {
        if (this.commandGroupComparator != null) {
            groups = new ArrayList<>(groups);
            Collections.sort(groups, this.commandGroupComparator);
        }
        return groups;
    }
    
    /**
     * Finds the help sections
     *
     * @param command
     *            Command meta-data
     * @param preSections
     *            Sections that should be placed before base content
     * @param postSections
     *            Sections that should be placed after base content
     */
    protected void findHelpSections(GlobalMetadata global, List preSections,
            List postSections) {
        for (HelpSection section : global.getBaseHelpSections()) {
            if (section.suggestedOrder() < 0) {
                preSections.add(section);
            } else {
                postSections.add(section);
            }
        }
        HelpSectionComparator comparator = new HelpSectionComparator();
        Collections.sort(preSections, comparator);
        Collections.sort(postSections, comparator);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy