Skip to content

Commit

Permalink
Fix unknown command throws System.InvalidOperationException: Enumerat…
Browse files Browse the repository at this point in the history
…ion already finished (#542)

* Update tests to run .NET 8
* Don't call enumerator.Current if MoveNext is not called or returns false in CommandArgumentEnumerator.

---------

Co-authored-by: Adam Sällergård <adam.sallergard@griffeye.com>
  • Loading branch information
sallerga and Adam Sällergård committed Feb 19, 2024
1 parent 14f9a49 commit e94c9fe
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/CommandLineUtils/Internal/CommandLineProcessor.cs
Original file line number Diff line number Diff line change
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)
{
return _enumerator.MoveNext();
_currentValid = _enumerator.MoveNext();
return _currentValid;
}

// If current argument allows multiple values, we don't move forward and
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e94c9fe

Please sign in to comment.