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 */
017package Apache.CLI;
018
019import java.util.Iterator;
020import java.util.List;
021
022/** Thrown when a required option has not been provided. */
023@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="LICENSE")
024public class MissingOptionException extends ParseException
025{
026    // This exception {@code serialVersionUID}.
027    private static final long serialVersionUID = 1;
028
029    /**
030     * Build the exception message from the specified list of options.
031     * @param missingOptions the list of missing options and groups
032     */
033    private static String createMessage(final List<?> missingOptions)
034    {
035        final StringBuilder buf = new StringBuilder("Missing required option");
036
037        buf.append(missingOptions.size() == 1 ? "" : "s");
038        buf.append(": ");
039
040        final Iterator<?> it = missingOptions.iterator();
041
042        while (it.hasNext())
043        {
044            buf.append(it.next());
045
046            if (it.hasNext()) buf.append(", ");
047        }
048
049        return buf.toString();
050    }
051
052    // The list of missing options and groups
053    @SuppressWarnings("rawtypes")
054    private List missingOptions;
055
056    /**
057     * Constructs a new {@code MissingSelectedException} with the specified list of missing
058     * options.
059     *
060     * @param missingOptions the list of missing options and groups
061     */
062    public MissingOptionException(@SuppressWarnings("rawtypes") final List missingOptions)
063    {
064        this(createMessage(missingOptions));
065        this.missingOptions = missingOptions;
066    }
067
068    /**
069     * Constructs a new {@code MissingSelectedException} with the specified detail message.
070     * @param message the detail message
071     */
072    public MissingOptionException(final String message)
073    { super(message); }
074
075    /**
076     * Gets the list of options or option groups missing in the command line parsed.
077     *
078     * @return the missing options, consisting of String instances for simple options, and
079     * OptionGroup instances for required option groups.
080     */
081    @SuppressWarnings("rawtypes")
082    public List getMissingOptions()
083    { return missingOptions; }
084}