New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect behavior of flags with optional value #1978
Comments
The way flags work are a bit different than you expect. Flags are not themselves Additionally, flags should not be combined with optional elements because it adds multiple layers of optional. Flags are inheritably optional, so you should stick to using The idea of including a way to determine what flags were specified themselves is definitely meritable and worth discussion for API 8. |
I wondered how I'd missed this in the first place, but then realised I was on holiday at the time. While @WillBAnders's response is mostly correct, there is one thing that I'd contest:
This is true, except in the case where you might want to be able to omit a value so that specifying a flag has a "default" value. That's clearly what is wanted here ( In other words, changing:
to
would resolve this issue as required. A note on how As a stop gap, probably could add something to the |
That's a good clarification. Optional elements themselves are fine with things like defaults. The issues I was referring to only happen when you have layers of non-default optionals - like combining multiple nulls, it's easy to loose context for what that means. I'm all in for adding a |
That's not what I was suggesting. I was thinking you could have something outside of the whole |
I like that solution better than merging it into arguments because it's effectively not an argument. A |
Honestly, having slept on it, I'm not sure it's necessary at all. Or rather, I can't really see a use case for this information precisely because they aren't arguments and they should have been handled by being given a value, or whatever. My thought about a collection is that we would just need to store the names of the flags. Then you can get a copy of that collection and inspect it as will. I don't see it needing to be any more complex than that - I'm not sure exactly why a map is necessary but, hey, the devil will be in the detail, I'm sure. If you make a PR and explain what use cases there are, I'm not against it. I'm just not sure there is any benefit so I'd like to see what they are. |
PR merged |
I registered a command with a flag that takes an optional value.
CommandElement flagValue = GenericArguments.bool(Text.of("flag_value"));
CommandElement optionalFlagValue = GenericArguments.optional(flagValue);
CommandFlags.Builder flags = GenericArguments.flags();
flags.valueFlag(optionalFlagValue, "-flag");
CommandElement commandElement = flags.buildWith(GenericArguments.none());
As well as a handler that displays data on the flag. ps: Just in case, I tried to use "-".
Text.of("has flag_value ", args.hasAny("flag_value") )
Text.of("has -flag_value ", args.hasAny("-flag_value"))
Text.of("has flag ", args.hasAny("flag"))
Text.of("has -flag ", args.hasAny("-flag"))
Text.of("get flag_value ", args.getOne("flag_value"))
Text.of("get -flag_value ", args.getOne("-flag_value"))
Text.of("get flag ", args.getOne("flag"))
Text.of("get -flag ", args.getOne("-flag"))
For command "/test_flags --flag", the result can be seen in the screenshot below.
Expected behavior:
By my logic,
args.hasAny("flag")
should have returned true, andargs.getOne("flag_value")
should returnOptional.empty()
. If I am mistaken in some way, then correct me.Server:
spongevanilla-1.12.2-7.1.5
p.s. The text is translated by a translator.
The text was updated successfully, but these errors were encountered: