Skip to content

Commit

Permalink
Fixed CommandInputStream issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Oct 7, 2024
1 parent 9385116 commit 15c627f
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static <S extends Source> Builder<S> builder() {
* @return the flag from the raw input, null if it cannot be a flag
*/
@Nullable
FlagData getFlagFromRaw(String rawInput);
FlagData<S> getFlagFromRaw(String rawInput);

/**
* Adds parameters to the usage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ protected ParameterFlag() {

if (!flagParameter.isSwitch()) {
ParameterType<S, ?> inputType = flagParameter.flagData().inputType();
rawInput = commandInputStream.popRaw().orElseThrow();
input = inputType.resolve(context, commandInputStream);
rawInput = commandInputStream.popRaw().orElse(null);
if (rawInput != null) {
input = inputType.resolve(context, commandInputStream);
}
} else {
System.out.println("SWITCH INPUT = TRUE");
input = true;
}
return new CommandFlag(flagParameter.flagData(), rawFlag, rawInput, input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

public final class ParameterString<S extends Source> extends BaseParameterType<S, String> {

private final static char DOUBLE_QUOTE = '"', SINGLE_QUOTE = '\'', BACKSLASH = '\\';
private final static char DOUBLE_QUOTE = '"', SINGLE_QUOTE = '\'';

ParameterString() {
super(TypeWrap.of(String.class));
Expand All @@ -33,10 +33,6 @@ public final class ParameterString<S extends Source> extends BaseParameterType<S
//we shift to next char
next = inputStream.popLetter().orElse(null);
if (next == null) break;
if (Character.isWhitespace(current) || next == BACKSLASH) {
continue;
}

builder.append(next);

} while (inputStream.hasNextLetter() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ <T> void resolveArgument(
* @param flagInputValue the flag's input value resolved by {@link ParameterType#resolve(ExecutionContext, CommandInputStream)}
*/
default void resolveFlag(
FlagData flagDetected,
FlagData<S> flagDetected,
String flagRaw,
@Nullable String flagInputRaw,
@Nullable Object flagInputValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ public Optional<String> popRaw() {
@Override
public boolean hasNextLetter() {
return Optional.ofNullable(currentRaw())
.map((raw) -> (letterPos < raw.length())).orElse(false);
.map((raw) -> {
if (letterPos >= raw.length()) {
return peekRaw().isPresent();
}
return true;
}).orElse(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public <T> void resolveArgument(
@Override
public void resolveFlag(CommandFlag flag) {
flagRegistry.setData(flag.flag().name(), flag);
System.out.println("REGISTERING " + flag.flag().name() + " to value " + flag.value());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public void resolve() throws ImperatException {

String currentRaw = stream.currentRaw();
if (currentRaw == null) {
//ImperatDebugger.visualize("Filling empty optional args");
if (currentParameter.isOptional()) {
handleEmptyOptional(currentParameter);
}
Expand Down Expand Up @@ -102,15 +101,18 @@ public void resolve() throws ImperatException {
getDefaultValue(context, currentParameter),
currentParameter.asFlagParameter().flagData()
);
stream.skipParameter();
stream.skip();
continue;
}
//TODO fix the infinity error
//ImperatDebugger.debug("FLAG DETECTED=`%s`, current-raw=`%s`, current-param=`%s`", (flag == null ? null : flag.name()), currentRaw, currentParameter.name());
var value = currentParameter.type().resolve(context, stream);
//ImperatDebugger.debug("AfterResolve >> current-raw=`%s`, current-param=`%s`", currentRaw, currentParameter.name());
if (value instanceof CommandFlag commandFlag) {
context.resolveFlag(commandFlag);
stream.skip();
} else if (currentParameter.isOptional()) {
//ImperatDebugger.debug("Resolving optional %s", currentParameter.name());
resolveOptional(
currentRaw,
currentParameter,
Expand Down
55 changes: 41 additions & 14 deletions core/src/test/java/dev/velix/imperat/TestSmartUsageResolve.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import static dev.velix.imperat.TestRun.IMPERAT;
import static dev.velix.imperat.TestRun.SOURCE;
Expand Down Expand Up @@ -46,19 +47,20 @@ private static void test(String cmdLine, ResolvedArgsData expected) {
System.arraycopy(split, 1, raws, 0, split.length - 1);
//System.arraycopy(split, 1, raws, 0, raws.length - 1);

var input = ResolvedArgsData.of(
inputResolve(split[0], raws)
);
input.debug();
Assertions.assertTrue(expected
.matches(
ResolvedArgsData.of(
inputResolve(split[0], raws)
)
input
)
);
}

@Test
public void banWithSwitchFlag() {
IMPERAT.registerCommand(new BanCommand());

test(
"ban mqzen",
ResolvedArgsData.empty()
Expand All @@ -67,16 +69,17 @@ public void banWithSwitchFlag() {
.arg("duration", null)
.arg("reason", "Breaking server laws")
);


test("ban mqzen -s",
// /ban <user> [reason]
// /ban mqzen "for being a good man"
test(
"ban mqzen -s",
ResolvedArgsData.empty()
.arg("player", "mqzen")
.flag("silent", true)
.arg("duration", null)
.arg("reason", "Breaking server laws")
);

);
test(
"ban mqzen -s 1d",
ResolvedArgsData.empty()
Expand All @@ -86,10 +89,8 @@ public void banWithSwitchFlag() {
.arg("reason", "Breaking server laws")

);


test(
"ban mqzen -s 1d A disgrace to community",
"ban mqzen -s 1d \"A disgrace to community\"",
ResolvedArgsData.empty()
.arg("player", "mqzen")
.flag("silent", true)
Expand Down Expand Up @@ -144,18 +145,44 @@ public boolean matches(ResolvedArgsData other) {
System.out.println(other.resolvedFlags.getMap());
*/


if (args.size() != other.args.size() || resolvedFlags.size() != other.resolvedFlags.size()) return false;

for (var entry : this.args.entrySet()) {
var thisObj = entry.getValue();
var otherObj = other.args.get(entry.getKey());
if (otherObj == null) break;
if (!thisObj.equals(otherObj)) {
if (!Objects.equals(thisObj, otherObj)) {
return false;
}
}

for (String flagKey : resolvedFlags.getKeys()) {
Object flagValue = resolvedFlags.getData(flagKey).orElse(null);
if (!other.resolvedFlags.getMap().containsKey(flagKey)) {
return false;
}
if (!Objects.equals(flagValue, other.resolvedFlags.getData(flagKey)
.orElse(null))) {
return false;
}
}
return true;
}

public void debug() {

System.out.println("----------------");
System.out.println("Args: ");
for (var arg : args.keySet()) {
System.out.println(" '" + arg + "' -> " + args.get(arg));
}

System.out.println("Flags: ");
for (var flag : resolvedFlags.getKeys()) {
var flagInputValue = resolvedFlags.getData(flag).orElse(null);
System.out.println(" '" + flag + "' -> " + flagInputValue);
}

return resolvedFlags.equals(other.resolvedFlags);
}
}

Expand Down

0 comments on commit 15c627f

Please sign in to comment.