-
Notifications
You must be signed in to change notification settings - Fork 390
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
Add CommandResult extensions for HasOption() and HasArgument() #1272
Conversation
@jonsequitur - don't know the formal process here, but would be good to get this reviewed at some point if possible. 😃 |
No complicated formal process, other than we'd want tests. But that said, I'm wondering if |
I agree with Jon. I think some of that might be cleared up by a slight name change. To try and communicate the intent with how it is work (a convenience helper for FindResultFor), perhaps simply including the term "Result" in the name. Thoughts? |
We could go with that approach. The names I've chosen so far are similar to Anyway, I'll go ahead and try to fix some tests now, they need to be done either way. 😊 |
85d4a9f
to
c8b77c1
Compare
Tests added, feel free to give it a look @jonsequitur @Keboo. As far as the suffixing is concerned, I'm not fully convinced yet. The current form reads quite well, as least in the test: CommandResult completeResult = result.CommandResult;
completeResult.HasArgument(stringArgument).Should().BeFalse(); CommandResult completeResult = result.CommandResult;
completeResult.HasOption(positionOption).Should().BeTrue(); Compared to the pre-existing tests in e.g. var option = new Option<string>("-x");
var result = option.Parse("");
result.HasOption(option)
.Should()
.BeFalse(); |
The distinction between the two that I'm pointing out (and yeah, I know I'm being very nitpicky) is that in the existing |
Ahh, thanks for making this a bit clearer. 👍 (I'm honestly quite a novice in this code base.) I wonder if something like Naming is indeed hard. 😅 |
Any feedback on these options (pun not intended 🙈) would be nice, I can try to get this finalized once we settle on the naming. |
@perlun I've proposed naming for a few new methods for related functionality here: #1127 (comment). Thoughts? |
@jonsequitur Hmm, interesting. So maybe something like |
The key difference in behavior is whether the lookup encompasses the entire It's possible we're better off just recommending the pattern Out of curiosity, can you say more about your use cases for these new methods? |
Yeah, maybe you're right that it would be a bad fit for the "generic" nature that this library want to support. I think
Sure. It's basically something like this (simplified example, the real-world code has more options etc). var rootCommand = new RootCommand
{
// Define Description, Handler etc
}
var evalOption = new Option<string>("-e", "Executes a single-line script") { AllowMultipleArgumentsPerToken = false, ArgumentHelpName = "script" };
var printOption = new Option<string>("-p", "Parse a single-line script and output a human-readable version of the AST") { ArgumentHelpName = "script" };
rootCommand.AddOption(evalOption);
rootCommand.AddOption(printOption);
var scriptNameArgument = new Argument<string>
{
Name = "script-name",
Arity = ArgumentArity.ZeroOrOne,
};
rootCommand.AddArgument(scriptNameArgument);
rootCommand.AddValidator(result =>
{
if (result.HasOption(evalOption) && result.HasOption(printOption))
{
return "Error: the -e and -p options are mutually exclusive";
}
if (result.HasOption(evalOption) && result.HasArgument(scriptNameArgument))
{
return "Error: the -e option cannot be combined with the <script-name> argument";
}
return null;
}); |
This has been hanging for too long, and it's unlikely that it'll get merged as-is. I'll close it now, and instead try to figure out where (Side note, but it would be incredibly nice with some form of release notes/changelog where a list of breaking changes like this can be tracked. I looked but couldn't find anything to the like; if it exists, please point me to it. 👍) Edit: https://github.com/dotnet/command-line-api/issues?q=label%3A%22Breaking+Change%22+milestone%3A%222.0+GA%22+ actually has some of this. #1537 is also useful. 🙏 |
Ah, #1538 has me covered. 🎉 |
This adds extension methods similar in spirit to the ones already present in
ParseResultExtensions
. I added these to my local project and decided to upstream them in the hope that they can be useful to others also. 🙏The new methods don't have any tests unfortunately, but it could probably easily be added. Feel free to suggest an existing test class/method where tests for these would fit in well and I'll happily add one or more tests for them.