Skip to content

Commit e995227

Browse files
committed
make TrimOuterQuotes() an extension method
1 parent aff5703 commit e995227

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

src/Utility.CommandLine.Arguments/Arguments.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ internal static Type GetCallingType(string caller)
9696

9797
return callingMethod.DeclaringType;
9898
}
99+
100+
internal static string TrimOuterQuotes(this string value)
101+
{
102+
if (value.StartsWith("\"") && value.EndsWith("\""))
103+
{
104+
value = value.Trim('"');
105+
}
106+
else if (value.StartsWith("'") && value.EndsWith("'"))
107+
{
108+
value = value.Trim('\'');
109+
}
110+
111+
return value;
112+
}
99113
}
100114

101115
/// <summary>
@@ -554,7 +568,7 @@ private static List<KeyValuePair<string, string>> GetArgumentList(string command
554568
string argument = match.Groups[1].Value;
555569
string value = match.Groups[2].Value;
556570

557-
value = TrimOuterQuotes(value);
571+
value = value.TrimOuterQuotes();
558572

559573
// check to see if the argument uses a single dash. if so, split the argument name into a char array and add each
560574
// to the list. if a value is specified, it belongs to the final character.
@@ -618,7 +632,7 @@ private static List<string> GetOperandList(string commandLineString)
618632
string fullMatch = match.Groups[0].Value;
619633
string operand = match.Groups[3].Value;
620634

621-
operands.Add(TrimOuterQuotes(operand));
635+
operands.Add(operand.TrimOuterQuotes());
622636
}
623637

624638
return operands;
@@ -650,20 +664,6 @@ private static PropertyInfo GetOperandsProperty(Type type)
650664

651665
return property;
652666
}
653-
654-
private static string TrimOuterQuotes(string value)
655-
{
656-
if (value.StartsWith("\"") && value.EndsWith("\""))
657-
{
658-
value = value.Trim('"');
659-
}
660-
else if (value.StartsWith("'") && value.EndsWith("'"))
661-
{
662-
value = value.Trim('\'');
663-
}
664-
665-
return value;
666-
}
667667
}
668668

669669
/// <summary>

tests/Utility.CommandLine.Arguments.Tests/ArgumentsTests.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,19 @@ public void PopulateMultipleValuesNotCollectionBacked()
464464
{
465465
Exception ex = Record.Exception(() => CommandLine.Arguments.Populate(GetType(), "--integer 1 --integer 2"));
466466

467-
Assert.NotNull(ex);
468-
Assert.IsType<InvalidCastException>(ex);
467+
Assert.Null(ex);
468+
469+
Assert.Equal(2, Integer);
470+
}
471+
472+
[Fact]
473+
public void PopulateMultipleValuesNotCollectionBacked_Last_Value_Wins()
474+
{
475+
Exception ex = Record.Exception(() => CommandLine.Arguments.Populate(GetType(), "--integer 2 --integer 1"));
476+
477+
Assert.Null(ex);
478+
479+
Assert.Equal(1, Integer);
469480
}
470481

471482
[Fact]
@@ -851,7 +862,7 @@ public void Value_Is_Replaced_Given_Multiple_Short()
851862

852863
Assert.Single(dict);
853864

854-
Assert.Equal(2, dict["b"]);
865+
Assert.Equal("2", dict["b"]);
855866
}
856867

857868
[Fact]
@@ -866,7 +877,7 @@ public void Value_Is_Replaced_Given_Multiple_Long()
866877

867878
Assert.Single(dict);
868879

869-
Assert.Equal(2, dict["bb"]);
880+
Assert.Equal("2", dict["bb"]);
870881
}
871882

872883
[Fact]
@@ -881,7 +892,7 @@ public void Value_Is_Replaced_Given_Mixed_Args_Long_First()
881892

882893
Assert.Single(dict);
883894

884-
Assert.Equal(2, dict["bb"]);
895+
Assert.Equal("2", dict["bb"]);
885896
}
886897

887898
[Fact]
@@ -896,7 +907,7 @@ public void Value_Is_Replaced_Given_Mixed_Args_Short_First()
896907

897908
Assert.Single(dict);
898909

899-
Assert.Equal(2, dict["b"]);
910+
Assert.Equal("2", dict["b"]);
900911
}
901912

902913
[Fact]
@@ -912,8 +923,8 @@ public void Value_Is_Not_Replaced_Given_Mixed_Args_Short_First_No_Type()
912923
Assert.Equal(2, dict.Count);
913924
Assert.True(dict.ContainsKey("b"));
914925
Assert.True(dict.ContainsKey("bb"));
915-
Assert.Equal(1, dict["b"]);
916-
Assert.Equal(2, dict["bb"]);
926+
Assert.Equal("1", dict["b"]);
927+
Assert.Equal("2", dict["bb"]);
917928
}
918929
}
919930

0 commit comments

Comments
 (0)