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 package org.apache.commons.cli2.builder; 18 19 import java.util.HashSet; 20 import java.util.Set; 21 22 import org.apache.commons.cli2.Argument; 23 import org.apache.commons.cli2.Group; 24 import org.apache.commons.cli2.option.Command; 25 import org.apache.commons.cli2.resource.ResourceConstants; 26 import org.apache.commons.cli2.resource.ResourceHelper; 27 28 /** 29 * Builds Command instances 30 */ 31 public class CommandBuilder { 32 /** the preferred name of the command */ 33 private String preferredName; 34 35 /** the description of the command */ 36 private String description; 37 38 /** the aliases of the command */ 39 private Set aliases; 40 41 /** whether the command is required or not */ 42 private boolean required; 43 44 /** the argument of the command */ 45 private Argument argument; 46 47 /** the children of the command */ 48 private Group children; 49 50 /** the id of the command */ 51 private int id; 52 53 /** 54 * Creates a new <code>CommandBuilder</code> instance. 55 */ 56 public CommandBuilder() { 57 reset(); 58 } 59 60 /** 61 * Creates a new <code>Command</code> instance using the properties of the 62 * <code>CommandBuilder</code>. 63 * 64 * @return the new Command instance 65 */ 66 public Command create() { 67 // check we have a valid name 68 if (preferredName == null) { 69 throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.OPTION_NO_NAME)); 70 } 71 72 // build the command 73 final Command option = 74 new Command(preferredName, description, aliases, required, argument, children, id); 75 76 // reset the builder 77 reset(); 78 79 return option; 80 } 81 82 /** 83 * Resets the CommandBuilder to the defaults for a new Command. 84 * 85 * This method is called automatically at the end of the 86 * {@link #create() create} method. 87 * @return this <code>CommandBuilder</code> 88 */ 89 public CommandBuilder reset() { 90 preferredName = null; 91 description = null; 92 aliases = new HashSet(); 93 required = false; 94 argument = null; 95 children = null; 96 id = 0; 97 98 return this; 99 } 100 101 /** 102 * Specifies the name for the next <code>Command</code> 103 * that is created. The first name is used as the preferred 104 * display name for the <code>Command</code> and then 105 * later names are used as aliases. 106 * 107 * @param name the name for the next <code>Command</code> 108 * that is created. 109 * @return this <code>CommandBuilder</code>. 110 */ 111 public CommandBuilder withName(final String name) { 112 if (preferredName == null) { 113 preferredName = name; 114 } else { 115 aliases.add(name); 116 } 117 118 return this; 119 } 120 121 /** 122 * Specifies the description for the next <code>Command</code> 123 * that is created. This description is used to produce 124 * help documentation for the <code>Command</code>. 125 * 126 * @param newDescription the description for the next 127 * <code>Command</code> that is created. 128 * @return this <code>CommandBuilder</code>. 129 */ 130 public CommandBuilder withDescription(final String newDescription) { 131 this.description = newDescription; 132 133 return this; 134 } 135 136 /** 137 * Specifies whether the next <code>Command</code> created is 138 * required or not. 139 * @param newRequired whether the next <code>Command</code> created is 140 * required or not. 141 * @return this <code>CommandBuilder</code>. 142 */ 143 public CommandBuilder withRequired(final boolean newRequired) { 144 this.required = newRequired; 145 146 return this; 147 } 148 149 /** 150 * Specifies the children for the next <code>Command</code> 151 * that is created. 152 * 153 * @param newChildren the child options for the next <code>Command</code> 154 * that is created. 155 * @return this <code>CommandBuilder</code>. 156 */ 157 public CommandBuilder withChildren(final Group newChildren) { 158 this.children = newChildren; 159 160 return this; 161 } 162 163 /** 164 * Specifies the argument for the next <code>Command</code> 165 * that is created. 166 * 167 * @param newArgument the argument for the next <code>Command</code> 168 * that is created. 169 * @return this <code>CommandBuilder</code>. 170 */ 171 public CommandBuilder withArgument(final Argument newArgument) { 172 this.argument = newArgument; 173 174 return this; 175 } 176 177 /** 178 * Specifies the id for the next <code>Command</code> that is created. 179 * 180 * @param newId the id for the next <code>Command</code> that is created. 181 * @return this <code>CommandBuilder</code>. 182 */ 183 public final CommandBuilder withId(final int newId) { 184 this.id = newId; 185 186 return this; 187 } 188 }