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