Skip to content

Commit 8346d33

Browse files
committed
+ Added support string collections
1 parent 9746c52 commit 8346d33

File tree

3 files changed

+160
-4
lines changed

3 files changed

+160
-4
lines changed

src/FlowCommandLine/CommandLine.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,12 @@ private List<string> GetParts () {
305305
if ( currentIndex > -1 ) previousCharacter = m_commandLine[currentIndex];
306306
currentIndex++;
307307

308-
if ( character == '"' && parameterStarted ) continue; // don't fill quotes to result
309-
310308
if ( character == ' ' && !parameterStarted ) {
311309
result.Add ( currentPart.ToString () );
312310
currentPart.Clear ();
313311
continue;
314312
}
315-
if ( character == '-' && previousCharacter == ' ' && !parameterStarted ) {
313+
if ( character == '-' && (previousCharacter == ' ' || currentIndex == 0) && !parameterStarted ) {
316314
parameterStarted = true;
317315
result.Add ( currentPart.ToString () );
318316
currentPart.Clear ();

src/FlowCommandLine/FlowPropertyMapper.cs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Globalization;
22
using System.Reflection;
3+
using System.Text;
34

45
namespace FlowCommandLine {
56

@@ -120,7 +121,19 @@ public static bool SetPropertyValue<T> ( Type type, Dictionary<string, string> v
120121
break;
121122
case Type _ when type == typeof ( string ):
122123
if ( values.ContainsKey ( parameterKey ) ) {
123-
property.SetValue ( model, values[parameterKey] );
124+
property.SetValue ( model, MapStringValue ( values[parameterKey] ) );
125+
isChanged = true;
126+
}
127+
break;
128+
case Type _ when type == typeof ( IEnumerable<string> ):
129+
if ( values.ContainsKey ( parameterKey ) ) {
130+
property.SetValue ( model, MapStringCollections ( values[parameterKey] ) );
131+
isChanged = true;
132+
}
133+
break;
134+
case Type _ when type == typeof ( List<string> ):
135+
if ( values.ContainsKey ( parameterKey ) ) {
136+
property.SetValue ( model, MapStringCollections ( values[parameterKey] ) );
124137
isChanged = true;
125138
}
126139
break;
@@ -200,6 +213,62 @@ private static List<float> MapFloatCollections ( Dictionary<string, string> valu
200213
.ToList ();
201214
}
202215

216+
private static string MapStringValue ( string value ) {
217+
value = value.Trim ();
218+
if ( value.StartsWith ( '\"' ) ) value = value[1..];
219+
if ( value.EndsWith ( '\"' ) ) value = value[..^1];
220+
221+
return value;
222+
}
223+
224+
private static List<string> GetStringParts ( string value ) {
225+
var currentValue = new StringBuilder ();
226+
var result = new List<string> ();
227+
var quoteStarted = false;
228+
foreach ( var character in value ) {
229+
if ( character == ' ' && !quoteStarted ) {
230+
if ( currentValue.Length > 0 ) {
231+
result.Add ( currentValue.ToString () );
232+
currentValue.Clear ();
233+
}
234+
continue;
235+
}
236+
if ( character == '\"' && !quoteStarted ) {
237+
quoteStarted = true;
238+
if ( currentValue.Length > 0 ) {
239+
result.Add ( currentValue.ToString () );
240+
currentValue.Clear ();
241+
}
242+
continue;
243+
}
244+
if ( character == '\"' && quoteStarted ) {
245+
quoteStarted = false;
246+
if ( currentValue.Length > 0 ) {
247+
result.Add ( currentValue.ToString () );
248+
currentValue.Clear ();
249+
}
250+
continue;
251+
}
252+
253+
currentValue.Append ( character );
254+
}
255+
256+
if ( currentValue.Length > 0 ) result.Add ( currentValue.ToString () );
257+
258+
return result;
259+
}
260+
261+
private static List<string> MapStringCollections ( string value ) {
262+
if ( value.Contains ( '\"' ) ) {
263+
return GetStringParts ( value );
264+
} else {
265+
return value
266+
.Split ( " " )
267+
.Where ( a => !string.IsNullOrEmpty ( a ) )
268+
.ToList ();
269+
}
270+
}
271+
203272
}
204273

205274
}

src/FlowCommandLineTests/CommandLineUnitTests.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,95 @@ public void RunOptions_Success_ListFloatParameter () {
709709
Assert.Equal ( new List<float> { 100.0f, 100.1f, 100.2f, 100.3f, 100.4f, 100.5f }, result.Parameter3 );
710710
}
711711

712+
public record RunOptions_Success_StringParameter_Class {
713+
public string Parameter1 { get; set; } = "";
714+
public string Parameter2 { get; set; } = "";
715+
public string Parameter3 { get; set; } = "";
716+
}
717+
718+
[Fact]
719+
public void RunOptions_Success_StringParameter () {
720+
//arrange
721+
var messages = new List<string> ();
722+
var fakeProvider = A.Fake<ICommandLineProvider> ();
723+
A.CallTo ( () => fakeProvider.GetCommandLine () ).Returns ( "--parameter1=argus --parameter2=test values with spaces --parameter3=\"quotes string with spaces\"" );
724+
A.CallTo ( () => fakeProvider.WriteLine ( A<string>._ ) ).Invokes ( ( string fake ) => { messages.Add ( fake ); } );
725+
var commandLine = new CommandLine ( fakeProvider );
726+
727+
//act
728+
var result = commandLine
729+
.Application ( "TestApplication", "1.0.0" )
730+
.AddOption ( fullName: "Parameter1" )
731+
.AddOption ( fullName: "Parameter2" )
732+
.AddOption ( fullName: "Parameter3" )
733+
.RunOptions<RunOptions_Success_StringParameter_Class> ();
734+
735+
//assert
736+
Assert.NotNull ( result );
737+
Assert.Equal ( "argus", result.Parameter1 );
738+
Assert.Equal ( "test values with spaces", result.Parameter2 );
739+
Assert.Equal ( "quotes string with spaces", result.Parameter3 );
740+
}
741+
742+
public record RunOptions_Success_IEnumerableStringParameter_Class {
743+
public IEnumerable<string> Parameter1 { get; set; } = Enumerable.Empty<string> ();
744+
public IEnumerable<string> Parameter2 { get; set; } = Enumerable.Empty<string> ();
745+
public IEnumerable<string> Parameter3 { get; set; } = Enumerable.Empty<string> ();
746+
}
747+
748+
[Fact]
749+
public void RunOptions_Success_IEnumerableStringParameter () {
750+
//arrange
751+
var messages = new List<string> ();
752+
var fakeProvider = A.Fake<ICommandLineProvider> ();
753+
A.CallTo ( () => fakeProvider.GetCommandLine () ).Returns ( "--parameter1=argus bargus margus --parameter2=test values with spaces --parameter3=first1212121 \"quotes string with spaces\" last4534534" );
754+
A.CallTo ( () => fakeProvider.WriteLine ( A<string>._ ) ).Invokes ( ( string fake ) => { messages.Add ( fake ); } );
755+
var commandLine = new CommandLine ( fakeProvider );
756+
757+
//act
758+
var result = commandLine
759+
.Application ( "TestApplication", "1.0.0" )
760+
.AddOption ( fullName: "Parameter1" )
761+
.AddOption ( fullName: "Parameter2" )
762+
.AddOption ( fullName: "Parameter3" )
763+
.RunOptions<RunOptions_Success_IEnumerableStringParameter_Class> ();
764+
765+
//assert
766+
Assert.NotNull ( result );
767+
Assert.Equal ( new List<string> { "argus", "bargus", "margus" }, result.Parameter1 );
768+
Assert.Equal ( new List<string> { "test", "values", "with", "spaces" }, result.Parameter2 );
769+
Assert.Equal ( new List<string> { "first1212121", "quotes string with spaces", "last4534534" }, result.Parameter3 );
770+
}
771+
772+
public record RunOptions_Success_ListStringParameter_Class {
773+
public List<string> Parameter1 { get; set; } = Enumerable.Empty<string> ().ToList ();
774+
public List<string> Parameter2 { get; set; } = Enumerable.Empty<string> ().ToList ();
775+
public List<string> Parameter3 { get; set; } = Enumerable.Empty<string> ().ToList ();
776+
}
777+
778+
[Fact]
779+
public void RunOptions_Success_ListStringParameter () {
780+
//arrange
781+
var messages = new List<string> ();
782+
var fakeProvider = A.Fake<ICommandLineProvider> ();
783+
A.CallTo ( () => fakeProvider.GetCommandLine () ).Returns ( "--parameter1=argus bargus margus --parameter2=test values with spaces --parameter3=first1212121 \"quotes string with spaces\" last4534534" );
784+
A.CallTo ( () => fakeProvider.WriteLine ( A<string>._ ) ).Invokes ( ( string fake ) => { messages.Add ( fake ); } );
785+
var commandLine = new CommandLine ( fakeProvider );
786+
787+
//act
788+
var result = commandLine
789+
.Application ( "TestApplication", "1.0.0" )
790+
.AddOption ( fullName: "Parameter1" )
791+
.AddOption ( fullName: "Parameter2" )
792+
.AddOption ( fullName: "Parameter3" )
793+
.RunOptions<RunOptions_Success_ListStringParameter_Class> ();
794+
795+
//assert
796+
Assert.NotNull ( result );
797+
Assert.Equal ( new List<string> { "argus", "bargus", "margus" }, result.Parameter1 );
798+
Assert.Equal ( new List<string> { "test", "values", "with", "spaces" }, result.Parameter2 );
799+
Assert.Equal ( new List<string> { "first1212121", "quotes string with spaces", "last4534534" }, result.Parameter3 );
800+
}
712801

713802
}
714803

0 commit comments

Comments
 (0)