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

com.github.rvesse.airline.Cli 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-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;

import java.util.Arrays;

import org.apache.commons.collections4.ListUtils;

import com.github.rvesse.airline.builder.CliBuilder;
import com.github.rvesse.airline.model.GlobalMetadata;
import com.github.rvesse.airline.model.MetadataLoader;
import com.github.rvesse.airline.model.ParserMetadata;
import com.github.rvesse.airline.parser.ParseResult;
import com.github.rvesse.airline.parser.command.CliParser;

/**
 * Class for encapsulating and parsing CLIs
 * 
 * @author rvesse
 *
 * @param 
 *            Command type
 */
public class Cli {
    /**
     * Creates a builder for specifying a command line in fluent style
     * 
     * @param name
     *            Program name
     * @param 
     *            Command type to be built
     * @return CLI Builder
     */
    public static  CliBuilder builder(String name) {
        if (name == null)
            throw new NullPointerException("name cannot be null");
        return new CliBuilder(name);
    }

    private final GlobalMetadata metadata;

    /**
     * Creates a new CLI from a class annotated with the
     * {@link com.github.rvesse.airline.annotations.Cli} annotation
     * 
     * @param cliClass
     *            CLI class
     */
    public Cli(Class cliClass) {
        this(MetadataLoader. loadGlobal(cliClass));
    }

    /**
     * Creates a new CLI from a class annotated with the
     * {@link com.github.rvesse.airline.annotations.Cli} annotation
     * 
     * @param cliClass
     *            CLI class
     * @param parserConfig
     *            Parser configuration, this will override any configuration
     *            specified by the
     *            {@link com.github.rvesse.airline.annotations.Cli#parserConfiguration()}
     *            field
     */
    public Cli(Class cliClass, ParserMetadata parserConfig) {
        this(MetadataLoader. loadGlobal(cliClass, parserConfig));
    }

    /**
     * Creates a new CLI
     * 
     * @param metadata
     *            Metadata
     */
    public Cli(GlobalMetadata metadata) {
        if (metadata == null)
            throw new NullPointerException("metadata cannot be null");
        this.metadata = metadata;
    }

    /**
     * Gets the global meta-data
     * 
     * @return Meta-data
     */
    public GlobalMetadata getMetadata() {
        return metadata;
    }

    /**
     * Parses the arguments to produce a command instance, this may be
     * {@code null} if the arguments don't identify a command and there was no
     * appropriate default command configured
     * 
     * @param args
     *            Arguments
     * @return Command instance
     */
    public C parse(String... args) {
        return parse(ListUtils.unmodifiableList(Arrays.asList(args)));
    }

    /**
     * Parses the arguments to produce a command instance, this may be
     * {@code null} if the arguments don't identify a command and there was no
     * appropriate default command configured
     * 
     * @param args
     *            Arguments
     * @return Command instance
     */
    private C parse(Iterable args) {
        CliParser parser = new CliParser();
        return parser.parse(metadata, args);
    }

    /**
     * Parses the arguments to produce a result. The result can be inspected to
     * see errors (assuming a suitable error handler was used e.g.
     * {@code CollectAll}) and to get a command instance. This may be
     * {@code null} if the arguments don't identify a command and there was no
     * appropriate default command configured
     * 
     * @param args
     *            Arguments
     * @return Parse result
     */
    public ParseResult parseWithResult(String... args) {
        return parseWithResult(Arrays.asList(args));
    }

    /**
     * Parses the arguments to produce a result. The result can be inspected to
     * see errors (assuming a suitable error handler was used e.g.
     * {@code CollectAll}) and to get a command instance. This may be
     * {@code null} if the arguments don't identify a command and there was no
     * appropriate default command configured
     * 
     * @param args
     *            Arguments
     * @return Parse result
     */
    public ParseResult parseWithResult(Iterable args) {
        CliParser parser = new CliParser();
        return parser.parseWithResult(metadata, args);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy