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