Skip to content

Commit 86446eb

Browse files
committed
Refactor flag handling logic in SmartUsageResolve to improve free flag detection and add debugging information
1 parent 28fb84e commit 86446eb

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

core/src/main/java/dev/velix/imperat/context/internal/SmartUsageResolve.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,34 @@ public void resolve() throws ImperatException {
109109
if (currentParameter.isFlag()) {
110110
handleParameterFlag(currentParameter, currentRaw, flag);
111111
continue;
112-
} else if (Patterns.isInputFlag(currentRaw) && command.getFlagFromRaw(currentRaw).isPresent()) {
113-
//FOUND FREE FLAG
114-
var flagData = command.getFlagFromRaw(currentRaw).get();
115-
ParameterFlag<S> parameterFlag = ParameterTypes.flag(flagData);
116-
context.resolveFlag(parameterFlag.resolveFreeFlag(context, stream, flagData));
112+
} else if (Patterns.isInputFlag(currentRaw)) {
113+
114+
if(command.getFlagFromRaw(currentRaw).isPresent()) {
115+
116+
//FOUND FREE FLAG
117+
var flagData = command.getFlagFromRaw(currentRaw).get();
118+
ImperatDebugger.debug("Found free flag '%s' from raw input '%s'", flagData.name(), currentRaw);
119+
120+
ParameterFlag<S> parameterFlag = ParameterTypes.flag(flagData);
121+
context.resolveFlag(parameterFlag.resolveFreeFlag(context, stream, flagData));
122+
123+
}else {
124+
125+
/*
126+
* if the current param IS NOT A FLAG, & the current input matches a flag's criteria
127+
* if the input also isn't from the free flags register that is linked to the command's instance, THEN
128+
* this can be a case of two consecutive optional flags next to each other , example usage:
129+
* '/ban <target> [-silent] [-ip] [duration] [reason]'
130+
* If user enters '/ban mqzen -s -ip' this should work flawlessly, while '/ban mqzen -ip -s' DOESN'T resolve the silent flag,
131+
* because of the algorithm that retains the parameters order while resolving their values.
132+
* I think this can be fixed in the command tree instead of doing it here by making the tree shuffle the possibilities of
133+
* flag-command-nodes.
134+
*/
135+
136+
//TODO fix this in the command tree
137+
}
138+
139+
//we skip the raw input cursor only
117140
stream.skipRaw();
118141
continue;
119142
}
@@ -163,7 +186,7 @@ public void resolve() throws ImperatException {
163186
}
164187
}
165188

166-
private void handleParameterFlag(CommandParameter<S> currentParameter, String currentRaw, FlagData<S> flag) throws ImperatException {
189+
private void handleParameterFlag(CommandParameter<S> currentParameter, String currentRaw, @Nullable FlagData<S> flag) throws ImperatException {
167190
ImperatDebugger.debug("Found parameter flag '%s' from raw input '%s'", currentParameter.format(), currentRaw);
168191
ImperatDebugger.debug("It's FlagData='%s'", (flag == null ? "NULL" : flag.name()));
169192

@@ -202,12 +225,14 @@ private CommandParameter<S> findMatchingFlagParameter(
202225
ParameterFlag<S> parameterFlag
203226
) {
204227
CommandParameter<S> flagParam = currentParameter;
228+
205229
while (flagParam != null && !parameterFlag.matchesInput(currentRaw, flagParam)) {
206230
if (!flagParam.isFlag()) {
207231
break;
208232
}
209233
flagParam = stream.popParameter().orElse(null);
210234
}
235+
211236
return flagParam;
212237
}
213238

0 commit comments

Comments
 (0)