✨ Allow for use of named suggestion providers in @Flag annotated command method parameters, add methods to FlagContext to work with flag values as optionals
This commit is contained in:
parent
6d0301d9dd
commit
fc1a613463
10 changed files with 104 additions and 30 deletions
|
|
@ -28,6 +28,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
|
|||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Flag value mappings
|
||||
|
|
@ -78,11 +79,11 @@ public final class FlagContext {
|
|||
}
|
||||
|
||||
/**
|
||||
* Check whether or not a flag is present. This will return {@code false}
|
||||
* Check whether a presence flag is present. This will return {@code false}
|
||||
* for all value flags.
|
||||
*
|
||||
* @param flag Flag name
|
||||
* @return {@code true} if the flag is presence and the flag is a presence flag,
|
||||
* @return {@code true} if the flag is a presence flag and is present,
|
||||
* else {@code false}
|
||||
*/
|
||||
public boolean isPresent(final @NonNull String flag) {
|
||||
|
|
@ -90,6 +91,25 @@ public final class FlagContext {
|
|||
return FLAG_PRESENCE_VALUE.equals(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a flag value as an optional. Will be empty if the value is not present.
|
||||
*
|
||||
* @param name Flag name
|
||||
* @param <T> Value type
|
||||
* @return Optional containing stored value if present
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public <T> @NonNull Optional<T> getValue(
|
||||
final @NonNull String name
|
||||
) {
|
||||
final Object value = this.flagValues.get(name);
|
||||
if (value == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
@SuppressWarnings("unchecked") final T casted = (T) value;
|
||||
return Optional.of(casted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a flag value
|
||||
*
|
||||
|
|
@ -102,12 +122,22 @@ public final class FlagContext {
|
|||
final @NonNull String name,
|
||||
final @Nullable T defaultValue
|
||||
) {
|
||||
final Object value = this.flagValues.get(name);
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
@SuppressWarnings("unchecked") final T casted = (T) value;
|
||||
return casted;
|
||||
return this.<T>getValue(name).orElse(defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a flag is present. This will return {@code true} if the flag
|
||||
* is a presence flag and is present, or if the flag is a value flag and has
|
||||
* a value provided.
|
||||
*
|
||||
* @param name Flag name
|
||||
* @return {@code true} if the flag is present, else {@code false}
|
||||
* @since 1.2.0
|
||||
*/
|
||||
public boolean hasFlag(
|
||||
final @NonNull String name
|
||||
) {
|
||||
return this.getValue(name).isPresent();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -140,7 +140,7 @@ public interface ParserRegistry<C> {
|
|||
* Get a named suggestion provider, if a suggestion provider with the given name exists in the registry
|
||||
*
|
||||
* @param name Suggestion provider name. The name is case independent.
|
||||
* @return Optional that either contains the suggestion provider name, or nothing ({@link Optional#empty()}) if no
|
||||
* @return Optional that either contains the suggestion provider, or is empty if no
|
||||
* suggestion provider is registered with the given name
|
||||
* @see #registerSuggestionProvider(String, BiFunction) Register a suggestion provider
|
||||
* @since 1.1.0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue