Skip to content

Commit 77a44d4

Browse files
authored
feat(annotations): add @Default (#552)
1 parent 27a7eef commit 77a44d4

5 files changed

Lines changed: 61 additions & 10 deletions

File tree

cloud-annotations/src/main/java/cloud/commandframework/annotations/Argument.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@
7373
*/
7474
@NonNull String suggestions() default "";
7575

76-
/**
77-
* Get the default value
78-
*
79-
* @return Default value
80-
*/
81-
@NonNull String defaultValue() default "";
82-
8376
/**
8477
* The argument description
8578
*

cloud-annotations/src/main/java/cloud/commandframework/annotations/ArgumentExtractorImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,16 @@ class ArgumentExtractorImpl implements ArgumentExtractor {
7272
name = argument.value();
7373
}
7474

75+
String defaultValue = null;
76+
if (parameter.isAnnotationPresent(Default.class)) {
77+
defaultValue = parameter.getAnnotation(Default.class).value();
78+
}
79+
7580
final ArgumentDescriptor argumentDescriptor = ArgumentDescriptor.builder()
7681
.parameter(parameter)
7782
.name(name)
7883
.parserName(nullIfEmpty(argument.parserName()))
79-
.defaultValue(nullIfEmpty(argument.defaultValue()))
84+
.defaultValue(defaultValue)
8085
.description(this.descriptionMapper.apply(argument))
8186
.suggestions(nullIfEmpty(argument.suggestions()))
8287
.build();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// MIT License
3+
//
4+
// Copyright (c) 2022 Alexander Söderberg & Contributors
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
//
24+
package cloud.commandframework.annotations;
25+
26+
import java.lang.annotation.ElementType;
27+
import java.lang.annotation.Retention;
28+
import java.lang.annotation.RetentionPolicy;
29+
import java.lang.annotation.Target;
30+
import org.apiguardian.api.API;
31+
import org.checkerframework.checker.nullness.qual.NonNull;
32+
33+
/**
34+
* Used to give an optional command component a
35+
* {@link cloud.commandframework.arguments.DefaultValue#parsed(String) parsed default value}.
36+
*
37+
* @since 2.0.0
38+
*/
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Target(ElementType.PARAMETER)
41+
@API(status = API.Status.STABLE, since = "2.0.0")
42+
public @interface Default {
43+
44+
/**
45+
* Returns the default value.
46+
* <p>
47+
* This value will be parsed when the command is being parsed in the case that the optional parameter has been omitted.
48+
*
49+
* @return the default value
50+
*/
51+
@NonNull String value();
52+
}

cloud-annotations/src/test/java/cloud/commandframework/annotations/AnnotationParserTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void injectedCommand(final CommandContext<TestCommandSender> context) {
227227
public void testCommand(
228228
final TestCommandSender sender,
229229
@Argument("int") @Range(max = "100") final int argument,
230-
@Argument(value = "string", defaultValue = "potato", parserName = "potato") final String string
230+
@Argument(value = "string", parserName = "potato") @Default("potato") final String string
231231
) {
232232
System.out.printf("Received int: %d and string '%s'\n", argument, string);
233233
}

examples/example-bukkit/src/main/java/cloud/commandframework/examples/bukkit/annotations/feature/StringArrayExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import cloud.commandframework.annotations.AnnotationParser;
2727
import cloud.commandframework.annotations.Argument;
2828
import cloud.commandframework.annotations.CommandMethod;
29+
import cloud.commandframework.annotations.Default;
2930
import cloud.commandframework.examples.bukkit.ExamplePlugin;
3031
import cloud.commandframework.examples.bukkit.annotations.AnnotationFeature;
3132
import org.apache.commons.lang.StringUtils;
@@ -49,7 +50,7 @@ public void registerFeature(
4950
@CommandMethod("annotations arraycommand [args]")
5051
public void arrayCommand(
5152
final @NonNull CommandSender sender,
52-
@Argument(value = "args", defaultValue = "") final @NonNull String[] args
53+
@Argument(value = "args") @Default("") final @NonNull String[] args
5354
) {
5455
sender.sendMessage("You wrote: " + StringUtils.join(args, " "));
5556
}

0 commit comments

Comments
 (0)