Skip to content
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

Fix unknown command throws System.InvalidOperationException: Enumeration already finished #542

Merged
merged 2 commits into from Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/CommandLineUtils/Internal/CommandLineProcessor.cs
Expand Up @@ -519,10 +519,12 @@ public void Dispose()
private sealed class CommandArgumentEnumerator : IEnumerator<CommandArgument>
{
private readonly IEnumerator<CommandArgument> _enumerator;
private bool _currentValid;

public CommandArgumentEnumerator(IEnumerator<CommandArgument> enumerator)
{
_enumerator = enumerator;
_currentValid = false;
}

public CommandArgument Current => _enumerator.Current;
Expand All @@ -533,9 +535,10 @@ public CommandArgumentEnumerator(IEnumerator<CommandArgument> enumerator)

public bool MoveNext()
{
if (Current == null || !Current.MultipleValues)
if (!_currentValid || !Current.MultipleValues)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current is not nullable so I removed the null check. I think it was only used to check if MoveNext had been called. Not to allow Current to be null while iterating. Please let me know if I should add it again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also better because it not only checks that MoveNext has been called, but also that it returned true. The error was caused by calling Current after MoveNext has returned false.

{
return _enumerator.MoveNext();
_currentValid = _enumerator.MoveNext();
return _currentValid;
}

// If current argument allows multiple values, we don't move forward and
Expand Down
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(TestFullFramework)' != 'false'">$(TargetFrameworks);net472</TargetFrameworks>
<!-- Once xunit supports nullable reference types, I might reconsider -->
<Nullable>annotations</Nullable>
Expand Down
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(TestFullFramework)' != 'false'">$(TargetFrameworks);net472</TargetFrameworks>
</PropertyGroup>

Expand Down