public class Option<T> extends Object
An option may have a short (single-dash) or long (double-dash) variant.
Each option may optionally take a value, which may or may not be allowed to be separated from the option.
For instance, if valueMayBeSeparate(boolean)
is specified as false,
then only -Dwhatever
would be allowed, while -D whatever
would not.
Upon successful match, each Option
has an Option.Action
associated
which is called with the value (if any) and the state-holding CommandLine
object.
Modifier and Type | Class and Description |
---|---|
static interface |
Option.Action<T>
Callback functional interface for matched options.
|
Constructor and Description |
---|
Option()
Construct an empty option.
|
Modifier and Type | Method and Description |
---|---|
void |
displayHelp(PrintStream out)
Display formatted help associated with this option.
|
Option<T> |
hasValue(String valueDescription)
Specify that this option takes an argument value.
|
Option<T> |
then(Option.Action<T> action)
Associate an action with this option.
|
String |
toString() |
static URL |
toURL(String value)
Helper to attempt forming a URL from a String in a sensible fashion.
|
Option<T> |
valueMayBeSeparate(boolean valueMayBeSeparate)
Indicate if the value to the option may be separated by whitespace.
|
Option<T> |
withDefault(Supplier<T> supplier)
Specify a default value supplier for this option.
|
Option<T> |
withDescription(String description)
Set a human-readable description of this option.
|
Option<T> |
withLong(String longArg)
Specify a long (double-dash) variant.
|
Option<T> |
withShort(Character shortArg)
Specify a short (single-dash) variant.
|
public Option<T> then(Option.Action<T> action)
action
- The action to call when matched.Option
object.public void displayHelp(PrintStream out)
out
- The output stream to display the help upon.public Option<T> withShort(Character shortArg)
A single character following a single dash, such as -c
.
shortArg
- The character.Option
object.public Option<T> withLong(String longArg)
A string following a double-dash, such as --help
.
longArg
- The string (without dashes).Option
object.public Option<T> withDescription(String description)
description
- The description.Option
object.public Option<T> hasValue(String valueDescription)
A non-null string passed to this method will be used to signify that this option may take an argument, and to describe its format.
For instance a string of name[=value]
name
is expected, with an optional value
which should follow an equal sign.valueDescription
- public Option<T> valueMayBeSeparate(boolean valueMayBeSeparate)
true
).
In some cases, because of ambiguity, it may be required to indicate that an argument may not be separate from the option. If an argument is to be provided, it must immediately follow the option without whitespace.
valueMayBeSeparate
- true
(the default) if value may be separate, or false
to indicate it must be conjoined.Option
object.public Option<T> withDefault(Supplier<T> supplier)
supplier
- The supplier.Option
object.public static URL toURL(String value) throws MalformedURLException
value
- The input string.MalformedURLException
Copyright © 2018 JBoss by Red Hat. All rights reserved.