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.List;
021import java.util.Iterator;
022
023/**
024 * Thrown when a required option has not been provided.
025 *
026 * @version $Id: MissingOptionException.java 1443102 2013-02-06 18:12:16Z tn $
027 */
028public class MissingOptionException extends ParseException
029{
030    /** This exception {@code serialVersionUID}. */
031    private static final long serialVersionUID = 8161889051578563249L;
032
033    /** The list of missing options and groups */
034    private List missingOptions;
035
036    /**
037     * Construct a new <code>MissingSelectedException</code>
038     * with the specified detail message.
039     *
040     * @param message the detail message
041     */
042    public MissingOptionException(String message)
043    {
044        super(message);
045    }
046
047    /**
048     * Constructs a new <code>MissingSelectedException</code> with the
049     * specified list of missing options.
050     *
051     * @param missingOptions the list of missing options and groups
052     * @since 1.2
053     */
054    public MissingOptionException(List missingOptions)
055    {
056        this(createMessage(missingOptions));
057        this.missingOptions = missingOptions;
058    }
059
060    /**
061     * Returns the list of options or option groups missing in the command line parsed.
062     *
063     * @return the missing options, consisting of String instances for simple
064     *         options, and OptionGroup instances for required option groups.
065     * @since 1.2
066     */
067    public List getMissingOptions()
068    {
069        return missingOptions;
070    }
071
072    /**
073     * Build the exception message from the specified list of options.
074     *
075     * @param missingOptions the list of missing options and groups
076     * @since 1.2
077     */
078    private static String createMessage(List<?> missingOptions)
079    {
080        StringBuilder buf = new StringBuilder("Missing required option");
081        buf.append(missingOptions.size() == 1 ? "" : "s");
082        buf.append(": ");
083
084        Iterator<?> it = missingOptions.iterator();
085        while (it.hasNext())
086        {
087            buf.append(it.next());
088            if (it.hasNext())
089            {
090                buf.append(", ");
091            }
092        }
093
094        return buf.toString();
095    }
096}