Skip to content

Commit 1c632a1

Browse files
committed
cleanup: obsolete async API that doesn't have Async in the name
1 parent 988c426 commit 1c632a1

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

docs/samples/custom-conventions/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void Apply(ConventionContext context)
7171
}
7272

7373
// when this subcommand is selected, invoke the method on the model instance
74-
cmd.OnExecute(async () =>
74+
cmd.OnExecuteAsync(async cancellationToken =>
7575
{
7676
// get an instance of the model type from CommandLineApplication<TModel>
7777
var modelInstance = context.ModelAccessor.GetModel();

docs/samples/helloworld-async/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static int Main(string[] args)
1919
var optionSubject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);
2020
var optionRepeat = app.Option<int>("-n|--count <N>", "Repeat", CommandOptionType.SingleValue);
2121

22-
app.OnExecute(async () =>
22+
app.OnExecuteAsync(async cancellationToken =>
2323
{
2424
var subject = optionSubject.HasValue()
2525
? optionSubject.Value()
@@ -31,7 +31,7 @@ public static int Main(string[] args)
3131
Console.Write($"Hello");
3232

3333
// This pause here is just for indication that some awaitable operation could happens here.
34-
await Task.Delay(5000);
34+
await Task.Delay(5000, cancellationToken);
3535
Console.WriteLine($" {subject}!");
3636
}
3737
});

src/CommandLineUtils/CommandLineApplication.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,22 @@ private void AddArgument(CommandArgument argument)
668668
/// <param name="invoke"></param>
669669
public void OnExecute(Func<int> invoke)
670670
{
671-
_action = _ => Task.FromResult(invoke());
671+
_handler = _ => Task.FromResult(invoke());
672672
}
673673

674674
/// <summary>
675+
/// <para>
676+
/// This method is obsolete and will be removed in a future version.
677+
/// The recommended alternative is <see cref="OnExecuteAsync" />.
678+
/// </para>
679+
/// <para>
675680
/// Defines an asynchronous callback.
681+
/// </para>
676682
/// </summary>
677683
/// <param name="invoke"></param>
684+
[Obsolete("This method is obsolete and will be removed in a future version. " +
685+
"The recommended replacement is .OnExecuteAsync()")]
686+
[EditorBrowsable(EditorBrowsableState.Never)]
678687
public void OnExecute(Func<Task<int>> invoke) => OnExecuteAsync(_ => invoke());
679688

680689
/// <summary>

src/CommandLineUtils/CommandLineApplicationExtensions.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// This file has been modified from the original form. See Notice.txt in the project root for more information.
44

55
using System;
6+
using System.ComponentModel;
67
using System.ComponentModel.DataAnnotations;
78
using System.Reflection;
9+
using System.Threading;
810
using System.Threading.Tasks;
911

1012
namespace McMaster.Extensions.CommandLineUtils
@@ -95,14 +97,37 @@ public static CommandOption VerboseOption(this CommandLineApplication app, strin
9597
=> app.Option(template, "Show verbose output", CommandOptionType.NoValue, inherited: true);
9698

9799
/// <summary>
98-
/// Sets <see cref="CommandLineApplication.Invoke"/> with a return code of <c>0</c>.
100+
/// <para>
101+
/// This method is obsolete and will be removed in a future version.
102+
/// The recommended alternative is <see cref="OnExecuteAsync" />.
103+
/// </para>
104+
/// <para>
105+
/// Sets an async handler with a return code of <c>0</c>.
106+
/// </para>
99107
/// </summary>
100108
/// <param name="app"></param>
101109
/// <param name="action">An asynchronous action to invoke when the ocmmand is selected..</param>
110+
[Obsolete("This method is obsolete and will be removed in a future version. " +
111+
"The recommended replacement is .OnExecuteAsync()")]
112+
[EditorBrowsable(EditorBrowsableState.Never)]
102113
public static void OnExecute(this CommandLineApplication app, Func<Task> action)
103-
=> app.OnExecute(async () =>
114+
{
115+
app.OnExecute(async () =>
116+
{
117+
await action();
118+
return 0;
119+
});
120+
}
121+
122+
/// <summary>
123+
/// Sets an async handler with a return code of <c>0</c>.
124+
/// </summary>
125+
/// <param name="app"></param>
126+
/// <param name="action">An asynchronous action to invoke when the ocmmand is selected..</param>
127+
public static void OnExecuteAsync(this CommandLineApplication app, Func<CancellationToken, Task> action)
128+
=> app.OnExecuteAsync(async ct =>
104129
{
105-
await action();
130+
await action(ct);
106131
return 0;
107132
});
108133

0 commit comments

Comments
 (0)