com.alibaba.antx.util.cli.OptionBuilder Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2012 Alibaba Group Holding Limited.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.antx.util.cli;
/**
*
* OptionBuilder allows the user to create Options using descriptive methods.
*
*
* Details on the Builder pattern can be found at http://c2.com/cgi-bin/wiki?BuilderPattern.
*
*
* @author John Keyes ( john at integralsource.com )
* @since 1.0
*/
public class OptionBuilder {
/** long option */
private String longopt;
/** option description */
private String description;
/** argument name */
private String argName;
/** is required? */
private boolean required;
/** the number of arguments */
private int numberOfArgs = Option.UNINITIALIZED;
/** option type */
private Object type;
/** option can have an optional argument value */
private boolean optionalArg;
/** value separator for argument value */
private char valuesep;
public OptionBuilder() {
}
/**
*
* Resets the member variables to their default values.
*
*/
private void reset() {
description = null;
argName = null;
longopt = null;
type = null;
required = false;
numberOfArgs = Option.UNINITIALIZED;
// PMM 9/6/02 - these were missing
optionalArg = false;
valuesep = (char) 0;
}
/**
*
* The next Option created will have the following long option value.
*
*
* @param longopt the long option value
* @return the OptionBuilder instance
*/
public OptionBuilder withLongOpt(String longopt) {
this.longopt = longopt;
return this;
}
/**
*
* The next Option created will require an argument value.
*
*
* @return the OptionBuilder instance
*/
public OptionBuilder hasArg() {
this.numberOfArgs = 1;
return this;
}
/**
*
* The next Option created will require an argument value if
* hasArg
is true.
*
*
* @param hasArg if true then the Option has an argument value
* @return the OptionBuilder instance
*/
public OptionBuilder hasArg(boolean hasArg) {
this.numberOfArgs = hasArg == true ? 1 : Option.UNINITIALIZED;
return this;
}
/**
*
* The next Option created will have the specified argument value name.
*
*
* @param name the name for the argument value
* @return the OptionBuilder instance
*/
public OptionBuilder withArgName(String name) {
this.argName = name;
return this;
}
/**
*
* The next Option created will be required.
*
*
* @return the OptionBuilder instance
*/
public OptionBuilder isRequired() {
this.required = true;
return this;
}
/**
*
* The next Option created uses sep
as a means to separate
* argument values.
*
* Example:
*
*
* Option opt = OptionBuilder.withValueSeparator(':').create('D');
* CommandLine line = parser.parse(args);
* String propertyName = opt.getValue(0);
* String propertyValue = opt.getValue(1);
*
*
* @return the OptionBuilder instance
*/
public OptionBuilder withValueSeparator(char sep) {
this.valuesep = sep;
return this;
}
/**
*
* The next Option created uses '=
' as a means to separate
* argument values.
*
* Example:
*
*
* Option opt = OptionBuilder.withValueSeparator().create('D');
* CommandLine line = parser.parse(args);
* String propertyName = opt.getValue(0);
* String propertyValue = opt.getValue(1);
*
*
* @return the OptionBuilder instance
*/
public OptionBuilder withValueSeparator() {
this.valuesep = '=';
return this;
}
/**
*
* The next Option created will be required if required
is
* true.
*
*
* @param required if true then the Option is required
* @return the OptionBuilder instance
*/
public OptionBuilder isRequired(boolean required) {
this.required = required;
return this;
}
/**
*
* The next Option created can have unlimited argument values.
*
*
* @return the OptionBuilder instance
*/
public OptionBuilder hasArgs() {
this.numberOfArgs = Option.UNLIMITED_VALUES;
return this;
}
/**
*
* The next Option created can have num
argument values.
*
*
* @param num the number of args that the option can have
* @return the OptionBuilder instance
*/
public OptionBuilder hasArgs(int num) {
this.numberOfArgs = num;
return this;
}
/**
*
* The next Option can have an optional argument.
*
*
* @return the OptionBuilder instance
*/
public OptionBuilder hasOptionalArg() {
this.numberOfArgs = 1;
this.optionalArg = true;
return this;
}
/**
*
* The next Option can have an unlimited number of optional arguments.
*
*
* @return the OptionBuilder instance
*/
public OptionBuilder hasOptionalArgs() {
this.numberOfArgs = Option.UNLIMITED_VALUES;
this.optionalArg = true;
return this;
}
/**
*
* The next Option can have the specified number of optional arguments.
*
*
* @param numArgs - the maximum number of optional arguments the next Option
* created can have.
* @return the OptionBuilder instance
*/
public OptionBuilder hasOptionalArgs(int numArgs) {
this.numberOfArgs = numArgs;
this.optionalArg = true;
return this;
}
/**
*
* The next Option created will have a value that will be an instance of
* type
.
*
*
* @param type the type of the Options argument value
* @return the OptionBuilder instance
*/
public OptionBuilder withType(Object type) {
this.type = type;
return this;
}
/**
*
* The next Option created will have the specified description
*
*
* @param description a description of the Option's purpose
* @return the OptionBuilder instance
*/
public OptionBuilder withDescription(String description) {
this.description = description;
return this;
}
/**
*
* Create an Option using the current settings and with the specified Option
* char
.
*
*
* @param opt the character representation of the Option
* @return the Option instance
* @throws IllegalArgumentException if opt
is not a valid
* character. See Option.
*/
public Option create(char opt) throws IllegalArgumentException {
return create(String.valueOf(opt));
}
/**
*
* Create an Option using the current settings
*
*
* @return the Option instance
* @throws IllegalArgumentException if longOpt
has not been set.
*/
public Option create() throws IllegalArgumentException {
if (longopt == null) {
throw new IllegalArgumentException("must specify longopt");
}
return create(" ");
}
/**
*
* Create an Option using the current settings and with the specified Option
* char
.
*
*
* @param opt the java.lang.String
representation of the Option
* @return the Option instance
* @throws IllegalArgumentException if opt
is not a valid
* character. See Option.
*/
public Option create(String opt) throws IllegalArgumentException {
// create the option
Option option = new Option(opt, description);
// set the option properties
option.setLongOpt(longopt);
option.setRequired(required);
option.setOptionalArg(optionalArg);
option.setArgs(numberOfArgs);
option.setType(type);
option.setValueSeparator(valuesep);
option.setArgName(argName);
// reset the OptionBuilder properties
reset();
// return the Option instance
return option;
}
}