- java.lang.Object
-
- Apache.CLI.Option.Builder
-
- Enclosing class:
- Option
public static final class Option.Builder extends java.lang.Object
This Class-File has been copied, directly from the Apache Commons Software Foundation. Other than the minor, cosmetic changes (mentioned in the second-list below), this file is perfectly identical to the original that may be found on the Apache Website:apache.org
.- The suggested license for using this file may be read here:
Apache License
- The Source-Code Header for this file may be viewed here:
Source-File Header
- Partial contents of the
'site/resources/images'
directory:Resource Images
Notes about changes that have been made as a result of the import process:- Within all files, the original Package-Name declaration has been changed:
Original: package org.apache.commons.cli;
Updated: package Apache.CLI;
- All Classes and Class-Members (Methods, Fields, etc...) that were previously Annotated
with the
@Deprecated
Annotation have been summarily removed.
- Code Formattings and Styling has been heavily modified to conform to the Standard Java-HTML
Indentation & Styling Choices. The code itself remains identical, with only a few
squiggly-braces
'{' and '}'
being removed, for cosmetic reasons (and for lowering "Developer Stress" Levels)..
A nested builder class to createOption
instances using descriptive methods.
Example usage:
Example:
Option option = Option.builder("a").required(true).longOpt("arg-name").build();
Hi-Lited Source-Code:- View Here: Apache/CLI/Option.java
- Open New Browser-Tab: Apache/CLI/Option.java
File Size: 7,584 Bytes Line Count: 231 '\n' Characters Found
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Option.Builder
argName(String argName)
Sets the display name for the argument value.Option
build()
Constructs an Option with the values declared by thisOption.Builder
.Option.Builder
desc(String description)
Sets the description for this option.Option.Builder
hasArg()
Indicates that the Option will require an argument.Option.Builder
hasArg(boolean hasArg)
Indicates if the Option has an argument or not.Option.Builder
hasArgs()
Indicates that the Option can have unlimited argument values.Option.Builder
longOpt(String longOpt)
Sets the long name of the Option.Option.Builder
numberOfArgs(int argCount)
Sets the number of argument values the Option can take.Option.Builder
option(String option)
Sets the name of the Option.Option.Builder
optionalArg(boolean optionalArg)
Sets whether the Option can have an optional argument.Option.Builder
required()
Marks this Option as required.Option.Builder
required(boolean required)
Sets whether the Option is mandatory.Option.Builder
type(Class<?> type)
Sets the type of the Option.Option.Builder
valueSeparator()
The Option will use'='
as a means to separate argument value.Option.Builder
valueSeparator(char valueSeparator)
The Option will usesep
as a means to separate argument values.
-
-
-
Method Detail
-
argName
public Option.Builder argName(java.lang.String argName)
Sets the display name for the argument value.- Parameters:
argName
- the display name for the argument value.- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
this.argName = argName; return this;
-
build
public Option build()
Constructs an Option with the values declared by thisOption.Builder
.- Returns:
- the new
Option
- Throws:
java.lang.IllegalArgumentException
- if neitheropt
orlongOpt
has been set- Code:
- Exact Method Body:
if (option == null && longOption == null) throw new IllegalArgumentException("Either opt or longOpt must be specified"); return new Option(this);
-
desc
public Option.Builder desc(java.lang.String description)
Sets the description for this option.- Parameters:
description
- the description of the option.- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
this.description = description; return this;
-
hasArg
public Option.Builder hasArg()
Indicates that the Option will require an argument.- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
return hasArg(true);
-
hasArg
public Option.Builder hasArg(boolean hasArg)
Indicates if the Option has an argument or not.- Parameters:
hasArg
- specifies whether the Option takes an argument or not- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
// set to UNINITIALIZED when no arg is specified to be compatible with OptionBuilder argCount = hasArg ? 1 : Option.UNINITIALIZED; return this;
-
hasArgs
public Option.Builder hasArgs()
Indicates that the Option can have unlimited argument values.- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
argCount = Option.UNLIMITED_VALUES; return this;
-
longOpt
public Option.Builder longOpt(java.lang.String longOpt)
Sets the long name of the Option.- Parameters:
longOpt
- the long name of the Option- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
this.longOption = longOpt; return this;
-
numberOfArgs
public Option.Builder numberOfArgs(int argCount)
Sets the number of argument values the Option can take.- Parameters:
argCount
- the number of argument values- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
this.argCount = argCount; return this;
-
option
public Option.Builder option(java.lang.String option) throws java.lang.IllegalArgumentException
Sets the name of the Option.- Parameters:
option
- the name of the Option- Returns:
- this builder, to allow method chaining
- Throws:
java.lang.IllegalArgumentException
- if there are any non valid Option characters inopt
- Code:
- Exact Method Body:
this.option = OptionValidator.validate(option); return this;
-
optionalArg
public Option.Builder optionalArg(boolean optionalArg)
Sets whether the Option can have an optional argument.- Parameters:
optionalArg
- specifies whether the Option can have an optional argument.- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
this.argCount = optionalArg ? 1 : UNINITIALIZED; this.optionalArg = optionalArg; return this;
-
required
public Option.Builder required()
Marks this Option as required.- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
return required(true);
-
required
public Option.Builder required(boolean required)
Sets whether the Option is mandatory.- Parameters:
required
- specifies whether the Option is mandatory- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
this.required = required; return this;
-
type
public Option.Builder type(java.lang.Class<?> type)
Sets the type of the Option.- Parameters:
type
- the type of the Option- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
this.type = type; return this;
-
valueSeparator
public Option.Builder valueSeparator()
The Option will use'='
as a means to separate argument value.- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
return valueSeparator('=');
-
valueSeparator
public Option.Builder valueSeparator(char valueSeparator)
The Option will usesep
as a means to separate argument values. See the example below:
Example:
Option opt = Option.builder("D").hasArgs().valueSeparator('=').build(); Options options = new Options(); options.addOption(opt); String[] args = {"-Dkey=value"}; CommandLineParser parser = new DefaultParser(); CommandLine line = parser.parse(options, args); // will be "key" String propertyName = line.getOptionValues("D")[0]; // will be "value" String propertyValue = line.getOptionValues("D")[1];
- Parameters:
valueSeparator
- The value separator.- Returns:
- this builder, to allow method chaining
- Code:
- Exact Method Body:
this.valueSeparator = valueSeparator; return this;
-
-