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.io.Serializable; 021import java.util.Collection; 022import java.util.HashMap; 023import java.util.Iterator; 024import java.util.Map; 025 026/** 027 * A group of mutually exclusive options. 028 * 029 * @version $Id: OptionGroup.java 1669814 2015-03-28 18:09:26Z britter $ 030 */ 031public class OptionGroup implements Serializable 032{ 033 /** The serial version UID. */ 034 private static final long serialVersionUID = 1L; 035 036 /** hold the options */ 037 private final Map<String, Option> optionMap = new HashMap<String, Option>(); 038 039 /** the name of the selected option */ 040 private String selected; 041 042 /** specified whether this group is required */ 043 private boolean required; 044 045 /** 046 * Add the specified <code>Option</code> to this group. 047 * 048 * @param option the option to add to this group 049 * @return this option group with the option added 050 */ 051 public OptionGroup addOption(Option option) 052 { 053 // key - option name 054 // value - the option 055 optionMap.put(option.getKey(), option); 056 057 return this; 058 } 059 060 /** 061 * @return the names of the options in this group as a 062 * <code>Collection</code> 063 */ 064 public Collection<String> getNames() 065 { 066 // the key set is the collection of names 067 return optionMap.keySet(); 068 } 069 070 /** 071 * @return the options in this group as a <code>Collection</code> 072 */ 073 public Collection<Option> getOptions() 074 { 075 // the values are the collection of options 076 return optionMap.values(); 077 } 078 079 /** 080 * Set the selected option of this group to <code>name</code>. 081 * 082 * @param option the option that is selected 083 * @throws AlreadySelectedException if an option from this group has 084 * already been selected. 085 */ 086 public void setSelected(Option option) throws AlreadySelectedException 087 { 088 if (option == null) 089 { 090 // reset the option previously selected 091 selected = null; 092 return; 093 } 094 095 // if no option has already been selected or the 096 // same option is being reselected then set the 097 // selected member variable 098 if (selected == null || selected.equals(option.getKey())) 099 { 100 selected = option.getKey(); 101 } 102 else 103 { 104 throw new AlreadySelectedException(this, option); 105 } 106 } 107 108 /** 109 * @return the selected option name 110 */ 111 public String getSelected() 112 { 113 return selected; 114 } 115 116 /** 117 * @param required specifies if this group is required 118 */ 119 public void setRequired(boolean required) 120 { 121 this.required = required; 122 } 123 124 /** 125 * Returns whether this option group is required. 126 * 127 * @return whether this option group is required 128 */ 129 public boolean isRequired() 130 { 131 return required; 132 } 133 134 /** 135 * Returns the stringified version of this OptionGroup. 136 * 137 * @return the stringified representation of this group 138 */ 139 @Override 140 public String toString() 141 { 142 StringBuilder buff = new StringBuilder(); 143 144 Iterator<Option> iter = getOptions().iterator(); 145 146 buff.append("["); 147 148 while (iter.hasNext()) 149 { 150 Option option = iter.next(); 151 152 if (option.getOpt() != null) 153 { 154 buff.append("-"); 155 buff.append(option.getOpt()); 156 } 157 else 158 { 159 buff.append("--"); 160 buff.append(option.getLongOpt()); 161 } 162 163 if (option.getDescription() != null) 164 { 165 buff.append(" "); 166 buff.append(option.getDescription()); 167 } 168 169 if (iter.hasNext()) 170 { 171 buff.append(", "); 172 } 173 } 174 175 buff.append("]"); 176 177 return buff.toString(); 178 } 179}