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  public class MissingOptionException extends ParseException
27  {
28      /** This exception {@code serialVersionUID}. */
29      private static final long serialVersionUID = 8161889051578563249L;
30  
31      /** The list of missing options and groups */
32      private List missingOptions;
33  
34      /**
35       * Construct a new <code>MissingSelectedException</code>
36       * with the specified detail message.
37       *
38       * @param message the detail message
39       */
40      public MissingOptionException(final String message)
41      {
42          super(message);
43      }
44  
45      /**
46       * Constructs a new <code>MissingSelectedException</code> with the
47       * specified list of missing options.
48       *
49       * @param missingOptions the list of missing options and groups
50       * @since 1.2
51       */
52      public MissingOptionException(final List missingOptions)
53      {
54          this(createMessage(missingOptions));
55          this.missingOptions = missingOptions;
56      }
57  
58      /**
59       * Returns the list of options or option groups missing in the command line parsed.
60       *
61       * @return the missing options, consisting of String instances for simple
62       *         options, and OptionGroup instances for required option groups.
63       * @since 1.2
64       */
65      public List getMissingOptions()
66      {
67          return missingOptions;
68      }
69  
70      /**
71       * Build the exception message from the specified list of options.
72       *
73       * @param missingOptions the list of missing options and groups
74       * @since 1.2
75       */
76      private static String createMessage(final List<?> missingOptions)
77      {
78          final StringBuilder buf = new StringBuilder("Missing required option");
79          buf.append(missingOptions.size() == 1 ? "" : "s");
80          buf.append(": ");
81  
82          final Iterator<?> it = missingOptions.iterator();
83          while (it.hasNext())
84          {
85              buf.append(it.next());
86              if (it.hasNext())
87              {
88                  buf.append(", ");
89              }
90          }
91  
92          return buf.toString();
93      }
94  }