-
Notifications
You must be signed in to change notification settings - Fork 407
Update System.CommandLine #4022
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
Open
jonsequitur
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
jonsequitur:update-System.CommandLine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
10 changes: 9 additions & 1 deletion
10
...t.DotNet.Interactive.Http.Tests/Microsoft.DotNet.Interactive.Http.Tests.v3.ncrunchproject
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,5 +1,13 @@ | ||
<ProjectConfiguration> | ||
<ProjectConfiguration> | ||
<Settings> | ||
<DefaultTestTimeout>20000</DefaultTestTimeout> | ||
<IgnoredTests> | ||
<NamedTestSelector> | ||
<TestName>Microsoft.DotNet.Interactive.Http.Tests.HttpKernelTests+NamedRequest.body_content_produces_the_entirety_of_the_body_content("example.response.body.*")</TestName> | ||
</NamedTestSelector> | ||
<NamedTestSelector> | ||
<TestName>Microsoft.DotNet.Interactive.Http.Tests.HttpKernelTests.traceparent_header_has_a_new_top_level_value_for_each_request</TestName> | ||
</NamedTestSelector> | ||
</IgnoredTests> | ||
</Settings> | ||
</ProjectConfiguration> |
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
76 changes: 76 additions & 0 deletions
76
src/dotnet-interactive.Tests/CommandLine/CommandExtensions.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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Linq; | ||
using Microsoft.DotNet.Interactive.App.Tests.Extensions; | ||
|
||
namespace Microsoft.DotNet.Interactive.App.Tests.CommandLine; | ||
|
||
public static class CommandExtensions | ||
{ | ||
/// <summary> | ||
/// Throws an exception if the parser configuration is ambiguous or otherwise not valid. | ||
/// </summary> | ||
/// <remarks>Due to the performance cost of this method, it is recommended to be used in unit testing or in scenarios where the parser is configured dynamically at runtime.</remarks> | ||
/// <exception cref="InvalidOperationException">Thrown if the configuration is found to be invalid.</exception> | ||
public static void ThrowIfInvalid(this Command command) | ||
{ | ||
if (command.Parents.FlattenBreadthFirst(c => c.Parents).Any(ancestor => ancestor == command)) | ||
{ | ||
throw new InvalidOperationException($"Cycle detected in command tree. Command '{command.Name}' is its own ancestor."); | ||
} | ||
|
||
int count = command.Subcommands.Count + command.Options.Count; | ||
for (var i = 0; i < count; i++) | ||
{ | ||
Symbol symbol1 = GetChild(i, command, out HashSet<string> aliases1); | ||
|
||
for (var j = i + 1; j < count; j++) | ||
{ | ||
Symbol symbol2 = GetChild(j, command, out HashSet<string> aliases2); | ||
|
||
if (symbol1.Name.Equals(symbol2.Name, StringComparison.Ordinal) | ||
|| aliases1 is not null && aliases1.Contains(symbol2.Name)) | ||
{ | ||
throw new InvalidOperationException($"Duplicate alias '{symbol2.Name}' found on command '{command.Name}'."); | ||
} | ||
else if (aliases2 is not null && aliases2.Contains(symbol1.Name)) | ||
{ | ||
throw new InvalidOperationException($"Duplicate alias '{symbol1.Name}' found on command '{command.Name}'."); | ||
} | ||
|
||
if (aliases1 is not null && aliases2 is not null) | ||
{ | ||
// take advantage of the fact that we are dealing with two hash sets | ||
if (aliases1.Overlaps(aliases2)) | ||
{ | ||
foreach (string symbol2Alias in aliases2) | ||
{ | ||
if (aliases1.Contains(symbol2Alias)) | ||
{ | ||
throw new InvalidOperationException($"Duplicate alias '{symbol2Alias}' found on command '{command.Name}'."); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (symbol1 is Command childCommand) | ||
{ | ||
childCommand.ThrowIfInvalid(); | ||
} | ||
} | ||
|
||
static Symbol GetChild(int index, Command command, out HashSet<string> aliases) | ||
{ | ||
if (index < command.Subcommands.Count) | ||
{ | ||
aliases = command.Subcommands[index].Aliases.ToHashSet(); | ||
return command.Subcommands[index]; | ||
} | ||
|
||
aliases = command.Options[index - command.Subcommands.Count].Aliases.ToHashSet(); | ||
return command.Options[index - command.Subcommands.Count]; | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting - I remember us removing all System.CommandLine dependencies (when you made the parser changes) but don't recall when we added it back...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
System.CommandLine had been used for parsing magic commands. That code was removed and a new, more fit-for-purpose parser was written. System.CommandLine was never removed for actual command line parsing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad - I thought it had been removed as part of the push to get all the interactive pieces to GA.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was the driver, but also the magic command parser needed improvements that couldn't be implemented with System.CommandLine, while System.CommandLine was also having features removed to prepare it for GA.