Skip to content

Commit 27243c4

Browse files
authored
Merge pull request #401 from zii-dmg/same-value-option-arg
Fix for no value if [Option] arg equals to [Value] arg
2 parents 1cca1ac + f2c7ab9 commit 27243c4

File tree

7 files changed

+86
-4
lines changed

7 files changed

+86
-4
lines changed

src/CommandLine/CommandLine.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="Infrastructure\ExceptionExtensions.cs" />
8585
<Compile Include="Infrastructure\FSharpOptionHelper.cs" />
8686
<Compile Include="Infrastructure\PopsicleSetter.cs" />
87+
<Compile Include="Infrastructure\ReferenceEqualityComparer.cs" />
8788
<Compile Include="Infrastructure\ReflectionHelper.cs" />
8889
<Compile Include="Infrastructure\ResultExtensions.cs" />
8990
<Compile Include="Infrastructure\StringExtensions.cs" />

src/CommandLine/Core/TokenPartitioner.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using CommandLine.Infrastructure;
67
using CSharpx;
78

89
namespace CommandLine.Core
@@ -23,11 +24,11 @@ public static
2324
var scalars = Scalar.Partition(tokenList, typeLookup).Memorize();
2425
var sequences = Sequence.Partition(tokenList, typeLookup).Memorize();
2526
var nonOptions = tokenList
26-
.Where(t => !switches.Contains(t))
27-
.Where(t => !scalars.Contains(t))
28-
.Where(t => !sequences.Contains(t)).Memorize();
27+
.Where(t => !switches.Contains(t, ReferenceEqualityComparer.Default))
28+
.Where(t => !scalars.Contains(t, ReferenceEqualityComparer.Default))
29+
.Where(t => !sequences.Contains(t, ReferenceEqualityComparer.Default)).Memorize();
2930
var values = nonOptions.Where(v => v.IsValue()).Memorize();
30-
var errors = nonOptions.Except(values).Memorize();
31+
var errors = nonOptions.Except(values, (IEqualityComparer<Token>)ReferenceEqualityComparer.Default).Memorize();
3132

3233
return Tuple.Create(
3334
KeyValuePairHelper.ForSwitch(switches)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2+
3+
using System;
4+
using System.Collections;
5+
using System.Collections.Generic;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace CommandLine.Infrastructure
9+
{
10+
internal sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer<object>
11+
{
12+
public static readonly ReferenceEqualityComparer Default = new ReferenceEqualityComparer();
13+
14+
public new bool Equals(object x, object y)
15+
{
16+
return ReferenceEquals(x, y);
17+
}
18+
19+
public int GetHashCode(object obj)
20+
{
21+
return RuntimeHelpers.GetHashCode(obj);
22+
}
23+
}
24+
}

tests/CommandLine.Tests/CommandLine.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="CultureInfoExtensions.cs" />
6161
<Compile Include="Fakes\Options_With_Default_Set_To_Sequence.cs" />
6262
<Compile Include="Fakes\Options_With_Guid.cs" />
63+
<Compile Include="Fakes\Options_With_Option_And_Value_Of_String_Type.cs" />
6364
<Compile Include="Fakes\Options_With_SetName_That_Ends_With_Previous_SetName.cs" />
6465
<Compile Include="Fakes\Options_With_Shuffled_Index_Values.cs" />
6566
<Compile Include="Fakes\Options_With_Uri_And_SimpleType.cs" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2+
3+
using System;
4+
using System.Linq;
5+
6+
namespace CommandLine.Tests.Fakes
7+
{
8+
class Options_With_Option_And_Value_Of_String_Type
9+
{
10+
[Option('o', "opt")]
11+
public string OptValue { get; set; }
12+
13+
[Value(0)]
14+
public string PosValue { get; set; }
15+
}
16+
}

tests/CommandLine.Tests/Fakes/Verb_Fakes.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,14 @@ class Derived_Verb : Base_Class_For_Verb
7575
HelpText = "Allow adding otherwise ignored files.")]
7676
public bool Force { get; set; }
7777
}
78+
79+
[Verb("test")]
80+
class Verb_With_Option_And_Value_Of_String_Type
81+
{
82+
[Option('o', "opt")]
83+
public string OptValue { get; set; }
84+
85+
[Value(0)]
86+
public string PosValue { get; set; }
87+
}
7888
}

tests/CommandLine.Tests/Unit/ParserTests.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,35 @@ public class NullDefaultCommandLineArguments
805805
}
806806

807807
[Fact]
808+
public void Parse_options_with_same_option_and_value_args()
809+
{
810+
var parser = Parser.Default;
811+
parser.ParseArguments<Options_With_Option_And_Value_Of_String_Type>(
812+
new[] { "arg", "-o", "arg" })
813+
.WithNotParsed(errors => { throw new InvalidOperationException("Must be parsed."); })
814+
.WithParsed(args =>
815+
{
816+
Assert.Equal("arg", args.OptValue);
817+
Assert.Equal("arg", args.PosValue);
818+
});
819+
}
820+
821+
[Fact]
822+
public void Parse_verb_with_same_option_and_value_args()
823+
{
824+
var parser = Parser.Default;
825+
var result = parser.ParseArguments(
826+
new[] { "test", "arg", "-o", "arg" },
827+
typeof(Verb_With_Option_And_Value_Of_String_Type));
828+
result
829+
.WithNotParsed(errors => { throw new InvalidOperationException("Must be parsed."); })
830+
.WithParsed<Verb_With_Option_And_Value_Of_String_Type>(args =>
831+
{
832+
Assert.Equal("arg", args.OptValue);
833+
Assert.Equal("arg", args.PosValue);
834+
});
835+
}
836+
808837
public void Parse_options_with_shuffled_index_values()
809838
{
810839
var parser = Parser.Default;

0 commit comments

Comments
 (0)