-
Notifications
You must be signed in to change notification settings - Fork 479
Expand file tree
/
Copy pathParserResultExtensionsAsync.cs
More file actions
65 lines (62 loc) · 2.93 KB
/
ParserResultExtensionsAsync.cs
File metadata and controls
65 lines (62 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace CommandLine
{
public static partial class ParserResultExtensions
{
#if !NET40
/// <summary>
/// Executes asynchronously <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 as a <see cref="Task"/> instance.</returns>
public static async Task<ParserResult<T>> WithParsedAsync<T>(this ParserResult<T> result, Func<T, Task> action)
{
if (result is Parsed<T> parsed)
{
await action(parsed.Value);
}
return result;
}
/// <summary>
/// Executes asynchronously <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 as a <see cref="Task"/> instance.</returns>
public static async Task<ParserResult<object>> WithParsedAsync<T>(this ParserResult<object> result, Func<T, Task> action)
{
if (result is Parsed<object> parsed)
{
if (parsed.Value is T value)
{
await action(value);
}
}
return result;
}
/// <summary>
/// Executes asynchronously <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{Task}"/> delegate to execute.</param>
/// <returns>The same <paramref name="result"/> instance as a <see cref="Task"/> instance.</returns>
public static async Task<ParserResult<T>> WithNotParsedAsync<T>(this ParserResult<T> result, Func<IEnumerable<Error>, Task> action)
{
if (result is NotParsed<T> notParsed)
{
await action(notParsed.Errors);
}
return result;
}
#endif
}
}