|
| 1 | +// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information. |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Threading.Tasks; |
| 5 | + |
| 6 | +namespace CommandLine |
| 7 | +{ |
| 8 | + public static partial class ParserResultExtensions |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// Executes asynchronously <paramref name="action"/> if <see cref="CommandLine.ParserResult{T}"/> contains |
| 12 | + /// parsed values. |
| 13 | + /// </summary> |
| 14 | + /// <typeparam name="T">Type of the target instance built with parsed value.</typeparam> |
| 15 | + /// <param name="result">An <see cref="CommandLine.ParserResult{T}"/> instance.</param> |
| 16 | + /// <param name="action">The <see cref="Func{T, Task}"/> to execute.</param> |
| 17 | + /// <returns>The same <paramref name="result"/> instance as a <see cref="Task"/> instance.</returns> |
| 18 | + public static async Task<ParserResult<T>> WithParsedAsync<T>(this ParserResult<T> result, Func<T, Task> action) |
| 19 | + { |
| 20 | + if (result is Parsed<T> parsed) |
| 21 | + { |
| 22 | + await action(parsed.Value); |
| 23 | + } |
| 24 | + return result; |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Executes asynchronously <paramref name="action"/> if parsed values are of <typeparamref name="T"/>. |
| 29 | + /// </summary> |
| 30 | + /// <typeparam name="T">Type of the target instance built with parsed value.</typeparam> |
| 31 | + /// <param name="result">An verb result instance.</param> |
| 32 | + /// <param name="action">The <see cref="Func{T, Task}"/> to execute.</param> |
| 33 | + /// <returns>The same <paramref name="result"/> instance as a <see cref="Task"/> instance.</returns> |
| 34 | + public static async Task<ParserResult<object>> WithParsedAsync<T>(this ParserResult<object> result, Func<T, Task> action) |
| 35 | + { |
| 36 | + if (result is Parsed<object> parsed) |
| 37 | + { |
| 38 | + if (parsed.Value is T value) |
| 39 | + { |
| 40 | + await action(value); |
| 41 | + } |
| 42 | + } |
| 43 | + return result; |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Executes asynchronously <paramref name="action"/> if <see cref="CommandLine.ParserResult{T}"/> lacks |
| 48 | + /// parsed values and contains errors. |
| 49 | + /// </summary> |
| 50 | + /// <typeparam name="T">Type of the target instance built with parsed value.</typeparam> |
| 51 | + /// <param name="result">An <see cref="CommandLine.ParserResult{T}"/> instance.</param> |
| 52 | + /// <param name="action">The <see cref="System.Func{Task}"/> delegate to execute.</param> |
| 53 | + /// <returns>The same <paramref name="result"/> instance as a <see cref="Task"/> instance.</returns> |
| 54 | + public static async Task<ParserResult<T>> WithNotParsedAsync<T>(this ParserResult<T> result, Func<IEnumerable<Error>, Task> action) |
| 55 | + { |
| 56 | + if (result is NotParsed<T> notParsed) |
| 57 | + { |
| 58 | + await action(notParsed.Errors); |
| 59 | + } |
| 60 | + return result; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments