View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.cli;
19  
20  import java.util.List;
21  import java.util.Iterator;
22  
23  /**
24   * Thrown when a required option has not been provided.
25   *
26   * @version $Id: MissingOptionException.java 1443102 2013-02-06 18:12:16Z tn $
27   */
28  public class MissingOptionException extends ParseException
29  {
30      /** This exception {@code serialVersionUID}. */
31      private static final long serialVersionUID = 8161889051578563249L;
32  
33      /** The list of missing options and groups */
34      private List missingOptions;
35  
36      /**
37       * Construct a new <code>MissingSelectedException</code>
38       * with the specified detail message.
39       *
40       * @param message the detail message
41       */
42      public MissingOptionException(String message)
43      {
44          super(message);
45      }
46  
47      /**
48       * Constructs a new <code>MissingSelectedException</code> with the
49       * specified list of missing options.
50       *
51       * @param missingOptions the list of missing options and groups
52       * @since 1.2
53       */
54      public MissingOptionException(List missingOptions)
55      {
56          this(createMessage(missingOptions));
57          this.missingOptions = missingOptions;
58      }
59  
60      /**
61       * Returns the list of options or option groups missing in the command line parsed.
62       *
63       * @return the missing options, consisting of String instances for simple
64       *         options, and OptionGroup instances for required option groups.
65       * @since 1.2
66       */
67      public List getMissingOptions()
68      {
69          return missingOptions;
70      }
71  
72      /**
73       * Build the exception message from the specified list of options.
74       *
75       * @param missingOptions the list of missing options and groups
76       * @since 1.2
77       */
78      private static String createMessage(List<?> missingOptions)
79      {
80          StringBuilder buf = new StringBuilder("Missing required option");
81          buf.append(missingOptions.size() == 1 ? "" : "s");
82          buf.append(": ");
83  
84          Iterator<?> it = missingOptions.iterator();
85          while (it.hasNext())
86          {
87              buf.append(it.next());
88              if (it.hasNext())
89              {
90                  buf.append(", ");
91              }
92          }
93  
94          return buf.toString();
95      }
96  }