-
Notifications
You must be signed in to change notification settings - Fork 459
Simplify command and parse result handling #2261
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
Merged
alzimmermsft
merged 8 commits into
microsoft:main
from
alzimmermsft:ChangeCommandParsingExtensions
Apr 17, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
390bf31
Small PoC on simplifying command and parse result handling
alzimmermsft 40868e5
Merge branch 'main' into ChangeCommandParsingExtensions
alzimmermsft 8eddcf2
PR feedback
alzimmermsft e952a40
Test updates
alzimmermsft e81fab0
Add more tests
alzimmermsft b4b9aba
Merge branch 'main' into ChangeCommandParsingExtensions
alzimmermsft df91f90
Merge branch 'main' into ChangeCommandParsingExtensions
alzimmermsft 7b9be56
PR feedback
alzimmermsft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
535 changes: 218 additions & 317 deletions
535
.../Azure.Mcp.Core/tests/Azure.Mcp.Core.UnitTests/Extensions/CommandResultExtensionsTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 9 additions & 64 deletions
73
core/Microsoft.Mcp.Core/src/Extensions/ParseResultExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,86 +1,31 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.CommandLine.Parsing; | ||
| using Microsoft.Mcp.Core.Helpers; | ||
| using Microsoft.Mcp.Core.Models.Option; | ||
|
|
||
| namespace Microsoft.Mcp.Core.Extensions; | ||
|
|
||
| public static class ParseResultExtensions | ||
| { | ||
| public static bool TryGetValue<T>(this ParseResult parseResult, Option<T> option, out T? value) | ||
| => parseResult.CommandResult.TryGetValue(option, out value); | ||
| => TryGetValue(parseResult, option.Name, out value); | ||
|
|
||
|
alzimmermsft marked this conversation as resolved.
|
||
| public static bool TryGetValue<T>(this ParseResult parseResult, string optionName, out T? value) | ||
| { | ||
| // Find the option by name in the command | ||
| var command = parseResult.CommandResult.Command; | ||
| var option = command.Options.OfType<Option<T>>() | ||
| .FirstOrDefault(o => o.Name == optionName || o.Aliases.Contains(optionName)); | ||
|
|
||
| if (option != null) | ||
| { | ||
| return parseResult.CommandResult.TryGetValue(option, out value); | ||
| } | ||
|
|
||
| value = default; | ||
| return false; | ||
| } | ||
| => parseResult.CommandResult.TryGetValue(optionName, out value); | ||
|
|
||
| public static T? GetValueOrDefault<T>(this ParseResult parseResult, Option<T> option) | ||
| => parseResult.CommandResult.GetValueOrDefault(option); | ||
| => GetValueOrDefault<T>(parseResult, option.Name); | ||
|
|
||
| /// <summary> | ||
| /// Gets the value of an option by name, returning default if not found or not set | ||
| /// </summary> | ||
| public static T? GetValueOrDefault<T>(this ParseResult parseResult, string optionName) | ||
| { | ||
| // Find the option by name in the command | ||
| var command = parseResult.CommandResult.Command; | ||
| var option = command.Options.OfType<Option<T>>() | ||
| .FirstOrDefault(o => o.Name == optionName || o.Aliases.Contains(optionName)); | ||
|
|
||
| return option != null ? parseResult.GetValueOrDefault(option) : default; | ||
| } | ||
| => parseResult.CommandResult.GetValueOrDefault<T>(optionName); | ||
|
|
||
| public static bool HasAnyRetryOptions(this ParseResult parseResult) | ||
| { | ||
| foreach (var child in parseResult.CommandResult.Children) | ||
| { | ||
| if (child is OptionResult optionResult) | ||
| { | ||
| var option = optionResult.Option; | ||
| if (option is null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var name = NameNormalization.NormalizeOptionName(option.Name); | ||
| if (RetryOptionNames.Contains(name)) | ||
| return true; | ||
|
|
||
| var aliases = option.Aliases ?? []; | ||
| foreach (var alias in aliases) | ||
| { | ||
| var normalized = NameNormalization.NormalizeOptionName(alias); | ||
| if (RetryOptionNames.Contains(normalized)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static readonly HashSet<string> RetryOptionNames = new(StringComparer.OrdinalIgnoreCase) | ||
| { | ||
| NameNormalization.NormalizeOptionName(OptionDefinitions.RetryPolicy.DelayName), | ||
| NameNormalization.NormalizeOptionName(OptionDefinitions.RetryPolicy.MaxDelayName), | ||
| NameNormalization.NormalizeOptionName(OptionDefinitions.RetryPolicy.MaxRetriesName), | ||
| NameNormalization.NormalizeOptionName(OptionDefinitions.RetryPolicy.ModeName), | ||
| NameNormalization.NormalizeOptionName(OptionDefinitions.RetryPolicy.NetworkTimeoutName), | ||
| }; | ||
| => parseResult.CommandResult.HasOptionResult(OptionDefinitions.RetryPolicy.Delay) || | ||
| parseResult.CommandResult.HasOptionResult(OptionDefinitions.RetryPolicy.MaxDelay) || | ||
| parseResult.CommandResult.HasOptionResult(OptionDefinitions.RetryPolicy.MaxRetries) || | ||
| parseResult.CommandResult.HasOptionResult(OptionDefinitions.RetryPolicy.Mode) || | ||
| parseResult.CommandResult.HasOptionResult(OptionDefinitions.RetryPolicy.NetworkTimeout); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.