ArgumentParser with 0 arguments doesn't function (flags)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 548
- Forks
- 60
- PR merge metrics
- No merged PRs in 30d
Description
When you create your own ArgumentParser that returns 0 in getRequestedArgumentCount(), with the intention of it inspecting the command context to deduce the argument value from there, it doesnt work. I used one with my own flag, --nearest. But when I tried to run the command with --nearest, it gives an error that it expects an argument. Despite argument count being 0.
Another issue is that you can't have a variable number of argument values for a flag, but that's a different issue altogether and less important here.
If there is an alternative way to register a new flag with some sort of 'value supplier' or function CommandContext -> Value, that would be fine too. There's no suggestions and the queue isn't used, anyway, so using an ArgumentParser for this is a bit out of place as well.
Example code:
private static class NearestParser implements ArgumentParser<CommandSender, NearPosition> {
private final LocationArgument.LocationParser<CommandSender> locationParser = new LocationArgument.LocationParser<>();
@Override
public ArgumentParseResult<NearPosition> parse(
final CommandContext<CommandSender> commandContext,
final Queue<String> inputQueue
) {
Queue<String> atSenderQueue = new LinkedList<>();
atSenderQueue.add("~");
atSenderQueue.add("~");
atSenderQueue.add("~");
ArgumentParseResult<Location> locationResult = this.locationParser.parse(commandContext, atSenderQueue);
if (locationResult.getFailure().isPresent()) {
return ArgumentParseResult.failure(
locationResult.getFailure().get()
);
}
// Done!
return ArgumentParseResult.success(new NearPosition(
locationResult.getParsedValue().get(),
128.0));
}
@Override
public List<String> suggestions(
final CommandContext<CommandSender> commandContext,
final String input
) {
return Collections.emptyList();
}
@Override
public int getRequestedArgumentCount() {
return 0;
}
}
private static class NearArgument extends CommandArgument<CommandSender, NearPosition> {
private NearArgument(String name) {
super(true, name, new NearestParser(), "",
TypeToken.get(NearPosition.class),
null,
Collections.emptyList());
}
}
private final CommandFlag<NearPosition> flagNearest = CommandFlag.newBuilder("nearest")
.withArgument(new NearArgument("where"))
.build();
// Somewhere:
builder = builder.flag(flagNearest);
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ArgumentParser.getRequestedArgumentCount contract and trace the CommandFlag and CommandArgument registration path used by the --nearest example. Reproduce the command with a parser returning zero arguments, then identify why it still reports a missing argument. Done means a zero-argument parser can derive its value from CommandContext without the flag requiring input.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100