From 801251ed7cc74844e95279c449a65395e4848165 Mon Sep 17 00:00:00 2001 From: Henrik Larsson Date: Sun, 6 Oct 2024 22:01:46 +0200 Subject: [PATCH] add unit tests --- Simple.ArgumentParser.Tests/ArgsTests.cs | 314 ++++++++++++++++++ .../ArgumentParserTests.cs | 3 + .../Options/BooleanOptionTests.cs | 4 + .../Options/CharOptionTests.cs | 60 ++++ .../Options/DoubleOptionTests.cs | 61 ++++ .../Options/EnumerateOptionTests.cs | 59 ++++ .../Options/FlagOptionTests.cs | 60 ++++ .../Options/HelpOptionTests.cs | 34 ++ .../Options/IntegerOptionTests.cs | 65 ++++ .../Options/OptionSettingsTests.cs | 20 ++ .../Options/OptionTypeTests.cs | 19 ++ .../Options/VersionOptionTests.cs | 34 ++ Simple.ArgumentParser/Args.cs | 2 +- .../ArgumentOptionException.cs | 7 +- Simple.ArgumentParser/ArgumentParser.cs | 4 +- Simple.ArgumentParser/Constants.cs | 3 +- .../Options/BooleanOption.cs | 2 +- Simple.ArgumentParser/Options/FlagOption.cs | 6 +- 18 files changed, 742 insertions(+), 15 deletions(-) create mode 100644 Simple.ArgumentParser.Tests/ArgsTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/CharOptionTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/DoubleOptionTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/EnumerateOptionTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/FlagOptionTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/HelpOptionTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/IntegerOptionTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/OptionSettingsTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/OptionTypeTests.cs create mode 100644 Simple.ArgumentParser.Tests/Options/VersionOptionTests.cs diff --git a/Simple.ArgumentParser.Tests/ArgsTests.cs b/Simple.ArgumentParser.Tests/ArgsTests.cs new file mode 100644 index 0000000..c05eed2 --- /dev/null +++ b/Simple.ArgumentParser.Tests/ArgsTests.cs @@ -0,0 +1,314 @@ +namespace Simple.ArgumentParser.Tests; + +public class ArgsTests +{ + [Fact] + public void IsValid_should_return_true_if_no_missing_or_invalid_arguments() + { + // arrange + // act + + var args = new Args(); + + // assert + + Assert.True(args.IsValid); + } + + [Fact] + public void IsValid_should_return_false_if_any_missing_argument_exists() + { + // arrange + // act + + var args = new Args(); + args.Missing.Add("missing-argument"); + + // assert + + Assert.False(args.IsValid); + } + + [Fact] + public void IsValid_should_return_false_if_any_invalid_argument_exists() + { + // arrange + // act + + var args = new Args(); + args.Invalid.Add("invalid-argument"); + + // assert + + Assert.False(args.IsValid); + } + + [Fact] + public void HasIgnoredCommands_should_return_true_if_any_ignored_commands_exists() + { + // arrange + // act + + var args = new Args(); + args.Ignored.Add(new Command { Name = "ignored-command" }); + + // assert + + Assert.True(args.HasIgnoredCommands); + } + + [Fact] + public void HasIgnoredCommands_should_return_false_if_no_ignored_commands_exists() + { + // arrange + // act + + var args = new Args(); + + // assert + + Assert.False(args.HasIgnoredCommands); + } + + [Fact] + public void HasMissingCommands_should_return_true_if_any_missing_commands_exists() + { + // arrange + // act + + var args = new Args(); + args.Missing.Add("missing-command"); + + // assert + + Assert.True(args.HasMissingCommands); + } + + [Fact] + public void HasMissingCommands_should_return_false_if_no_missing_commands_exists() + { + // arrange + // act + + var args = new Args(); + + // assert + + Assert.False(args.HasMissingCommands); + } + + [Fact] + public void HasInvalidCommands_should_return_true_if_any_invalid_commands_exists() + { + // arrange + // act + + var args = new Args(); + args.Invalid.Add("invalid-command"); + + // assert + + Assert.True(args.HasInvalidCommands); + } + + [Fact] + public void HasInvalidCommands_should_return_false_if_no_invalid_commands_exists() + { + // arrange + // act + + var args = new Args(); + + // assert + + Assert.False(args.HasInvalidCommands); + } + + [Fact] + public void HelpRequested_should_return_true_if_help_flag_is_specified() + { + // arrange + // act + + var args = new ArgumentParser().AddHelpOption().Parse(["--help"]); + + // assert + + Assert.True(args.HelpRequested); + } + + [Fact] + public void HelpRequested_should_return_false_if_help_flag_specified() + { + // arrange + // act + + var args = new ArgumentParser().AddHelpOption().Parse(["--not-help"]); + + // assert + + Assert.False(args.HelpRequested); + } + + [Fact] + public void VersionRequested_should_return_true_if_version_flag_is_specified() + { + // arrange + // act + + var args = new ArgumentParser().AddVersionOption().Parse(["--version"]); + + // assert + + Assert.True(args.VersionRequested); + } + + [Fact] + public void HelpRequested_should_return_false_if_version_flag_is_specified() + { + // arrange + // act + + var args = new ArgumentParser().AddVersionOption().Parse(["--not-version"]); + + // assert + + Assert.False(args.VersionRequested); + } + + [Fact] + public void HelpSection_is_not_empty_if_help_flag_is_specified() + { + // arrange + // act + + var args = new ArgumentParser().AddHelpOption().Parse(["--help"]); + + // assert + + Assert.NotNull(args.HelpSection); + Assert.NotEmpty(args.HelpSection); + } + + [Fact] + public void HelpSection_is_empty_if_help_flag_is_not_specified() + { + // arrange + // act + + var args = new ArgumentParser().AddHelpOption().Parse(["--not-help"]); + + // assert + + Assert.Null(args.HelpSection); + } + + [Fact] + public void Version_is_not_empty_if_version_flag_is_specified() + { + // arrange + // act + + var args = new ArgumentParser().AddVersionOption().Parse(["--version"]); + + // assert + + Assert.NotNull(args.Version); + Assert.NotEmpty(args.Version); + } + + [Fact] + public void Version_is_empty_if_version_flag_is_not_specified() + { + // arrange + // act + + var args = new ArgumentParser().AddVersionOption().Parse(["--not-version"]); + + // assert + + Assert.Null(args.Version); + } + + [Fact] + public void Any_returns_true_if_any_valid_command_exists() + { + // arrange + // act + + var args = new Args { Valid = { new Command { Name = "valid-command" } } }; + + // assert + + Assert.True(args.Any()); + } + + [Fact] + public void Any_returns_false_if_no_valid_command_exists() + { + // arrange + // act + + var args = new Args(); + + // assert + + Assert.False(args.Any()); + } + + [Fact] + public void Get_returns_matching_command_if_valid_key() + { + // arrange + + var args = new Args { Valid = { new Command { Name = "valid-command" } } }; + + // act + + var result = args.Get("valid-command"); + + // assert + + Assert.NotNull(result); + Assert.Equal(result.Name, "valid-command"); + } + + [Fact] + public void Get_returns_null_if_invalid_key() + { + // arrange + + var args = new Args { Valid = { new Command { Name = "valid-command" } } }; + + // act + + var result = args.Get("invalid-key"); + + // assert + + Assert.Null(result); + } + + [Fact] + public void GetAll_returns_all_valid_commands() + { + // arrange + + var args = new Args { Valid = + { + new Command { Name = "valid-command-1" }, + new Command { Name = "valid-command-2" }, + new Command { Name = "valid-command-3" } + } }; + + // act + + var result = args.GetAll(); + + // assert + + Assert.NotNull(result); + Assert.NotEmpty(result); + Assert.Equal(result.Count, 3); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/ArgumentParserTests.cs b/Simple.ArgumentParser.Tests/ArgumentParserTests.cs index 8649022..5d5bcf7 100644 --- a/Simple.ArgumentParser.Tests/ArgumentParserTests.cs +++ b/Simple.ArgumentParser.Tests/ArgumentParserTests.cs @@ -43,6 +43,7 @@ public void Parse_should_handle_expected_arguments() Assert.False(result.HasMissingCommands); Assert.False(result.HasIgnoredCommands); Assert.False(result.HasInvalidCommands); + Assert.Equal(result.Valid.Count, 9); Assert.NotNull(result.Version); Assert.NotEmpty(result.Version); Assert.NotNull(result.HelpSection); @@ -71,6 +72,8 @@ public void Parse_should_ignore_unexpected_arguments() Assert.True(result.IsValid); Assert.False(result.HasMissingCommands); Assert.True(result.HasIgnoredCommands); + var ignored = Assert.Single(result.Ignored); + Assert.Equal(ignored.Name, "help"); Assert.False(result.HasInvalidCommands); Assert.Null(result.Version); Assert.Null(result.HelpSection); diff --git a/Simple.ArgumentParser.Tests/Options/BooleanOptionTests.cs b/Simple.ArgumentParser.Tests/Options/BooleanOptionTests.cs index bed1863..89f029f 100644 --- a/Simple.ArgumentParser.Tests/Options/BooleanOptionTests.cs +++ b/Simple.ArgumentParser.Tests/Options/BooleanOptionTests.cs @@ -18,7 +18,9 @@ public class BooleanOptionTests [Theory] [InlineData("true")] + [InlineData("TRUE")] [InlineData("false")] + [InlineData("FALSE")] [InlineData("1")] [InlineData("0")] public void ValueIsValid_should_return_true_with_strict_off(string inlineData) @@ -40,6 +42,8 @@ public void ValueIsValid_should_return_true_with_strict_off(string inlineData) [Theory] [InlineData("1")] [InlineData("0")] + [InlineData("TRUE")] + [InlineData("FALSE")] public void ValueIsValid_should_return_false_with_strict_on(string inlineData) { // arrange diff --git a/Simple.ArgumentParser.Tests/Options/CharOptionTests.cs b/Simple.ArgumentParser.Tests/Options/CharOptionTests.cs new file mode 100644 index 0000000..aa86a69 --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/CharOptionTests.cs @@ -0,0 +1,60 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class CharOptionTests +{ + private static CharOption GetSubjectUnderTest(OptionSettings? settings = null) => new( + name: "test-name", + shortName: 'c', + description: "test-description", + settings: settings ?? new OptionSettings()); + + private static OptionSettings GetStrictSettings() => new OptionSettings + { + Strict = true, + Required = false + }; + + [Theory] + [InlineData("a")] + [InlineData("1")] + [InlineData("@")] + [InlineData("-")] + public void ValueIsValid_should_return_true_as_expected(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.True(result); + Assert.NotNull(message); + Assert.Empty(message); + } + + [Theory] + [InlineData("")] + [InlineData("not-a-char")] + public void ValueIsValid_should_return_false_if_value_is_not_char(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.False(result); + Assert.NotNull(message); + Assert.NotEmpty(message); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/Options/DoubleOptionTests.cs b/Simple.ArgumentParser.Tests/Options/DoubleOptionTests.cs new file mode 100644 index 0000000..ff914f5 --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/DoubleOptionTests.cs @@ -0,0 +1,61 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class DoubleOptionTests +{ + private static DoubleOption GetSubjectUnderTest(OptionSettings? settings = null) => new( + name: "test-name", + shortName: 'c', + description: "test-description", + settings: settings ?? new OptionSettings()); + + private static OptionSettings GetStrictSettings() => new OptionSettings + { + Strict = true, + Required = false + }; + + [Theory] + [InlineData("123")] + [InlineData("1.23")] + [InlineData("1,23")] + [InlineData("0")] + [InlineData("-0")] + public void ValueIsValid_should_return_true_if_input_is_double(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.True(result); + Assert.NotNull(message); + Assert.Empty(message); + } + + [Theory] + [InlineData("not-a-double")] + [InlineData("")] + public void ValueIsValid_should_return_false_if_input_is_not_double(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.False(result); + Assert.NotNull(message); + Assert.NotEmpty(message); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/Options/EnumerateOptionTests.cs b/Simple.ArgumentParser.Tests/Options/EnumerateOptionTests.cs new file mode 100644 index 0000000..4f9b8f4 --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/EnumerateOptionTests.cs @@ -0,0 +1,59 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class EnumerateOptionTests +{ + private static EnumerateOption GetSubjectUnderTest(OptionSettings? settings = null) => new( + name: "test-name", + shortName: 'c', + description: "test-description", + [ "accepted1", "accepted2" ], + settings: settings ?? new OptionSettings()); + + private static OptionSettings GetStrictSettings() => new OptionSettings + { + Strict = true, + Required = false + }; + + [Theory] + [InlineData("accepted1")] + [InlineData("accepted2")] + public void ValueIsValid_should_return_true_if_input_is_defined(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.True(result); + Assert.NotNull(message); + Assert.Empty(message); + } + + [Theory] + [InlineData("accepted3")] + [InlineData("")] + public void ValueIsValid_should_return_false_if_input_is_not_defined(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.False(result); + Assert.NotNull(message); + Assert.NotEmpty(message); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/Options/FlagOptionTests.cs b/Simple.ArgumentParser.Tests/Options/FlagOptionTests.cs new file mode 100644 index 0000000..65d91f2 --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/FlagOptionTests.cs @@ -0,0 +1,60 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class FlagOptionTests +{ + private static FlagOption GetSubjectUnderTest(OptionSettings? settings = null) => new( + name: "test-name", + shortName: 'c', + description: "test-description", + settings: settings ?? new OptionSettings()); + + private static OptionSettings GetStrictSettings() => new OptionSettings + { + Strict = true, + Required = false + }; + + [Theory] + [InlineData("")] + [InlineData(" ")] + [InlineData(null)] + public void ValueIsValid_should_return_true_if_input_is_none(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.True(result); + Assert.NotNull(message); + Assert.Empty(message); + } + + [Theory] + [InlineData("1")] + [InlineData("abc")] + [InlineData("_")] + public void ValueIsValid_should_return_false_if_any_value_at_all(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.False(result); + Assert.NotNull(message); + Assert.NotEmpty(message); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/Options/HelpOptionTests.cs b/Simple.ArgumentParser.Tests/Options/HelpOptionTests.cs new file mode 100644 index 0000000..95e1c36 --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/HelpOptionTests.cs @@ -0,0 +1,34 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class HelpOptionTests +{ + private static HelpOption GetSubjectUnderTest() => new(); + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("123")] + [InlineData(" ")] + [InlineData("_")] + [InlineData("abc")] + [InlineData("bool")] + [InlineData("--")] + public void ValueIsValid_should_return_true_whatever_value_provided(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.True(result); + Assert.NotNull(message); + Assert.Empty(message); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/Options/IntegerOptionTests.cs b/Simple.ArgumentParser.Tests/Options/IntegerOptionTests.cs new file mode 100644 index 0000000..0edfc58 --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/IntegerOptionTests.cs @@ -0,0 +1,65 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class IntegerOptionTests +{ + private static IntegerOption GetSubjectUnderTest(OptionSettings? settings = null) => new( + name: "test-name", + shortName: 't', + description: "test-description", + settings: settings ?? new OptionSettings()); + + private static OptionSettings GetStrictSettings() => new OptionSettings + { + Strict = true, + Required = false + }; + + [Theory] + [InlineData("-1")] + [InlineData("0")] + [InlineData("-0")] + [InlineData("1")] + public void ValueIsValid_should_return_true_if_input_is_integer(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.True(result); + Assert.NotNull(message); + Assert.Empty(message); + } + + [Theory] + [InlineData("3.14")] + [InlineData("abc")] + [InlineData("")] + [InlineData(" ")] + [InlineData(null)] + [InlineData("3,14")] + [InlineData("five")] + public void ValueIsValid_should_return_false_if_input_is_not_integer(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.False(result); + Assert.NotNull(message); + Assert.NotEmpty(message); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/Options/OptionSettingsTests.cs b/Simple.ArgumentParser.Tests/Options/OptionSettingsTests.cs new file mode 100644 index 0000000..4e9e2b8 --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/OptionSettingsTests.cs @@ -0,0 +1,20 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class OptionSettingsTests +{ + [Fact] + public void Constructor_sets_default_values_to_false() + { + // arrange + // act + + var subjectUnderTest = new OptionSettings(); + + // assert + + Assert.False(subjectUnderTest.Required); + Assert.False(subjectUnderTest.Strict); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/Options/OptionTypeTests.cs b/Simple.ArgumentParser.Tests/Options/OptionTypeTests.cs new file mode 100644 index 0000000..2c2b45b --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/OptionTypeTests.cs @@ -0,0 +1,19 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class OptionTypeTests +{ + [Fact] + public void Constructor_sets_default_to_alpha() + { + // arrange + // act + + OptionType subjectUnderTest = default; + + // assert + + Assert.Equal(subjectUnderTest, OptionType.Alpha); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser.Tests/Options/VersionOptionTests.cs b/Simple.ArgumentParser.Tests/Options/VersionOptionTests.cs new file mode 100644 index 0000000..e203dfd --- /dev/null +++ b/Simple.ArgumentParser.Tests/Options/VersionOptionTests.cs @@ -0,0 +1,34 @@ +using Simple.ArgumentParser.Options; + +namespace Simple.ArgumentParser.Tests.Options; + +public class VersionOptionTests +{ + private static VersionOption GetSubjectUnderTest() => new(); + + [Theory] + [InlineData(null)] + [InlineData("")] + [InlineData("123")] + [InlineData(" ")] + [InlineData("_")] + [InlineData("abc")] + [InlineData("bool")] + [InlineData("--")] + public void ValueIsValid_should_return_true_whatever_value_provided(string input) + { + // arrange + + var subjectUnderTest = GetSubjectUnderTest(); + + // act + + var result = subjectUnderTest.ValueIsValid(input, out var message); + + // assert + + Assert.True(result); + Assert.NotNull(message); + Assert.Empty(message); + } +} \ No newline at end of file diff --git a/Simple.ArgumentParser/Args.cs b/Simple.ArgumentParser/Args.cs index f875945..ec4ac7d 100644 --- a/Simple.ArgumentParser/Args.cs +++ b/Simple.ArgumentParser/Args.cs @@ -10,7 +10,7 @@ public class Args public bool VersionRequested { get; internal set; } - public List Valid { get; set; } = []; + public List Valid { get; } = []; public List Ignored { get; } = []; public List Missing { get; } = []; public List Invalid { get; } = []; diff --git a/Simple.ArgumentParser/ArgumentOptionException.cs b/Simple.ArgumentParser/ArgumentOptionException.cs index efb6500..e091798 100644 --- a/Simple.ArgumentParser/ArgumentOptionException.cs +++ b/Simple.ArgumentParser/ArgumentOptionException.cs @@ -1,8 +1,3 @@ namespace Simple.ArgumentParser; -public class ArgumentOptionException : Exception -{ - public ArgumentOptionException(string message) : base(message) - { - } -} \ No newline at end of file +public class ArgumentOptionException(string message) : Exception(message); \ No newline at end of file diff --git a/Simple.ArgumentParser/ArgumentParser.cs b/Simple.ArgumentParser/ArgumentParser.cs index 5411adc..811db58 100644 --- a/Simple.ArgumentParser/ArgumentParser.cs +++ b/Simple.ArgumentParser/ArgumentParser.cs @@ -82,8 +82,6 @@ public ArgumentParser AddEnumerateOption(string name, char shortName, string des public Args Parse(string[] args) { - // if (_options.Count == 0) return new Args(); - foreach (Match match in Regex.Matches(string.Join(" ", args), Constants.Pattern)) { var kv = GetKeyValue(match); @@ -148,7 +146,7 @@ public Args Parse(string[] args) if (_validCommands.Count > 0) { - parsedArguments.Valid = _validCommands; + parsedArguments.Valid.AddRange(_validCommands); } return parsedArguments; diff --git a/Simple.ArgumentParser/Constants.cs b/Simple.ArgumentParser/Constants.cs index d78672c..780599d 100644 --- a/Simple.ArgumentParser/Constants.cs +++ b/Simple.ArgumentParser/Constants.cs @@ -2,7 +2,8 @@ namespace Simple.ArgumentParser; internal static class Constants { - internal const string Pattern = @"(?--\w+|-[a-zA-Z])(?:\s+(?(?!-[-\w])[^\s]+))?"; + internal const string Pattern = @"(?--[a-zA-Z0-9\-]+|-[a-zA-Z])(?:\s+(?(?!-[-a-zA-Z0-9])[^\s]+))?"; + // internal const string Pattern = @"(?--\w+|-[a-zA-Z])(?:\s+(?(?!-[-\w])[^\s]+))?"; internal const string LongPrefix = "--"; internal const string ShortPrefix = "-"; diff --git a/Simple.ArgumentParser/Options/BooleanOption.cs b/Simple.ArgumentParser/Options/BooleanOption.cs index 1c94df7..13d1890 100644 --- a/Simple.ArgumentParser/Options/BooleanOption.cs +++ b/Simple.ArgumentParser/Options/BooleanOption.cs @@ -23,7 +23,7 @@ protected internal override bool ValueIsValid(string value, out string message) return message == string.Empty; } - message = value is "1" or "0" or "true" or "false" + message = value.ToLower() is "1" or "0" or "true" or "false" ? string.Empty : $"Value '{value}' is invalid for Argument '{Name}' - expected a value of type '{Type}' [true or false, 1 or 0]"; diff --git a/Simple.ArgumentParser/Options/FlagOption.cs b/Simple.ArgumentParser/Options/FlagOption.cs index b6f4aa5..31447e9 100644 --- a/Simple.ArgumentParser/Options/FlagOption.cs +++ b/Simple.ArgumentParser/Options/FlagOption.cs @@ -11,9 +11,9 @@ internal FlagOption(string name, char shortName, string description, OptionSetti protected internal override bool ValueIsValid(string value, out string message) { - message = value != string.Empty - ? $"Argument '{Name}' is invalid - expected no value at all'" - : string.Empty; + message = string.IsNullOrWhiteSpace(value) + ? string.Empty + : $"Argument '{Name}' is invalid - expected no value at all'"; return message == string.Empty; }