Skip to content

Commit dbc79de

Browse files
authored
Make ICommand.Execute async
Merge pull request #83 from mgrosperrin/features/0.11.0/make-icommand-execute-async
1 parent edf6bdd commit dbc79de

25 files changed

+63
-42
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public class HelloWorldCommand : ICommand
4343

4444
public IList<string> Arguments {get; set;}
4545

46-
public int Execute()
46+
public Task<int> ExecuteAsync()
4747
{
4848
Console.WriteLine("Hello world {0} !", Name);
4949
if(Arguments.Count > 0)
5050
{
5151
Console.WriteLine("Arguments : {0}", string.Join("," Arguments));
5252
}
53-
return 0;
53+
return Task.FromResult(0);
5454
}
5555
}
5656
```
@@ -63,14 +63,14 @@ public class HelloWorldCommand : CommandBase
6363
[Required]
6464
public string Name {get; set;}
6565

66-
protected override int ExecuteCommand()
66+
protected override Task<int> ExecuteCommandasync()
6767
{
6868
Console.WriteLine("Hello world {0} !", Name);
6969
if(Arguments.Count > 0)
7070
{
7171
Console.WriteLine("Arguments : {0}", string.Join("," Arguments));
7272
}
73-
return 0;
73+
return Task.FromResult(0);
7474
}
7575
}
7676
```
@@ -96,7 +96,7 @@ IParser parser = parserBuilder.BuildParser();
9696
CommandResult<HelloWorldCommand> commandResult = parser.Parse<HelloWorldCommand>(args);
9797
if(commandResult.IsValid)
9898
{
99-
return commandResult.Execute();
99+
return commandResult.ExecuteAsync();
100100
}
101101
return commandResult.ReturnCode;
102102
```

src/MGR.CommandLineParser/Command/CommandBase.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel.DataAnnotations;
4+
using System.Threading.Tasks;
45
using JetBrains.Annotations;
56
using MGR.CommandLineParser.Extensibility;
67
using MGR.CommandLineParser.Extensibility.Command;
@@ -63,15 +64,15 @@ protected CommandBase()
6364
/// Executes the command.
6465
/// </summary>
6566
/// <returns> Return 0 is everything was right, an negative error code otherwise. </returns>
66-
public virtual int Execute()
67+
public virtual Task<int> ExecuteAsync()
6768
{
6869
if (Help)
6970
{
7071
var helpWriter = CurrentDependencyResolverScope.GetRequiredService<IHelpWriter>();
7172
helpWriter.WriteHelpForCommand(ParserOptions, CommandType);
72-
return 0;
73+
return Task.FromResult(0);
7374
}
74-
return ExecuteCommand();
75+
return ExecuteCommandAsync();
7576
}
7677

7778
/// <summary>
@@ -92,6 +93,6 @@ public virtual void Configure(IParserOptions parserOptions, IServiceProvider ser
9293
/// Executes the command.
9394
/// </summary>
9495
/// <returns> Return 0 is everything was right, an negative error code otherwise. </returns>
95-
protected abstract int ExecuteCommand();
96+
protected abstract Task<int> ExecuteCommandAsync();
9697
}
9798
}

src/MGR.CommandLineParser/Command/HelpCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using System.Threading.Tasks;
23
using JetBrains.Annotations;
34
using MGR.CommandLineParser.Extensibility;
45
using Microsoft.Extensions.DependencyInjection;
@@ -25,7 +26,7 @@ public sealed class HelpCommand : CommandBase
2526
/// Executes the command.
2627
/// </summary>
2728
/// <returns>Return 0 is everything was right, an negative error code otherwise.</returns>
28-
protected override int ExecuteCommand()
29+
protected override Task<int> ExecuteCommandAsync()
2930
{
3031
var commandTypeProvider = CurrentDependencyResolverScope.GetRequiredService<ICommandTypeProvider>();
3132
var helpWriter = CurrentDependencyResolverScope.GetRequiredService<IHelpWriter>();
@@ -38,7 +39,7 @@ protected override int ExecuteCommand()
3839
{
3940
helpWriter.WriteHelpForCommand(ParserOptions, commandType);
4041
}
41-
return 0;
42+
return Task.FromResult(0);
4243
}
4344

4445
private void WriteHelpWhenNoCommandAreSpecified(ICommandTypeProvider commandTypeProvider, IHelpWriter helpWriter)

src/MGR.CommandLineParser/Command/ICommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Threading.Tasks;
23

34
namespace MGR.CommandLineParser.Command
45
{
@@ -11,7 +12,7 @@ public interface ICommand
1112
/// Executes the command.
1213
/// </summary>
1314
/// <returns>Return 0 is everything was right, an negative error code otherwise.</returns>
14-
int Execute();
15+
Task<int> ExecuteAsync();
1516
/// <summary>
1617
/// The list of arguments of the command.
1718
/// </summary>

src/MGR.CommandLineParser/CommandResult`1.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.ComponentModel.DataAnnotations;
33
using System.Linq;
4+
using System.Threading.Tasks;
45
using MGR.CommandLineParser.Command;
56

67
namespace MGR.CommandLineParser
@@ -50,13 +51,13 @@ internal CommandResult(TCommand command, CommandResultCode returnCode, List<Vali
5051
/// </summary>
5152
/// <returns>Returns the result of the Execute method of the command.</returns>
5253
/// <exception cref="CommandLineParserException">Thrown if the underlying command is null, or if the command is in an invalid state.</exception>
53-
public int Execute()
54+
public Task<int> ExecuteAsync()
5455
{
5556
if (_command == null || !IsValid)
5657
{
5758
throw new CommandLineParserException(Constants.ExceptionMessages.NoValidCommand);
5859
}
59-
return Command.Execute();
60+
return Command.ExecuteAsync();
6061
}
6162

6263
/// <inheritdoc />

tests/MGR.CommandLineParser.IntegrationTests/UnspecifiedCommand/HelpCommandTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Threading.Tasks;
23
using MGR.CommandLineParser.Command;
34
using MGR.CommandLineParser.UnitTests;
45
using Xunit;
@@ -8,7 +9,7 @@ namespace MGR.CommandLineParser.IntegrationTests.UnspecifiedCommand
89
public class HelpCommandTests : ConsoleLoggingTestsBase
910
{
1011
[Fact]
11-
public void ShowGenericHelpForAllCommand()
12+
public async Task ShowGenericHelpForAllCommand()
1213
{
1314
// Arrange
1415
var parserBuild = new ParserBuilder()
@@ -43,7 +44,7 @@ Update UpdateCommandDescription
4344
using (new LangageSwitcher("en-us"))
4445
{
4546
var actual = parser.Parse(args);
46-
var actualResult = actual.Execute();
47+
var actualResult = await actual.ExecuteAsync();
4748

4849
// Assert
4950
Assert.True(actual.IsValid);

tests/MGR.CommandLineParser.IntegrationTests/UnspecifiedCommand/UseHelpOptionTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Threading.Tasks;
23
using MGR.CommandLineParser.Tests.Commands;
34
using MGR.CommandLineParser.UnitTests;
45
using Xunit;
@@ -8,7 +9,7 @@ namespace MGR.CommandLineParser.IntegrationTests.UnspecifiedCommand
89
public class UseHelpOptionTests : ConsoleLoggingTestsBase
910
{
1011
[Fact]
11-
public void ShowHelpForTheListCommand()
12+
public async Task ShowHelpForTheListCommand()
1213
{
1314
// Arrange
1415
var parserBuild = new ParserBuilder()
@@ -41,7 +42,7 @@ List sample number 2
4142
using (new LangageSwitcher("en-us"))
4243
{
4344
var actual = parser.Parse(args);
44-
var actualResult = actual.Execute();
45+
var actualResult = await actual.ExecuteAsync();
4546

4647
// Assert
4748
Assert.True(actual.IsValid);
@@ -54,7 +55,7 @@ List sample number 2
5455
}
5556

5657
[Fact]
57-
public void ShowHelpForThePackCommand()
58+
public async Task ShowHelpForThePackCommand()
5859
{
5960
// Arrange
6061
var parserBuild = new ParserBuilder()
@@ -91,7 +92,7 @@ public void ShowHelpForThePackCommand()
9192
using (new LangageSwitcher("en-us"))
9293
{
9394
var actual = parser.Parse(args);
94-
var actualResult = actual.Execute();
95+
var actualResult = await actual.ExecuteAsync();
9596

9697
// Assert
9798
Assert.True(actual.IsValid);

tests/MGR.CommandLineParser.Tests.Commands/DeleteCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.ComponentModel.DataAnnotations;
2+
using System.Threading.Tasks;
23
using MGR.CommandLineParser.Command;
34

45
namespace MGR.CommandLineParser.Tests.Commands
@@ -21,6 +22,6 @@ public class DeleteCommand : CommandBase
2122
[IgnoreOptionProperty]
2223
public object Settings { get; }
2324

24-
protected override int ExecuteCommand() => 0;
25+
protected override Task<int> ExecuteCommandAsync() => Task.FromResult(0);
2526
}
2627
}

tests/MGR.CommandLineParser.Tests.Commands/HiddenCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
using MGR.CommandLineParser.Command;
1+
using System.Threading.Tasks;
2+
using MGR.CommandLineParser.Command;
23

34
namespace MGR.CommandLineParser.Tests.Commands
45
{
56
[Command(Description = "HiddenCommandDescription", Usage = "HiddenCommandUsage", HideFromHelpListing = true)]
67
public class HiddenCommand : CommandBase
78
{
8-
protected override int ExecuteCommand()
9+
protected override Task<int> ExecuteCommandAsync()
910
{
1011
throw new System.NotImplementedException();
1112
}

tests/MGR.CommandLineParser.Tests.Commands/ImportCommand.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.ComponentModel;
22
using System.ComponentModel.DataAnnotations;
33
using System.IO;
4+
using System.Threading.Tasks;
45
using MGR.CommandLineParser.Command;
56

67
namespace MGR.CommandLineParser.Tests.Commands
@@ -17,9 +18,6 @@ public class ImportCommand : CommandBase
1718
[Display(ShortName = "of")]
1819
public FileInfo OutputFile { get; set; }
1920

20-
protected override int ExecuteCommand()
21-
{
22-
return 0;
23-
}
21+
protected override Task<int> ExecuteCommandAsync() => Task.FromResult(0);
2422
}
2523
}

0 commit comments

Comments
 (0)