Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CommandLine/CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="Infrastructure\ExceptionExtensions.cs" />
<Compile Include="Infrastructure\FSharpOptionHelper.cs" />
<Compile Include="Infrastructure\PopsicleSetter.cs" />
<Compile Include="Infrastructure\ReferenceEqualityComparer.cs" />
<Compile Include="Infrastructure\ReflectionHelper.cs" />
<Compile Include="Infrastructure\ResultExtensions.cs" />
<Compile Include="Infrastructure\StringExtensions.cs" />
Expand Down
9 changes: 5 additions & 4 deletions src/CommandLine/Core/TokenPartitioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLine.Infrastructure;
using CSharpx;

namespace CommandLine.Core
Expand All @@ -23,11 +24,11 @@ public static
var scalars = Scalar.Partition(tokenList, typeLookup).Memorize();
var sequences = Sequence.Partition(tokenList, typeLookup).Memorize();
var nonOptions = tokenList
.Where(t => !switches.Contains(t))
.Where(t => !scalars.Contains(t))
.Where(t => !sequences.Contains(t)).Memorize();
.Where(t => !switches.Contains(t, ReferenceEqualityComparer.Default))
.Where(t => !scalars.Contains(t, ReferenceEqualityComparer.Default))
.Where(t => !sequences.Contains(t, ReferenceEqualityComparer.Default)).Memorize();
var values = nonOptions.Where(v => v.IsValue()).Memorize();
var errors = nonOptions.Except(values).Memorize();
var errors = nonOptions.Except(values, (IEqualityComparer<Token>)ReferenceEqualityComparer.Default).Memorize();

return Tuple.Create(
KeyValuePairHelper.ForSwitch(switches)
Expand Down
24 changes: 24 additions & 0 deletions src/CommandLine/Infrastructure/ReferenceEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace CommandLine.Infrastructure
{
internal sealed class ReferenceEqualityComparer : IEqualityComparer, IEqualityComparer<object>
{
public static readonly ReferenceEqualityComparer Default = new ReferenceEqualityComparer();

public new bool Equals(object x, object y)
{
return ReferenceEquals(x, y);
}

public int GetHashCode(object obj)
{
return RuntimeHelpers.GetHashCode(obj);
}
}
}
1 change: 1 addition & 0 deletions tests/CommandLine.Tests/CommandLine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="CultureInfoExtensions.cs" />
<Compile Include="Fakes\Options_With_Default_Set_To_Sequence.cs" />
<Compile Include="Fakes\Options_With_Guid.cs" />
<Compile Include="Fakes\Options_With_Option_And_Value_Of_String_Type.cs" />
<Compile Include="Fakes\Options_With_SetName_That_Ends_With_Previous_SetName.cs" />
<Compile Include="Fakes\Options_With_Shuffled_Index_Values.cs" />
<Compile Include="Fakes\Options_With_Uri_And_SimpleType.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System;
using System.Linq;

namespace CommandLine.Tests.Fakes
{
class Options_With_Option_And_Value_Of_String_Type
{
[Option('o', "opt")]
public string OptValue { get; set; }

[Value(0)]
public string PosValue { get; set; }
}
}
10 changes: 10 additions & 0 deletions tests/CommandLine.Tests/Fakes/Verb_Fakes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,14 @@ class Derived_Verb : Base_Class_For_Verb
HelpText = "Allow adding otherwise ignored files.")]
public bool Force { get; set; }
}

[Verb("test")]
class Verb_With_Option_And_Value_Of_String_Type
{
[Option('o', "opt")]
public string OptValue { get; set; }

[Value(0)]
public string PosValue { get; set; }
}
}
29 changes: 29 additions & 0 deletions tests/CommandLine.Tests/Unit/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,35 @@ public class NullDefaultCommandLineArguments
}

[Fact]
public void Parse_options_with_same_option_and_value_args()
{
var parser = Parser.Default;
parser.ParseArguments<Options_With_Option_And_Value_Of_String_Type>(
new[] { "arg", "-o", "arg" })
.WithNotParsed(errors => { throw new InvalidOperationException("Must be parsed."); })
.WithParsed(args =>
{
Assert.Equal("arg", args.OptValue);
Assert.Equal("arg", args.PosValue);
});
}

[Fact]
public void Parse_verb_with_same_option_and_value_args()
{
var parser = Parser.Default;
var result = parser.ParseArguments(
new[] { "test", "arg", "-o", "arg" },
typeof(Verb_With_Option_And_Value_Of_String_Type));
result
.WithNotParsed(errors => { throw new InvalidOperationException("Must be parsed."); })
.WithParsed<Verb_With_Option_And_Value_Of_String_Type>(args =>
{
Assert.Equal("arg", args.OptValue);
Assert.Equal("arg", args.PosValue);
});
}

public void Parse_options_with_shuffled_index_values()
{
var parser = Parser.Default;
Expand Down