Skip to content

Commit

Permalink
cleanup: obsolete async API that doesn't have Async in the name
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Jul 24, 2019
1 parent 988c426 commit 1c632a1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/samples/custom-conventions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Apply(ConventionContext context)
}
// when this subcommand is selected, invoke the method on the model instance
cmd.OnExecute(async () =>
cmd.OnExecuteAsync(async cancellationToken =>
{
// get an instance of the model type from CommandLineApplication<TModel>
var modelInstance = context.ModelAccessor.GetModel();
Expand Down
4 changes: 2 additions & 2 deletions docs/samples/helloworld-async/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static int Main(string[] args)
var optionSubject = app.Option("-s|--subject <SUBJECT>", "The subject", CommandOptionType.SingleValue);
var optionRepeat = app.Option<int>("-n|--count <N>", "Repeat", CommandOptionType.SingleValue);

app.OnExecute(async () =>
app.OnExecuteAsync(async cancellationToken =>
{
var subject = optionSubject.HasValue()
? optionSubject.Value()
Expand All @@ -31,7 +31,7 @@ public static int Main(string[] args)
Console.Write($"Hello");
// This pause here is just for indication that some awaitable operation could happens here.
await Task.Delay(5000);
await Task.Delay(5000, cancellationToken);
Console.WriteLine($" {subject}!");
}
});
Expand Down
11 changes: 10 additions & 1 deletion src/CommandLineUtils/CommandLineApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -668,13 +668,22 @@ private void AddArgument(CommandArgument argument)
/// <param name="invoke"></param>
public void OnExecute(Func<int> invoke)
{
_action = _ => Task.FromResult(invoke());
_handler = _ => Task.FromResult(invoke());
}

/// <summary>
/// <para>
/// This method is obsolete and will be removed in a future version.
/// The recommended alternative is <see cref="OnExecuteAsync" />.
/// </para>
/// <para>
/// Defines an asynchronous callback.
/// </para>
/// </summary>
/// <param name="invoke"></param>
[Obsolete("This method is obsolete and will be removed in a future version. " +
"The recommended replacement is .OnExecuteAsync()")]
[EditorBrowsable(EditorBrowsableState.Never)]
public void OnExecute(Func<Task<int>> invoke) => OnExecuteAsync(_ => invoke());

/// <summary>
Expand Down
31 changes: 28 additions & 3 deletions src/CommandLineUtils/CommandLineApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// This file has been modified from the original form. See Notice.txt in the project root for more information.

using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

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

/// <summary>
/// Sets <see cref="CommandLineApplication.Invoke"/> with a return code of <c>0</c>.
/// <para>
/// This method is obsolete and will be removed in a future version.
/// The recommended alternative is <see cref="OnExecuteAsync" />.
/// </para>
/// <para>
/// Sets an async handler with a return code of <c>0</c>.
/// </para>
/// </summary>
/// <param name="app"></param>
/// <param name="action">An asynchronous action to invoke when the ocmmand is selected..</param>
[Obsolete("This method is obsolete and will be removed in a future version. " +
"The recommended replacement is .OnExecuteAsync()")]
[EditorBrowsable(EditorBrowsableState.Never)]
public static void OnExecute(this CommandLineApplication app, Func<Task> action)
=> app.OnExecute(async () =>
{
app.OnExecute(async () =>
{
await action();
return 0;
});
}

/// <summary>
/// Sets an async handler with a return code of <c>0</c>.
/// </summary>
/// <param name="app"></param>
/// <param name="action">An asynchronous action to invoke when the ocmmand is selected..</param>
public static void OnExecuteAsync(this CommandLineApplication app, Func<CancellationToken, Task> action)
=> app.OnExecuteAsync(async ct =>
{
await action();
await action(ct);
return 0;
});

Expand Down

0 comments on commit 1c632a1

Please sign in to comment.