001/*
002  Licensed to the Apache Software Foundation (ASF) under one or more
003  contributor license agreements.  See the NOTICE file distributed with
004  this work for additional information regarding copyright ownership.
005  The ASF licenses this file to You under the Apache License, Version 2.0
006  (the "License"); you may not use this file except in compliance with
007  the License.  You may obtain a copy of the License at
008
009      https://www.apache.org/licenses/LICENSE-2.0
010
011  Unless required by applicable law or agreed to in writing, software
012  distributed under the License is distributed on an "AS IS" BASIS,
013  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  See the License for the specific language governing permissions and
015  limitations under the License.
016 */
017
018package org.apache.commons.cli;
019
020import java.util.List;
021
022/**
023 * Thrown when a required option has not been provided.
024 */
025public class MissingOptionException extends ParseException {
026
027    /** This exception {@code serialVersionUID}. */
028    private static final long serialVersionUID = 8161889051578563249L;
029
030    /**
031     * Build the exception message from the specified list of options.
032     *
033     * @param missingOptions the list of missing options and groups
034     */
035    private static String createMessage(final List<?> missingOptions) {
036        final StringBuilder buf = new StringBuilder("Missing required option");
037        buf.append(missingOptions.size() == 1 ? "" : "s").append(": ");
038        final String string = missingOptions.toString();
039        return buf.append(string.substring(1, string.length() - 1)).toString();
040    }
041
042    /** The list of missing options and groups */
043    private List missingOptions;
044
045    /**
046     * Constructs a new {@code MissingSelectedException} with the specified list of missing options.
047     *
048     * @param missingOptions the list of missing options and groups
049     * @since 1.2
050     */
051    public MissingOptionException(final List missingOptions) {
052        this(createMessage(missingOptions));
053        this.missingOptions = missingOptions;
054    }
055
056    /**
057     * Constructs a new {@code MissingSelectedException} with the specified detail message.
058     *
059     * @param message the detail message
060     */
061    public MissingOptionException(final String message) {
062        super(message);
063    }
064
065    /**
066     * Gets the list of options or option groups missing in the command line parsed.
067     *
068     * @return the missing options, consisting of String instances for simple options, and OptionGroup instances for
069     *         required option groups.
070     * @since 1.2
071     */
072    public List getMissingOptions() {
073        return missingOptions;
074    }
075}