    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      http://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.Iterator;
    021import java.util.List;
    022
    023/**
    024 * Thrown when a required option has not been provided.
    025 */
    026public class MissingOptionException extends ParseException {
    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");
    038        buf.append(": ");
    039
    040        final Iterator<?> it = missingOptions.iterator();
    041        while (it.hasNext()) {
    042            buf.append(it.next());
    043            if (it.hasNext()) {
    044                buf.append(", ");
    045            }
    046        }
    047
    048        return buf.toString();
    049    }
    050
    051    /** The list of missing options and groups */
    052    private List missingOptions;
    053
    054    /**
    055     * Constructs a new {@code MissingSelectedException} with the specified list of missing options.
    056     *
    057     * @param missingOptions the list of missing options and groups
    058     * @since 1.2
    059     */
    060    public MissingOptionException(final List missingOptions) {
    061        this(createMessage(missingOptions));
    062        this.missingOptions = missingOptions;
    063    }
    064
    065    /**
    066     * Constructs a new {@code MissingSelectedException} with the specified detail message.
    067     *
    068     * @param message the detail message
    069     */
    070    public MissingOptionException(final String message) {
    071        super(message);
    072    }
    073
    074    /**
    075     * Gets the list of options or option groups missing in the command line parsed.
    076     *
    077     * @return the missing options, consisting of String instances for simple options, and OptionGroup instances for
    078     *         required option groups.
    079     * @since 1.2
    080     */
    081    public List getMissingOptions() {
    082        return missingOptions;
    083    }
    084}