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
<CompileInclude="Infrastructure\ExceptionExtensions.cs" />
8585
<CompileInclude="Infrastructure\FSharpOptionHelper.cs" />
8686
<CompileInclude="Infrastructure\PopsicleSetter.cs" />
87+
<CompileInclude="Infrastructure\ReferenceEqualityComparer.cs" />
8788
<CompileInclude="Infrastructure\ReflectionHelper.cs" />
8889
<CompileInclude="Infrastructure\ResultExtensions.cs" />
8990
<CompileInclude="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
usingSystem;
44
usingSystem.Collections.Generic;
55
usingSystem.Linq;
6+
usingCommandLine.Infrastructure;
67
usingCSharpx;
78

89
namespaceCommandLine.Core
@@ -23,11 +24,11 @@ public static
2324
varscalars=Scalar.Partition(tokenList,typeLookup).Memorize();
2425
varsequences=Sequence.Partition(tokenList,typeLookup).Memorize();
2526
varnonOptions=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
varvalues=nonOptions.Where(v =>v.IsValue()).Memorize();
30-
varerrors=nonOptions.Except(values).Memorize();
31+
varerrors=nonOptions.Except(values,(IEqualityComparer<Token>)ReferenceEqualityComparer.Default).Memorize();
3132

3233
returnTuple.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+
usingSystem;
4+
usingSystem.Collections;
5+
usingSystem.Collections.Generic;
6+
usingSystem.Runtime.CompilerServices;
7+
8+
namespaceCommandLine.Infrastructure
9+
{
10+
internalsealedclassReferenceEqualityComparer:IEqualityComparer,IEqualityComparer<object>
11+
{
12+
publicstaticreadonlyReferenceEqualityComparerDefault=newReferenceEqualityComparer();
13+
14+
publicnewboolEquals(objectx,objecty)
15+
{
16+
returnReferenceEquals(x,y);
17+
}
18+
19+
publicintGetHashCode(objectobj)
20+
{
21+
returnRuntimeHelpers.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
<CompileInclude="CultureInfoExtensions.cs" />
6161
<CompileInclude="Fakes\Options_With_Default_Set_To_Sequence.cs" />
6262
<CompileInclude="Fakes\Options_With_Guid.cs" />
63+
<CompileInclude="Fakes\Options_With_Option_And_Value_Of_String_Type.cs" />
6364
<CompileInclude="Fakes\Options_With_SetName_That_Ends_With_Previous_SetName.cs" />
6465
<CompileInclude="Fakes\Options_With_Shuffled_Index_Values.cs" />
6566
<CompileInclude="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+
usingSystem;
4+
usingSystem.Linq;
5+
6+
namespaceCommandLine.Tests.Fakes
7+
{
8+
classOptions_With_Option_And_Value_Of_String_Type
9+
{
10+
[Option('o',"opt")]
11+
publicstringOptValue{get;set;}
12+
13+
[Value(0)]
14+
publicstringPosValue{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
publicboolForce{get;set;}
7777
}
78+
79+
[Verb("test")]
80+
classVerb_With_Option_And_Value_Of_String_Type
81+
{
82+
[Option('o',"opt")]
83+
publicstringOptValue{get;set;}
84+
85+
[Value(0)]
86+
publicstringPosValue{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+
publicvoidParse_options_with_same_option_and_value_args()
809+
{
810+
varparser=Parser.Default;
811+
parser.ParseArguments<Options_With_Option_And_Value_Of_String_Type>(
812+
new[]{"arg","-o","arg"})
813+
.WithNotParsed(errors =>{thrownewInvalidOperationException("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+
publicvoidParse_verb_with_same_option_and_value_args()
823+
{
824+
varparser=Parser.Default;
825+
varresult=parser.ParseArguments(
826+
new[]{"test","arg","-o","arg"},
827+
typeof(Verb_With_Option_And_Value_Of_String_Type));
828+
result
829+
.WithNotParsed(errors =>{thrownewInvalidOperationException("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
publicvoidParse_options_with_shuffled_index_values()
809838
{
810839
varparser=Parser.Default;

0 commit comments

Comments
(0)