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    
018    package org.apache.commons.cli;
019    
020    import java.util.List;
021    import java.util.Iterator;
022    
023    /**
024     * Thrown when a required option has not been provided.
025     *
026     * @author John Keyes ( john at integralsource.com )
027     * @version $Revision: 680644 $, $Date: 2008-07-29 01:13:48 -0700 (Tue, 29 Jul 2008) $
028     */
029    public class MissingOptionException extends ParseException
030    {
031        /** The list of missing options */
032        private List missingOptions;
033    
034        /**
035         * Construct a new <code>MissingSelectedException</code>
036         * with the specified detail message.
037         *
038         * @param message the detail message
039         */
040        public MissingOptionException(String message)
041        {
042            super(message);
043        }
044    
045        /**
046         * Constructs a new <code>MissingSelectedException</code> with the
047         * specified list of missing options.
048         *
049         * @param missingOptions the list of missing options
050         * @since 1.2
051         */
052        public MissingOptionException(List missingOptions)
053        {
054            this(createMessage(missingOptions));
055            this.missingOptions = missingOptions;
056        }
057    
058        /**
059         * Return the list of options (as strings) missing in the command line parsed.
060         *
061         * @return the missing options
062         * @since 1.2
063         */
064        public List getMissingOptions()
065        {
066            return missingOptions;
067        }
068    
069        /**
070         * Build the exception message from the specified list of options.
071         *
072         * @param missingOptions
073         * @since 1.2
074         */
075        private static String createMessage(List missingOptions)
076        {
077            StringBuffer buff = new StringBuffer("Missing required option");
078            buff.append(missingOptions.size() == 1 ? "" : "s");
079            buff.append(": ");
080    
081            Iterator it = missingOptions.iterator();
082            while (it.hasNext())
083            {
084                buff.append(it.next());
085                if (it.hasNext())
086                {
087                    buff.append(", ");
088                }
089            }
090    
091            return buff.toString();
092        }
093    }