public static final class Option.Builder extends Object implements Supplier<Option>
Option instances using descriptive methods.
Example usage:
Option option = Option.builder("a").required(true).longOpt("arg-name").build();
| Modifier and Type | Method | Description |
|---|---|---|
Option.Builder |
argName(String argName) |
Sets the display name for the argument value.
|
Option |
build() |
Deprecated.
Use
get(). |
Option.Builder |
converter(Converter<?,?> converter) |
Sets the converter for the option.
|
Option.Builder |
deprecated() |
Marks this Option as deprecated.
|
Option.Builder |
deprecated(DeprecatedAttributes deprecated) |
Sets whether the Option is deprecated.
|
Option.Builder |
desc(String description) |
Sets the description for this option.
|
Option |
get() |
Constructs an Option with the values declared by this
Option.Builder. |
Option.Builder |
hasArg() |
Tests whether the Option will require an argument.
|
Option.Builder |
hasArg(boolean hasArg) |
Tests whether the Option has an argument or not.
|
Option.Builder |
hasArgs() |
Tests whether the Option can have unlimited argument values.
|
Option.Builder |
longOpt(String longOption) |
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 required.
|
Option.Builder |
since(String since) |
Sets the version number when this option was first defined."
|
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 use
sep as a means to separate argument values. |
public Option.Builder argName(String argName)
argName - the display name for the argument value.this instance..@Deprecated public Option build()
get().Option.Builder.Option.IllegalArgumentException - if neither opt or longOpt has been set.public Option.Builder converter(Converter<?,?> converter)
Note: See TypeHandler for serialization discussion.
converter - the Converter to use.this instance..public Option.Builder deprecated()
public Option.Builder deprecated(DeprecatedAttributes deprecated)
deprecated - specifies whether the Option is deprecated.public Option.Builder desc(String description)
description - the description of the option.this instance..public Option get()
Option.Builder.get in interface Supplier<Option>Option.IllegalStateException - if neither opt or longOpt has been set.public Option.Builder hasArg()
this instance..public Option.Builder hasArg(boolean hasArg)
hasArg - specifies whether the Option takes an argument or not.this instance..public Option.Builder hasArgs()
public Option.Builder longOpt(String longOption)
longOption - the long name of the Optionpublic Option.Builder numberOfArgs(int argCount)
argCount - the number of argument valuespublic Option.Builder option(String option) throws IllegalArgumentException
option - the name of the Option.IllegalArgumentException - if there are any non valid Option characters in opt.public Option.Builder optionalArg(boolean optionalArg)
optionalArg - specifies whether the Option can have an optional argument.public Option.Builder required()
public Option.Builder required(boolean required)
required - specifies whether the Option is required.public Option.Builder since(String since)
since - the version number when this option was first defined.public Option.Builder type(Class<?> type)
type - the type of the Option.public Option.Builder valueSeparator()
public Option.Builder valueSeparator(char valueSeparator)
sep as a means to separate argument values.
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);
String propertyName = line.getOptionValues("D")[0]; // will be "key"
String propertyValue = line.getOptionValues("D")[1]; // will be "value"
valueSeparator - The value separator.Copyright © 2002-2025 The Apache Software Foundation. All rights reserved.Apache Commons CLI | Issue management | Source repository