Skip to content

Commit

Permalink
Fixed falsely parsed subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Mar 5, 2025
1 parent b0ab826 commit 1cca21e
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

allprojects {
group = "dev.velix"
version = "1.7.0"
version = "1.7.1"

ext {
def kyoriVersion = "4.17.0"
Expand Down
11 changes: 9 additions & 2 deletions core/src/main/java/dev/velix/imperat/BaseImperat.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,19 @@ private CommandDispatch.Result handleExecution(Context<S> context) throws Impera
CommandUsage<S> usage = searchResult.toUsage(command);

//executing usage
if (searchResult.result() == CommandDispatch.Result.COMPLETE)
if (searchResult.result() == CommandDispatch.Result.COMPLETE) {
ImperatDebugger.debug("Executing usage '%s'", usage != null ? CommandUsage.format(command, usage) : "NULL");
executeUsage(command, source, context, usage);
}
else if (searchResult.result() == CommandDispatch.Result.INCOMPLETE) {
var lastParameter = searchResult.getLastParameter();
ImperatDebugger.debug("Executing subcommand-main-usage '%s'", CommandUsage.format(command, lastParameter.asCommand().mainUsage()));
ImperatDebugger.debug("Last param = '%s'", lastParameter.format());
if (lastParameter.isCommand()) {
executeUsage(command, source, context, lastParameter.asCommand().getDefaultUsage());
ImperatDebugger.debug("Executing usage '%s'", CommandUsage.format(command, lastParameter.asCommand().mainUsage()));
executeUsage(command, source, context, lastParameter.asCommand().mainUsage());
} else if (usage != null) {
ImperatDebugger.debug("Executing usage '%s'", CommandUsage.format(command, usage));
executeUsage(command, source, context, usage);
} else {
//usage is null and last param is not command
Expand All @@ -308,6 +314,7 @@ private void executeUsage(

ResolvedContext<S> resolvedContext = config.getContextFactory().createResolvedContext(context, usage);
resolvedContext.resolve();
resolvedContext.debug();

//global post-processing
postProcess(resolvedContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public static <S extends Source> MethodCommandExecutor<S> of(
public void execute(S source,
ExecutionContext<S> context) throws ImperatException {

ImperatDebugger.debug("Debugging params:-");
for (var param : fullParameters) {
ImperatDebugger.debug("-%s", param.format());
}
ImperatDebugger.debug("SIZE=%s", fullParameters.size());

var instances = AnnotationHelper.loadParameterInstances(
dispatcher, fullParameters,
source, context, method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,35 @@ private CommandUsage<S> loadUsage(
@NotNull Command<S> loadedCmd,
MethodElement method
) {
if (method.getInputCount() == 0) {
loadedCmd.setDefaultUsageExecution(
MethodCommandExecutor.of(imperat, method, Collections.emptyList())
);
return null;
int inputCount = method.getInputCount();
if (parentCmd != null) {
int parentalParams = 0;

if (!(method.isAnnotationPresent(SubCommand.class) && Objects.requireNonNull(method.getAnnotation(SubCommand.class)).attachDirectly())) {
Command<S> parent = parentCmd;
while (parent != null) {
parentalParams += parent.mainUsage().size();
parent = parent.parent();
}
}

inputCount = Math.abs(method.getInputCount() - parentalParams);
}

if (inputCount == 0) {
if (parentCmd != null) {
MethodUsageData<S> usageData = loadParameters(method, parentCmd);
loadedCmd.setDefaultUsageExecution(
MethodCommandExecutor.of(imperat, method, usageData.inheritedTotalParameters())
);
} else {
loadedCmd.setDefaultUsageExecution(
MethodCommandExecutor.of(imperat, method, Collections.emptyList())
);
}

return null;
}
ClassElement methodOwner = (ClassElement) method.getParent();
assert methodOwner != null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,6 @@ default void resolveFlag(
* @return The used usage to use it to resolve commands
*/
CommandUsage<S> getDetectedUsage();

void debug();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import dev.velix.imperat.exception.ImperatException;
import dev.velix.imperat.exception.NumberOutOfRangeException;
import dev.velix.imperat.resolvers.ContextResolver;
import dev.velix.imperat.util.ImperatDebugger;
import dev.velix.imperat.util.Registry;
import dev.velix.imperat.util.TypeUtility;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -233,4 +234,12 @@ public CommandUsage<S> getDetectedUsage() {
return usage;
}

@Override
public void debug() {
for (var arg : allResolvedArgs.getAll()) {
ImperatDebugger.debug("Argument '%s' at index #%s with input='%s' with value='%s'",
arg.parameter().format(), arg.index(), arg.raw(), arg.value());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public void defaultUsage(TestSource source) {
}

@Usage
public void sub1Main(TestSource source, @Named("a") String a) {
source.reply("sub1-main a=" + a);
public void sub1Main(TestSource source, @Named("otherText") String otherText, @Named("a") String a) {
source.reply("otherText=" + otherText + ", sub1-main a=" + a);
}

@SubCommand("sub2")
Expand All @@ -65,7 +65,7 @@ public void defaultUsage(TestSource source) {
}

@Usage
public void sub2Main(TestSource source, @Named("b") String b) {
public void sub2Main(TestSource source, @Named("otherText") String otherText, @Named("a") String a, @Named("b") String b) {
source.reply("sub2-main b=" + b);
}

Expand All @@ -78,7 +78,7 @@ public void defaultUsage(TestSource source) {
}

@Usage
public void sub3Main(TestSource source, @Named("c") String c) {
public void sub3Main(TestSource source, @Named("otherText") String otherText, @Named("a") String a, @Named("b") String b, @Named("c") String c) {
source.reply("sub3 c=" + c);
}

Expand Down

0 comments on commit 1cca21e

Please sign in to comment.