Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit be80664

Browse files
committed
Fix for no value if [Option] arg equals to [Value] arg
Make comparisons by reference in TokenPartitioner.
1 parent e8cf96b commit be80664

File tree

7 files changed

+87
-4
lines changed

7 files changed

+87
-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,ReferenceEqualityComparer.Default).Cast<Token>().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 x == y; // reference equality because operator== is static and resolved at compile-time
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_Uri_And_SimpleType.cs" />
6566
<Compile Include="Fakes\Options_With_Switches.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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,5 +803,35 @@ public class NullDefaultCommandLineArguments
803803
[Option('u', "user", Default = null)]
804804
public string User { get; set; }
805805
}
806+
807+
[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+
}
806836
}
807837
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /