Skip to content

Commit a7fef0e

Browse files
Aymen TROUDIAymen TROUDI
authored andcommitted
Small improvements
1 parent e117cf2 commit a7fef0e

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

03-Way/Commands/AbstractCommand.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,28 @@ protected AbstractCommand(string name) : base(name)
1212

1313
public void AddHandler<T>(Action<T> action)
1414
{
15-
AddArgument<T>(action);
15+
AddArgument(action);
1616
AddHandler(CommandHandler.Create(action));
1717
}
1818

1919
private void AddArgument<T>(Action<T> action)
2020
{
2121
var methodInfo = action.GetMethodInfo();
2222
var argumentName = GetArgumentName(methodInfo);
23-
AddArgument(new Argument<T>(argumentName));
23+
if (!string.IsNullOrWhiteSpace(argumentName))
24+
{
25+
AddArgument(new Argument<T>(argumentName));
26+
}
2427
}
2528

2629
private void AddHandler(ICommandHandler handler)
2730
{
2831
Handler = handler;
2932
}
3033

31-
private static string GetArgumentName(MethodBase method, int index = 0)
34+
private static string? GetArgumentName(MethodBase method, int index = 0)
3235
{
33-
var name = string.Empty;
34-
35-
if (index >= 0 && method.GetParameters().Length > index)
36-
{
37-
name = method.GetParameters()[index].Name;
38-
}
39-
40-
return name!;
36+
var parameters = method.GetParameters();
37+
return parameters.Any() ? parameters[index].Name : null;
4138
}
4239
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Exploring ways of using System.CommandLine to build CLI tools
66
```
77

8-
In this repo, i m exploring various ways in order to build a simple cli tool based on [System.CommandLine](https://github.com/dotnet/command-line-api)
8+
In this repo, i m exploring various ways in order to build a simple cli tool based on [System.CommandLine](https://github.com/dotnet/command-line-api).
99
>
1010
> :one: straightforward way
1111
> - pros : simplicity

0 commit comments

Comments
 (0)