Skip to content

Commit dae3081

Browse files
committed
Added async versions of WithParsed extension methods
1 parent 12aac09 commit dae3081

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

src/CommandLine/ParserResultExtensions.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Threading.Tasks;
56

67
namespace CommandLine
78
{
@@ -28,6 +29,24 @@ public static ParserResult<T> WithParsed<T>(this ParserResult<T> result, Action<
2829
return result;
2930
}
3031

32+
/// <summary>
33+
/// Executes asynchronously <paramref name="action"/> if <see cref="CommandLine.ParserResult{T}"/> contains
34+
/// parsed values.
35+
/// </summary>
36+
/// <typeparam name="T">Type of the target instance built with parsed value.</typeparam>
37+
/// <param name="result">An <see cref="CommandLine.ParserResult{T}"/> instance.</param>
38+
/// <param name="action">The <see cref="Func{T, Task}"/> to execute.</param>
39+
/// <returns>The same <paramref name="result"/> instance as a <see cref="Task"/> instance.</returns>
40+
public static async Task<ParserResult<T>> WithParsedAsync<T>(this ParserResult<T> result, Func<T, Task> action)
41+
{
42+
if (result is Parsed<T> parsed)
43+
{
44+
await action(parsed.Value);
45+
}
46+
return result;
47+
}
48+
49+
3150
/// <summary>
3251
/// Executes <paramref name="action"/> if parsed values are of <typeparamref name="T"/>.
3352
/// </summary>
@@ -48,6 +67,26 @@ public static ParserResult<object> WithParsed<T>(this ParserResult<object> resul
4867
return result;
4968
}
5069

70+
/// <summary>
71+
/// Executes asynchronously <paramref name="action"/> if parsed values are of <typeparamref name="T"/>.
72+
/// </summary>
73+
/// <typeparam name="T">Type of the target instance built with parsed value.</typeparam>
74+
/// <param name="result">An verb result instance.</param>
75+
/// <param name="action">The <see cref="Func{T, Task}"/> to execute.</param>
76+
/// <returns>The same <paramref name="result"/> instance as a <see cref="Task"/> instance.</returns>
77+
public static async Task<ParserResult<object>> WithParsedAsync<T>(this ParserResult<object> result, Func<T, Task> action)
78+
{
79+
if (result is Parsed<object> parsed)
80+
{
81+
if (parsed.Value is T)
82+
{
83+
await action((T)parsed.Value);
84+
}
85+
}
86+
return result;
87+
}
88+
89+
5190
/// <summary>
5291
/// Executes <paramref name="action"/> if <see cref="CommandLine.ParserResult{T}"/> lacks
5392
/// parsed values and contains errors.
@@ -57,11 +96,28 @@ public static ParserResult<object> WithParsed<T>(this ParserResult<object> resul
5796
/// <param name="action">The <see cref="System.Action"/> delegate to execute.</param>
5897
/// <returns>The same <paramref name="result"/> instance.</returns>
5998
public static ParserResult<T> WithNotParsed<T>(this ParserResult<T> result, Action<IEnumerable<Error>> action)
99+
{
100+
if (result is NotParsed<T> notParsed)
101+
{
102+
action(notParsed.Errors);
103+
}
104+
return result;
105+
}
106+
107+
/// <summary>
108+
/// Executes asynchronously <paramref name="action"/> if <see cref="CommandLine.ParserResult{T}"/> lacks
109+
/// parsed values and contains errors.
110+
/// </summary>
111+
/// <typeparam name="T">Type of the target instance built with parsed value.</typeparam>
112+
/// <param name="result">An <see cref="CommandLine.ParserResult{T}"/> instance.</param>
113+
/// <param name="action">The <see cref="System.Func{Task}"/> delegate to execute.</param>
114+
/// <returns>The same <paramref name="result"/> instance as a <see cref="Task"/> instance.</returns>
115+
public static async Task<ParserResult<T>> WithNotParsedAsync<T>(this ParserResult<T> result, Func<IEnumerable<Error>, Task> action)
60116
{
61117
var notParsed = result as NotParsed<T>;
62118
if (notParsed != null)
63119
{
64-
action(notParsed.Errors);
120+
await action(notParsed.Errors);
65121
}
66122
return result;
67123
}

0 commit comments

Comments
 (0)