Skip to content

Commit 67d7409

Browse files
committed
+ Added default parameter
1 parent 52977d8 commit 67d7409

8 files changed

Lines changed: 163 additions & 29 deletions

File tree

src/FlowCommandLine/CommandLine.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ public async Task<FlowCommandResult> RunCommandAsync () {
115115
};
116116
}
117117

118-
ParseParameters ( parts, out var command, out var parameters );
118+
ParseParameters ( parts, out var command, out var parameters, out var defaultParameter );
119119

120120
try {
121121
if ( m_commands.TryGetValue ( command, out var flowCommand ) ) {
122-
flowCommand.Execute ( parameters, m_commandLineProvider );
122+
flowCommand.Execute ( parameters, m_commandLineProvider, defaultParameter );
123123

124124
return new FlowCommandResult {
125125
EmptyInput = string.IsNullOrEmpty ( m_commandLine ),
@@ -128,7 +128,7 @@ public async Task<FlowCommandResult> RunCommandAsync () {
128128
};
129129
}
130130
if ( m_asyncCommands.TryGetValue ( command, out var flowAsyncCommand ) ) {
131-
await flowAsyncCommand.Execute ( parameters, m_commandLineProvider );
131+
await flowAsyncCommand.Execute ( parameters, m_commandLineProvider, defaultParameter );
132132
return new FlowCommandResult {
133133
EmptyInput = string.IsNullOrEmpty ( m_commandLine ),
134134
CommandHandled = true,
@@ -174,11 +174,11 @@ public FlowCommandResult RunCommand () {
174174
};
175175
}
176176

177-
ParseParameters ( parts, out var command, out var parameters );
177+
ParseParameters ( parts, out var command, out var parameters, out var defaultParameter );
178178

179179
try {
180180
if ( m_commands.TryGetValue ( command, out var flowCommand ) ) {
181-
flowCommand.Execute ( parameters, m_commandLineProvider );
181+
flowCommand.Execute ( parameters, m_commandLineProvider, defaultParameter );
182182

183183
return new FlowCommandResult {
184184
EmptyInput = string.IsNullOrEmpty ( m_commandLine ),
@@ -212,6 +212,13 @@ public CommandLine AddOption ( string shortName = "", string fullName = "", stri
212212
return this;
213213
}
214214

215+
public CommandLine AddOptions ( IEnumerable<FlowCommandParameter> parameters ) {
216+
m_options.AddRange ( parameters );
217+
218+
return this;
219+
}
220+
221+
215222
/// <summary>
216223
/// Run the command from the command line.
217224
/// </summary>
@@ -227,12 +234,12 @@ public CommandLine AddOption ( string shortName = "", string fullName = "", stri
227234
return default;
228235
}
229236

230-
ParseOptionParameters ( parts, out var parameters );
237+
ParseOptionParameters ( parts, out var parameters, out var defaultParameter );
231238

232239
var flowOptions = new FlowOptions<T> {
233240
Parameters = m_options
234241
};
235-
return flowOptions.MapParametersToType ( parameters, m_commandLineProvider );
242+
return flowOptions.MapParametersToType ( parameters, m_commandLineProvider, defaultParameter );
236243
}
237244

238245
private void ShowCommandHelp ( string description, List<FlowCommandParameter> parameters ) {
@@ -416,10 +423,12 @@ private List<string> GetParts () {
416423

417424
private bool IsHelpParameter ( string part ) => part is "-h" or "--help";
418425

419-
private static void ParseParameters ( List<string> parts, out string command, out Dictionary<string, string> parameters ) {
426+
public static void ParseParameters ( List<string> parts, out string command, out Dictionary<string, string> parameters, out string defaultParameter ) {
420427
command = parts.First ().ToLowerInvariant ();
428+
var isHaveDefaultParameter = parts.Count > 1 && !parts.ElementAt ( 1 ).StartsWith ( "-" );
429+
defaultParameter = isHaveDefaultParameter ? parts.ElementAt ( 1 ) : "";
421430
parameters = parts
422-
.Skip ( 1 )
431+
.Skip ( isHaveDefaultParameter ? 2 : 1 )
423432
.Where ( a => ( a.StartsWith ( "-" ) && a.Length > 1 ) || ( a.StartsWith ( "--" ) && a.Length > 2 ) )
424433
.Select (
425434
a => {
@@ -433,8 +442,11 @@ private static void ParseParameters ( List<string> parts, out string command, ou
433442
.ToDictionary ( a => a.Name, a => a.Value );
434443
}
435444

436-
private static void ParseOptionParameters ( List<string> parts, out Dictionary<string, string> parameters ) {
445+
private static void ParseOptionParameters ( List<string> parts, out Dictionary<string, string> parameters, out string defaultParameter ) {
446+
var isHaveDefaultParameter = parts.Count > 1 && !parts.ElementAt ( 0 ).StartsWith ( "-" );
447+
defaultParameter = isHaveDefaultParameter ? parts.ElementAt ( 0 ) : "";
437448
parameters = parts
449+
.Skip( isHaveDefaultParameter ? 1 : 0 )
438450
.Where ( a => ( a.StartsWith ( "-" ) && a.Length > 1 ) || ( a.StartsWith ( "--" ) && a.Length > 2 ) )
439451
.Select (
440452
a => {

src/FlowCommandLine/FlowAsyncCommand.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ namespace FlowCommandLine {
77

88
public FlowCommandLineCommandAsyncDelegate<T>? @Delegate { get; init; }
99

10-
public T MapParametersToType ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider ) {
10+
public T MapParametersToType ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider, string defaultParameterValue ) {
1111
var resultType = typeof ( T );
12+
var result = new T ();
1213

1314
var properties = resultType
1415
.GetProperties ( BindingFlags.Public | BindingFlags.Instance );
1516
var parameters = FlowPropertyMapper.ParametersToDictionary ( Parameters );
1617

18+
FlowPropertyMapper.FillDefaultParameter ( Parameters, defaultParameterValue, commandLineProvider, properties, result );
19+
1720
var processedParameters = new HashSet<FlowCommandParameter> ();
18-
var result = new T ();
1921

2022
foreach ( var parameter in parameters ) {
2123
var property = FlowPropertyMapper.GetPropertyFromParameter ( parameter.Value, properties );
@@ -35,8 +37,8 @@ public T MapParametersToType ( Dictionary<string, string> values, ICommandLinePr
3537
return result;
3638
}
3739

38-
public override Task Execute ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider ) {
39-
var task = Delegate?.Invoke ( MapParametersToType ( values, commandLineProvider ) );
40+
public override Task Execute ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider, string defaultParameterValue ) {
41+
var task = Delegate?.Invoke ( MapParametersToType ( values, commandLineProvider, defaultParameterValue ) );
4042
if ( task != null ) return task;
4143

4244
return Task.CompletedTask;
@@ -50,7 +52,7 @@ internal record FlowAsyncCommand {
5052

5153
public List<FlowCommandParameter> Parameters { get; init; } = new List<FlowCommandParameter> ();
5254

53-
public virtual Task Execute ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider ) => Task.CompletedTask;
55+
public virtual Task Execute ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider, string defaultParameterValue ) => Task.CompletedTask;
5456

5557
}
5658

src/FlowCommandLine/FlowCommand.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ namespace FlowCommandLine {
77

88
public FlowCommandLineCommandDelegate<T>? @Delegate { get; init; }
99

10-
public T MapParametersToType ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider ) {
10+
public T MapParametersToType ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider, string defaultParameterValue ) {
1111
var resultType = typeof ( T );
12+
var result = new T ();
1213

1314
var properties = resultType
1415
.GetProperties ( BindingFlags.Public | BindingFlags.Instance );
1516
var parameters = FlowPropertyMapper.ParametersToDictionary ( Parameters );
1617

18+
FlowPropertyMapper.FillDefaultParameter ( Parameters, defaultParameterValue, commandLineProvider, properties, result );
19+
1720
var processedParameters = new HashSet<FlowCommandParameter> ();
18-
var result = new T ();
1921

2022
foreach ( var parameter in parameters ) {
2123
var property = FlowPropertyMapper.GetPropertyFromParameter ( parameter.Value, properties );
@@ -26,7 +28,7 @@ public T MapParametersToType ( Dictionary<string, string> values, ICommandLinePr
2628
if ( isProcessed ) processedParameters.Add ( parameter.Value );
2729
}
2830

29-
var requiredParameters = Parameters.Where ( a => a.Required ).ToList ();
31+
var requiredParameters = Parameters.Where ( a => a.Required && !a.Default ).ToList ();
3032
if ( requiredParameters.Intersect ( processedParameters ).Count () != requiredParameters.Count ) {
3133
commandLineProvider.WriteLine ( "Not all required parameters is defined!" );
3234
throw new Exception ( "Not all required parameters is defined!" );
@@ -35,10 +37,10 @@ public T MapParametersToType ( Dictionary<string, string> values, ICommandLinePr
3537
return result;
3638
}
3739

38-
public override void Execute ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider ) {
40+
public override void Execute ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider, string defaultParameter ) {
3941
if ( Delegate == null ) return;
4042

41-
@Delegate.Invoke ( MapParametersToType ( values, commandLineProvider ) );
43+
@Delegate.Invoke ( MapParametersToType ( values, commandLineProvider, defaultParameter ) );
4244
}
4345

4446
}
@@ -49,7 +51,7 @@ internal record FlowCommand {
4951

5052
public List<FlowCommandParameter> Parameters { get; init; } = new List<FlowCommandParameter> ();
5153

52-
public virtual void Execute ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider ) {
54+
public virtual void Execute ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider, string defaultParameter ) {
5355
}
5456

5557
}

src/FlowCommandLine/FlowCommandLine.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
<PackageTags>command,line,commandline,command-line,parse,parser,parsing,shell</PackageTags>
1717
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1818
<PackageReadmeFile>README.md</PackageReadmeFile>
19-
<Version>1.0.10.0</Version>
19+
<Version>1.0.11.0</Version>
2020
<IsTrimmable>true</IsTrimmable>
2121
<PublishTrimmed>true</PublishTrimmed>
2222
<PackageReleaseNotes>
23-
Added supported types decimal,IEnumerable/decimal,List/decimal, CommandLineRange/decimal
23+
Added supported default command parameter or default option
24+
Added FlowCommandParameter.CreateDefault method for create default parameter
25+
Added FlowCommandParameter.Default property for store make if parameter is default or not
26+
Added CommandLine.AddOptions method for get collection of options (from factory etc)
2427
</PackageReleaseNotes>
2528
</PropertyGroup>
2629

src/FlowCommandLine/FlowCommandParameter.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@ public sealed record FlowCommandParameter {
1515

1616
public bool Required { get; init; }
1717

18+
public bool Default { get; init; }
19+
1820
public static FlowCommandParameter Create ( string name = "", string alias = "", string help = "", string property = "" ) {
1921
return new FlowCommandParameter {
2022
FullName = alias,
2123
ShortName = name,
2224
Description = help,
2325
PropertyName = property,
24-
Required = false
26+
Required = false,
27+
Default = false
2528
};
2629
}
2730

@@ -31,7 +34,19 @@ public static FlowCommandParameter CreateRequired ( string name = "", string ali
3134
ShortName = name,
3235
Description = help,
3336
PropertyName = property,
34-
Required = true
37+
Required = true,
38+
Default = false
39+
};
40+
}
41+
42+
public static FlowCommandParameter CreateDefault ( string name = "", string alias = "", string help = "", string property = "" ) {
43+
return new FlowCommandParameter {
44+
FullName = alias,
45+
ShortName = name,
46+
Description = help,
47+
PropertyName = property,
48+
Required = true,
49+
Default = true
3550
};
3651
}
3752

src/FlowCommandLine/FlowOptions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ namespace FlowCommandLine {
77

88
public List<FlowCommandParameter> Parameters { get; init; } = new List<FlowCommandParameter> ();
99

10-
public T MapParametersToType ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider ) {
10+
public T MapParametersToType ( Dictionary<string, string> values, ICommandLineProvider commandLineProvider, string defaultParameterValue ) {
1111
var resultType = typeof ( T );
12+
var result = new T ();
1213

1314
var properties = resultType
1415
.GetProperties ( BindingFlags.Public | BindingFlags.Instance );
1516

1617
var parameters = FlowPropertyMapper.ParametersToDictionary ( Parameters );
1718

19+
FlowPropertyMapper.FillDefaultParameter ( Parameters, defaultParameterValue, commandLineProvider, properties, result );
20+
1821
var processedParameters = new HashSet<FlowCommandParameter> ();
19-
var result = new T ();
2022

2123
foreach ( var parameter in parameters ) {
2224
var property = FlowPropertyMapper.GetPropertyFromParameter ( parameter.Value, properties );
@@ -27,7 +29,7 @@ public T MapParametersToType ( Dictionary<string, string> values, ICommandLinePr
2729
if ( isProcessed ) processedParameters.Add ( parameter.Value );
2830
}
2931

30-
var requiredParameters = Parameters.Where ( a => a.Required ).ToList ();
32+
var requiredParameters = Parameters.Where ( a => a.Required && !a.Default ).ToList ();
3133
if ( requiredParameters.Intersect ( processedParameters ).Count () != requiredParameters.Count ) {
3234
commandLineProvider.WriteLine ( "Not all required options is defined!" );
3335
throw new Exception ( "Not all required options is defined!" );

src/FlowCommandLine/FlowPropertyMapper.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ public static Dictionary<string, FlowCommandParameter> ParametersToDictionary (
2727
return result;
2828
}
2929

30+
public static void FillDefaultParameter<T> ( IEnumerable<FlowCommandParameter> parameters, string defaultParameterValue, ICommandLineProvider commandLineProvider, IEnumerable<PropertyInfo> properties, T model ) {
31+
var defaultParameter = parameters.Where ( a => a.Default ).FirstOrDefault ();
32+
if ( defaultParameter != null ) {
33+
if ( string.IsNullOrEmpty ( defaultParameterValue ) ) {
34+
commandLineProvider.WriteLine ( "Not all required parameters is defined!" );
35+
throw new Exception ( "Not all required parameters is defined!" );
36+
}
37+
var property = GetPropertyFromParameter ( defaultParameter, properties );
38+
var defaultValue = new Dictionary<string, string> { ["default"] = defaultParameterValue };
39+
if ( property != null ) SetPropertyValue ( property.PropertyType, defaultValue, "default", model, property );
40+
}
41+
}
42+
3043
public static bool SetPropertyValue<T> ( Type type, Dictionary<string, string> values, string parameterKey, T model, PropertyInfo property ) {
3144
bool isChanged = false;
3245
switch ( type ) {

0 commit comments

Comments
 (0)