-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Flag and Option annotations for parsing options via arguments
- Loading branch information
Showing
10 changed files
with
229 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/me/despical/commandframework/annotations/Flag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.despical.commandframework.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author Despical | ||
* <p> | ||
* Created at 20.09.2024 | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Repeatable(Flag.FlagContainer.class) | ||
public @interface Flag { | ||
|
||
String[] value(); | ||
|
||
String prefix() default "--"; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface FlagContainer { | ||
|
||
Flag[] value(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/me/despical/commandframework/annotations/Option.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package me.despical.commandframework.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Repeatable; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author Despical | ||
* <p> | ||
* Created at 20.09.2024 | ||
*/ | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Repeatable(Option.OptionContainer.class) | ||
public @interface Option { | ||
|
||
String name(); | ||
|
||
String prefix() default "--"; | ||
|
||
String valueSeparator() default ","; | ||
|
||
String keySeparator() default "="; | ||
|
||
boolean allowSeparating() default true; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@interface OptionContainer { | ||
|
||
Option[] value(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 1 addition & 4 deletions
5
src/main/java/me/despical/commandframework/exceptions/CooldownException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
package me.despical.commandframework.exceptions; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
/** | ||
* @author Despical | ||
* <p> | ||
* Created at 18.07.2024 | ||
*/ | ||
@ApiStatus.Internal | ||
public final class CooldownException extends RuntimeException { | ||
public class CooldownException extends CommandException { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/me/despical/commandframework/exceptions/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @author Despical | ||
* <p> | ||
* Created at 20.09.2024 | ||
*/ | ||
@ApiStatus.Internal | ||
package me.despical.commandframework.exceptions; | ||
|
||
import org.jetbrains.annotations.ApiStatus; |
102 changes: 102 additions & 0 deletions
102
src/main/java/me/despical/commandframework/parser/OptionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package me.despical.commandframework.parser; | ||
|
||
import me.despical.commandframework.annotations.Flag; | ||
import me.despical.commandframework.annotations.Option; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.*; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* @author Despical | ||
* <p> | ||
* Created at 20.09.2024 | ||
*/ | ||
public class OptionParser { | ||
|
||
private final Flag[] flags; | ||
private final Option[] options; | ||
private final Set<String> arguments; | ||
private final Set<String> parsedFlags; | ||
private final Map<String, List<String>> parsedOptions; | ||
|
||
public OptionParser(String[] arguments, Method method) { | ||
this.flags = method.getAnnotationsByType(Flag.class); | ||
this.options = method.getAnnotationsByType(Option.class); | ||
this.arguments = new HashSet<>(Arrays.asList(arguments)); | ||
this.parsedFlags = new HashSet<>(); | ||
this.parsedOptions = new HashMap<>(); | ||
} | ||
|
||
public Map<String, List<String>> parseOptions() { | ||
for (Option option : options) { | ||
this.parseOption(option); | ||
} | ||
|
||
return this.parsedOptions; | ||
} | ||
|
||
public Set<String> parseFlags() { | ||
for (Flag flag : flags) { | ||
this.parseFlag(flag); | ||
} | ||
|
||
return this.parsedFlags; | ||
} | ||
|
||
private void parseOption(Option option) { | ||
String prefix = option.prefix(); | ||
String keySeparator = Pattern.quote(option.keySeparator()); | ||
String valueSeparator = Pattern.quote(option.valueSeparator()); | ||
Iterator<String> iterator = arguments.iterator(); | ||
|
||
while (iterator.hasNext()) { | ||
String argument = iterator.next(); | ||
|
||
if (!argument.startsWith(prefix)) { | ||
continue; | ||
} | ||
|
||
if (option.allowSeparating() && !argument.contains(keySeparator)) { | ||
continue; | ||
} | ||
|
||
String[] options = argument.substring(prefix.length()).split(keySeparator); | ||
|
||
if (!option.allowSeparating()) { | ||
String value = options.length <= 1 ? "" : options[1]; | ||
this.parsedOptions.put(option.name(), Collections.singletonList(value)); | ||
|
||
iterator.remove(); | ||
continue; | ||
} | ||
|
||
String[] values = options[1].split(valueSeparator); | ||
this.parsedOptions.put(option.name(), Arrays.asList(values)); | ||
|
||
iterator.remove(); | ||
} | ||
} | ||
|
||
private void parseFlag(Flag flag) { | ||
String prefix = flag.prefix(); | ||
|
||
outer: | ||
for (String argument : this.arguments) { | ||
if (!argument.startsWith(prefix)) { | ||
continue; | ||
} | ||
|
||
String foundFlag = argument.substring(prefix.length()); | ||
|
||
for (String flagName : flag.value()) { | ||
if (!flagName.equals(foundFlag)) { | ||
continue; | ||
} | ||
|
||
this.parsedFlags.add(flagName); | ||
continue outer; | ||
} | ||
} | ||
} | ||
} |