Skip to content

Add async API for a few extension methods #474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/CommandLine/ParserResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CommandLine
{
Expand Down Expand Up @@ -48,6 +49,48 @@ public static ParserResult<object> WithParsed<T>(this ParserResult<object> resul
return result;
}

#if !NET40

/// <summary>
/// Executes an async <paramref name="action"/> if <see cref="CommandLine.ParserResult{T}"/> contains
/// parsed values.
/// </summary>
/// <typeparam name="T">Type of the target instance built with parsed value.</typeparam>
/// <param name="result">An <see cref="CommandLine.ParserResult{T}"/> instance.</param>
/// <param name="action">The <see cref="Func{T, Task}"/> to execute.</param>
/// <returns>The same <paramref name="result"/> instance.</returns>
public static async Task<ParserResult<T>> WithParsedAsync<T>(this ParserResult<T> result, Func<T, Task> action)
{
var parsed = result as Parsed<T>;
if (parsed != null)
{
await action(parsed.Value);
}
return result;
}

/// <summary>
/// Executes an async <paramref name="action"/> if parsed values are of <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">Type of the target instance built with parsed value.</typeparam>
/// <param name="result">An verb result instance.</param>
/// <param name="action">The <see cref="Func{T, Task}"/> to execute.</param>
/// <returns>The same <paramref name="result"/> instance.</returns>
public static async Task<ParserResult<object>> WithParsedAsync<T>(this ParserResult<object> result, Func<T, Task> action)
{
var parsed = result as Parsed<object>;
if (parsed != null)
{
if (parsed.Value is T)
{
await action((T)parsed.Value);
}
}
return result;
}

#endif

/// <summary>
/// Executes <paramref name="action"/> if <see cref="CommandLine.ParserResult{T}"/> lacks
/// parsed values and contains errors.
Expand All @@ -66,6 +109,28 @@ public static ParserResult<T> WithNotParsed<T>(this ParserResult<T> result, Acti
return result;
}

#if !NET40

/// <summary>
/// Executes an async <paramref name="action"/> if <see cref="CommandLine.ParserResult{T}"/> lacks
/// parsed values and contains errors.
/// </summary>
/// <typeparam name="T">Type of the target instance built with parsed value.</typeparam>
/// <param name="result">An <see cref="CommandLine.ParserResult{T}"/> instance.</param>
/// <param name="action">The <see cref="System.Func{T, TResult}"/> delegate to execute.</param>
/// <returns>The same <paramref name="result"/> instance.</returns>
public static async Task<ParserResult<T>> WithNotParsedAsync<T>(this ParserResult<T> result, Func<IEnumerable<Error>, Task> action)
{
var notParsed = result as NotParsed<T>;
if (notParsed != null)
{
await action(notParsed.Errors);
}
return result;
}

#endif

/// <summary>
/// Provides a way to transform result data into another value.
/// </summary>
Expand Down