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

org.apache.maven.wrapper.cli.ParsedCommandLine Maven / Gradle / Ivy

Go to download

Maven Wrapper Jar download, installs and launches installed target Maven distribution as part of Maven Wrapper scripts run.

There is a newer version: 3.3.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.maven.wrapper.cli;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Parsed command line.
 */
public class ParsedCommandLine {
    private final Map optionsByString = new HashMap<>();

    private final Set presentOptions = new HashSet<>();

    private final List extraArguments = new ArrayList<>();

    ParsedCommandLine(Iterable options) {
        for (CommandLineOption option : options) {
            ParsedCommandLineOption parsedOption = new ParsedCommandLineOption();
            for (String optionStr : option.getOptions()) {
                optionsByString.put(optionStr, parsedOption);
            }
        }
    }

    @Override
    public String toString() {
        return String.format(
                "options: %s, extraArguments: %s", quoteAndJoin(presentOptions), quoteAndJoin(extraArguments));
    }

    private String quoteAndJoin(Iterable strings) {
        StringBuilder output = new StringBuilder();
        boolean isFirst = true;
        for (String string : strings) {
            if (!isFirst) {
                output.append(", ");
            }
            output.append("'");
            output.append(string);
            output.append("'");
            isFirst = false;
        }
        return output.toString();
    }

    /**
     * Returns true if the given option is present in this command-line.
     *
     * @param option The option, without the '-' or '--' prefix.
     * @return true if the option is present.
     */
    public boolean hasOption(String option) {
        option(option);
        return presentOptions.contains(option);
    }

    /**
     * See also {@link #hasOption}.
     *
     * @param logLevelOptions the options to check
     * @return true if any of the passed options is present
     */
    public boolean hasAnyOption(Collection logLevelOptions) {
        for (String option : logLevelOptions) {
            if (hasOption(option)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Returns the value of the given option.
     *
     * @param option The option, without the '-' or '--' prefix.
     * @return The option. never returns null.
     */
    public ParsedCommandLineOption option(String option) {
        ParsedCommandLineOption parsedOption = optionsByString.get(option);
        if (parsedOption == null) {
            throw new IllegalArgumentException(String.format("Option '%s' not defined.", option));
        }
        return parsedOption;
    }

    public List getExtraArguments() {
        return extraArguments;
    }

    void addExtraValue(String value) {
        extraArguments.add(value);
    }

    ParsedCommandLineOption addOption(String optionStr, CommandLineOption option) {
        ParsedCommandLineOption parsedOption = optionsByString.get(optionStr);
        presentOptions.addAll(option.getOptions());
        return parsedOption;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy