diff --git a/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java b/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java index e60da783..a74a6733 100644 --- a/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java +++ b/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java @@ -38,6 +38,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Optional; +import java.util.function.Supplier; /** * Command context used to assist in the parsing of commands @@ -238,6 +239,22 @@ public final class CommandContext { return this.getOptional(key).orElse(defaultValue); } + /** + * Get a value if it exists, else return the value supplied by the given supplier + * + * @param key Argument key + * @param defaultSupplier Supplier of default value + * @param Argument type + * @return Argument, or supplied default value + * @since 1.2.0 + */ + public @Nullable T getOrDefault( + final @NonNull String key, + final @NonNull Supplier<@Nullable T> defaultSupplier + ) { + return this.getOptional(key).orElseGet(defaultSupplier); + } + /** * Get the raw input. This should only be used when {@link #isSuggestions()} is {@code true} *