Skip to content

Commit c3ad566

Browse files
committed
added a couple more tests around the expected results of the parse operation when the double dash is used throughout arg string, also updated the parser to strip out the -- from the results
1 parent c7c918b commit c3ad566

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

FluentCommandLineParser.Tests/Internals/CommandLineParserEngineMark2Tests.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class when_args_contains_negative_arguments_seperated_with_double_dash : ParseTe
108108
result.ParsedOptions.First().Value.ShouldEqual("-4321");
109109
}
110110

111-
class when_args_contains_single_switch : ParseTestContext
111+
class when_args_contains_a_single_switch : ParseTestContext
112112
{
113113
Establish context = () => SetupArgs("-b");
114114

@@ -129,8 +129,8 @@ class when_args_contains_only_the_double_dash_option_prefix : ParseTestContext
129129
It should_return_no_parsed_options = () =>
130130
result.ParsedOptions.ShouldBeEmpty();
131131

132-
It should_return_it_as_an_additional = () =>
133-
result.AdditionalValues.ShouldContainOnly("--");
132+
It should_return_no_additional = () =>
133+
result.AdditionalValues.ShouldBeEmpty();
134134
}
135135

136136
class when_args_contains_only_the_single_dash_option_prefix : ParseTestContext
@@ -154,6 +154,28 @@ class when_args_contains_only_the_slash_option_prefix : ParseTestContext
154154
It should_return_it_as_an_additional = () =>
155155
result.AdditionalValues.ShouldContainOnly("/");
156156
}
157+
158+
class when_args_contains_only_arguments_and_no_options : ParseTestContext
159+
{
160+
Establish context = () => SetupArgs("arg1 arg2 arg3");
161+
162+
It should_return_no_parsed_options = () =>
163+
result.ParsedOptions.ShouldBeEmpty();
164+
165+
It should_return_it_as_an_additional = () =>
166+
result.AdditionalValues.ShouldContainOnly("arg1", "arg2", "arg3");
167+
}
168+
169+
class when_args_starts_with_a_double_dash : ParseTestContext
170+
{
171+
Establish context = () => SetupArgs("-- --int 1 2 3 -a -b ");
172+
173+
It should_return_no_parsed_options = () =>
174+
result.ParsedOptions.ShouldBeEmpty();
175+
176+
It should_return_all_but_the_double_dash_as_an_additional = () =>
177+
result.AdditionalValues.ShouldContainOnly("--int", "1", "2", "3", "-a", "-b");
178+
}
157179
}
158180
}
159181

FluentCommandLineParser/Internals/Parsing/CommandLineParserEngineMark2.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
// POSSIBILITY OF SUCH DAMAGE.
2323
#endregion
2424

25+
using System;
2526
using System.Collections.Generic;
2627
using System.Linq;
28+
using Fclp.Internals.Extensions;
2729

2830
namespace Fclp.Internals.Parsing
2931
{
@@ -32,8 +34,9 @@ namespace Fclp.Internals.Parsing
3234
/// </summary>
3335
public class CommandLineParserEngineMark2 : ICommandLineParserEngine
3436
{
35-
private readonly List<string> _additionalOptionsFound = new List<string>();
37+
private readonly List<string> _additionalArgumentsFound = new List<string>();
3638
private readonly List<ParsedOption> _parsedOptions = new List<ParsedOption>();
39+
private readonly OptionArgumentParser _optionArgumentParser = new OptionArgumentParser();
3740

3841
/// <summary>
3942
/// Parses the specified <see><cref>T:System.String[]</cref></see> into appropriate <see cref="ParsedOption"/> objects..
@@ -52,7 +55,7 @@ public ParserEngineResult Parse(string[] args)
5255
ParseGroupIntoOption(rawKey, optionGroup.Skip(1));
5356
}
5457

55-
return new ParserEngineResult(_parsedOptions, _additionalOptionsFound);
58+
return new ParserEngineResult(_parsedOptions, _additionalArgumentsFound);
5659
}
5760

5861
private void ParseGroupIntoOption(string rawKey, IEnumerable<string> optionGroup)
@@ -63,14 +66,14 @@ private void ParseGroupIntoOption(string rawKey, IEnumerable<string> optionGroup
6366

6467
TrimSuffix(parsedOption);
6568

66-
new OptionArgumentParser().ParseArguments(optionGroup, parsedOption);
69+
_optionArgumentParser.ParseArguments(optionGroup, parsedOption);
6770

6871
AddParsedOptionToList(parsedOption);
6972
}
7073
else
7174
{
72-
_additionalOptionsFound.Add(rawKey);
73-
_additionalOptionsFound.AddRange(optionGroup);
75+
AddAdditionArgument(rawKey);
76+
optionGroup.ForEach(AddAdditionArgument);
7477
}
7578
}
7679

@@ -86,6 +89,14 @@ private void AddParsedOptionToList(ParsedOption parsedOption)
8689
}
8790
}
8891

92+
private void AddAdditionArgument(string argument)
93+
{
94+
if (IsEndOfOptionsKey(argument) == false)
95+
{
96+
_additionalArgumentsFound.Add(argument);
97+
}
98+
}
99+
89100
private static bool ShortOptionNeedsToBeSplit(ParsedOption parsedOption)
90101
{
91102
return PrefixIsShortOption(parsedOption.Prefix) && parsedOption.Key.Length > 1;
@@ -122,12 +133,20 @@ private static void TrimSuffix(ParsedOption parsedOption)
122133
/// <param name="arg">The <see cref="System.String"/> to examine.</param>
123134
/// <returns><c>true</c> if <paramref name="arg"/> is a Option key; otherwise <c>false</c>.</returns>
124135
static bool IsAKey(string arg)
125-
{
136+
{ // TODO: push related special char operations into there own object
126137
return arg != null
127138
&& SpecialCharacters.OptionPrefix.Any(arg.StartsWith)
128139
&& SpecialCharacters.OptionPrefix.Any(arg.Equals) == false;
129140
}
130141

142+
/// <summary>
143+
/// Determines whether the specified string indicates the end of parsed options.
144+
/// </summary>
145+
static bool IsEndOfOptionsKey(string arg)
146+
{
147+
return string.Equals(arg, SpecialCharacters.EndOfOptionsKey, StringComparison.InvariantCultureIgnoreCase);
148+
}
149+
131150
/// <summary>
132151
/// Parses the specified <see><cref>T:System.String[]</cref></see> into key value pairs.
133152
/// </summary>

0 commit comments

Comments
 (0)