Skip to content

Commit 6f852c0

Browse files
committed
make sure help respects case-sensitive/ignore case
1 parent 16d0f45 commit 6f852c0

File tree

7 files changed

+69
-12
lines changed

7 files changed

+69
-12
lines changed

FluentCommandLineParser.Tests/FluentCommandLineParserMSpecTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ class FluentCommandLineParserMSpecTests
88
[Subject(typeof(Fclp.FluentCommandLineParser))]
99
abstract class FluentCommandLineParserTestContext : TestContextBase<Fclp.FluentCommandLineParser>
1010
{
11-
Establish context = () =>
12-
CreateSut();
11+
Establish context = () => CreateSut();
1312
}
1413

1514
sealed class IsCaseSensitive

FluentCommandLineParser.Tests/FluentCommandLineParserTests.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,8 @@ public void Setup_Help_And_Ensure_It_Is_Called_With_Custom_Formatter()
641641
{
642642
var parser = new Fclp.FluentCommandLineParser();
643643

644+
parser.IsCaseSensitive = false;
645+
644646
var formatter = new Mock<ICommandLineOptionFormatter>();
645647

646648
var args = new[] { "/help", "i", "s" };
@@ -668,6 +670,8 @@ public void Setup_Help_And_Ensure_It_Is_Called()
668670
{
669671
var parser = new Fclp.FluentCommandLineParser();
670672

673+
parser.IsCaseSensitive = false;
674+
671675
var formatter = new Mock<ICommandLineOptionFormatter>();
672676

673677
var args = new[] { "/help", "i", "s" };
@@ -694,10 +698,12 @@ public void Setup_Help_And_Ensure_It_Is_Called()
694698
#region Case Sensitive
695699

696700
[Test]
697-
public void Ensure_Short_Options_Are_Case_Sensitive()
701+
public void Ensure_Short_Options_Are_Case_Sensitive_When_Enabled()
698702
{
699703
var parser = CreateFluentParser();
700704

705+
parser.IsCaseSensitive = true;
706+
701707
const string expectedUpperCaseValue = "UPPERCASE VALUE";
702708
const string expectedLowerCaseValue = "LOWERCASE VALUE";
703709

@@ -714,12 +720,13 @@ public void Ensure_Short_Options_Are_Case_Sensitive()
714720
Assert.AreEqual(expectedLowerCaseValue, lowerCaseValue);
715721
}
716722

717-
718723
[Test]
719-
public void Ensure_Long_Options_Are_Case_Sensitive()
724+
public void Ensure_Long_Options_Are_Case_Sensitive_When_Enabled()
720725
{
721726
var parser = CreateFluentParser();
722727

728+
parser.IsCaseSensitive = true;
729+
723730
const string expectedUpperCaseValue = "UPPERCASE VALUE";
724731
const string expectedLowerCaseValue = "LOWERCASE VALUE";
725732

@@ -736,6 +743,44 @@ public void Ensure_Long_Options_Are_Case_Sensitive()
736743
Assert.AreEqual(expectedLowerCaseValue, lowerCaseValue);
737744
}
738745

746+
[Test]
747+
public void Ensure_Short_Options_Ignore_Case_When_Disabled()
748+
{
749+
var parser = CreateFluentParser();
750+
751+
parser.IsCaseSensitive = false;
752+
753+
const string expectedValue = "expected value";
754+
755+
string actualValue = null;
756+
757+
parser.Setup<string>('s').Callback(str => actualValue = str).Required();
758+
759+
var result = parser.Parse(new[] { "--S", expectedValue });
760+
761+
Assert.IsFalse(result.HasErrors);
762+
Assert.AreEqual(expectedValue, actualValue);
763+
}
764+
765+
[Test]
766+
public void Ensure_Long_Options_Ignore_Case_When_Disabled()
767+
{
768+
var parser = CreateFluentParser();
769+
770+
parser.IsCaseSensitive = false;
771+
772+
const string expectedValue = "expected value";
773+
774+
string actualValue = null;
775+
776+
parser.Setup<string>("longoption").Callback(str => actualValue = str).Required();
777+
778+
var result = parser.Parse(new[] { "--LONGOPTION", expectedValue });
779+
780+
Assert.IsFalse(result.HasErrors);
781+
Assert.AreEqual(expectedValue, actualValue);
782+
}
783+
739784
#endregion
740785

741786
#region Obsolete

FluentCommandLineParser.Tests/Internals/HelpCommandLineOptionTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// POSSIBILITY OF SUCH DAMAGE.
2323
#endregion
2424

25+
using System;
2526
using System.Collections.Generic;
2627
using Fclp.Internals;
2728
using Machine.Specifications;
@@ -42,8 +43,12 @@ abstract class ShouldShowHelpTestContext : HelpCommandLineOptionTestContext
4243
{
4344
protected static bool actualResult;
4445
protected static IEnumerable<ParsedOption> parsedOptions;
46+
protected static StringComparison comparisonType;
4547

46-
Because of = () => actualResult = sut.ShouldShowHelp(parsedOptions);
48+
Establish context = () =>
49+
comparisonType = StringComparison.CurrentCulture;
50+
51+
Because of = () => actualResult = sut.ShouldShowHelp(parsedOptions, comparisonType);
4752
}
4853

4954
class when_the_args_are_empty_and_the_option_is_setup_to_handle_empty_args_like_help_args : ShouldShowHelpTestContext

FluentCommandLineParser/FluentCommandLineParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public ICommandLineParserResult Parse(string[] args)
216216

217217
var result = new CommandLineParserResult { EmptyArgs = parsedOptions.IsNullOrEmpty() };
218218

219-
if (this.HelpOption.ShouldShowHelp(parsedOptions))
219+
if (this.HelpOption.ShouldShowHelp(parsedOptions, StringComparison))
220220
{
221221
result.HelpCalled = true;
222222
this.HelpOption.ShowHelp(this.Options);

FluentCommandLineParser/Internals/EmptyHelpCommandLineOption.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ public class EmptyHelpCommandLineOption : IHelpCommandLineOption
3535
/// <summary>
3636
/// Always returns false.
3737
/// </summary>
38-
public bool ShouldShowHelp(IEnumerable<ParsedOption> commandLineArgs)
38+
/// <param name="commandLineArgs">The command line args.</param>
39+
/// <param name="comparisonType">Type of the comparison.</param>
40+
/// <returns></returns>
41+
public bool ShouldShowHelp(IEnumerable<ParsedOption> commandLineArgs, StringComparison comparisonType)
3942
{
4043
return false;
4144
}

FluentCommandLineParser/Internals/HelpCommandLineOption.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,11 @@ public IHelpCommandLineOptionFluent UseForEmptyArgs()
136136
/// Determines whether the help text should be shown.
137137
/// </summary>
138138
/// <param name="parsedOptions">The parsed command line arguments</param>
139-
/// <returns>true if the parser operation should cease and <see cref="ShowHelp"/> should be called; otherwise false if the parse operation to continue.</returns>
140-
public bool ShouldShowHelp(IEnumerable<ParsedOption> parsedOptions)
139+
/// <param name="comparisonType">The type of comparison to use when comparing Option names.</param>
140+
/// <returns>
141+
/// true if the parser operation should cease and <see cref="ShowHelp" /> should be called; otherwise false if the parse operation to continue.
142+
/// </returns>
143+
public bool ShouldShowHelp(IEnumerable<ParsedOption> parsedOptions, StringComparison comparisonType)
141144
{
142145
var parsed = parsedOptions != null ? parsedOptions.ToList() : new List<ParsedOption>();
143146

@@ -146,7 +149,7 @@ public bool ShouldShowHelp(IEnumerable<ParsedOption> parsedOptions)
146149
return true;
147150
}
148151

149-
return this.HelpArgs.Any(helpArg => parsed.Any(cmdArg => helpArg.Equals(cmdArg.Key, StringComparison.CurrentCultureIgnoreCase)));
152+
return this.HelpArgs.Any(helpArg => parsed.Any(cmdArg => helpArg.Equals(cmdArg.Key, comparisonType)));
150153
}
151154

152155
/// <summary>

FluentCommandLineParser/Internals/IHelpCommandLineOption.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// POSSIBILITY OF SUCH DAMAGE.
2323
#endregion
2424

25+
using System;
2526
using System.Collections.Generic;
2627

2728
namespace Fclp.Internals
@@ -35,8 +36,9 @@ public interface IHelpCommandLineOption
3536
/// Determines whether the help text should be shown.
3637
/// </summary>
3738
/// <param name="parsedOptions">The parsed command line arguments</param>
39+
/// <param name="comparisonType">The type of comparison to use when comparing Option names.</param>
3840
/// <returns>true if the parser operation should cease and <see cref="ShowHelp"/> should be called; otherwise false if the parse operation to continue.</returns>
39-
bool ShouldShowHelp(IEnumerable<ParsedOption> parsedOptions);
41+
bool ShouldShowHelp(IEnumerable<ParsedOption> parsedOptions, StringComparison comparisonType);
4042

4143
/// <summary>
4244
/// Shows the help text for the specified registered options.

0 commit comments

Comments
 (0)