From abce656d679383e1da2fba3d7dacd3be728d93a3 Mon Sep 17 00:00:00 2001 From: David Pine Date: Thu, 11 Jul 2019 15:05:59 -0500 Subject: [PATCH 1/2] Add async API for a few extension methods, allowing task-based Main entry point. --- src/CommandLine/ParserResultExtensions.cs | 57 +++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/CommandLine/ParserResultExtensions.cs b/src/CommandLine/ParserResultExtensions.cs index 66201dbb..0933d805 100644 --- a/src/CommandLine/ParserResultExtensions.cs +++ b/src/CommandLine/ParserResultExtensions.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace CommandLine { @@ -48,6 +49,44 @@ public static ParserResult WithParsed(this ParserResult resul return result; } + /// + /// Executes an async if contains + /// parsed values. + /// + /// Type of the target instance built with parsed value. + /// An instance. + /// The to execute. + /// The same instance. + public static async Task> WithParsedAsync(this ParserResult result, Func action) + { + var parsed = result as Parsed; + if (parsed != null) + { + await action(parsed.Value); + } + return result; + } + + /// + /// Executes an async if parsed values are of . + /// + /// Type of the target instance built with parsed value. + /// An verb result instance. + /// The to execute. + /// The same instance. + public static async Task> WithParsedAsync(this ParserResult result, Func action) + { + var parsed = result as Parsed; + if (parsed != null) + { + if (parsed.Value is T) + { + await action((T)parsed.Value); + } + } + return result; + } + /// /// Executes if lacks /// parsed values and contains errors. @@ -66,6 +105,24 @@ public static ParserResult WithNotParsed(this ParserResult result, Acti return result; } + /// + /// Executes an async if lacks + /// parsed values and contains errors. + /// + /// Type of the target instance built with parsed value. + /// An instance. + /// The delegate to execute. + /// The same instance. + public static async Task> WithNotParsedAsync(this ParserResult result, Func, Task> action) + { + var notParsed = result as NotParsed; + if (notParsed != null) + { + await action(notParsed.Errors); + } + return result; + } + /// /// Provides a way to transform result data into another value. /// From c9f7ab6bce40a094801d845a3ee3954627ff86c3 Mon Sep 17 00:00:00 2001 From: David Pine Date: Thu, 11 Jul 2019 15:18:11 -0500 Subject: [PATCH 2/2] Added compiler preprocessor checks for NET40 that doesn't support async --- src/CommandLine/ParserResultExtensions.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/CommandLine/ParserResultExtensions.cs b/src/CommandLine/ParserResultExtensions.cs index 0933d805..bc9326ea 100644 --- a/src/CommandLine/ParserResultExtensions.cs +++ b/src/CommandLine/ParserResultExtensions.cs @@ -49,6 +49,8 @@ public static ParserResult WithParsed(this ParserResult resul return result; } +#if !NET40 + /// /// Executes an async if contains /// parsed values. @@ -87,6 +89,8 @@ public static async Task> WithParsedAsync(this ParserRes return result; } +#endif + /// /// Executes if lacks /// parsed values and contains errors. @@ -105,6 +109,8 @@ public static ParserResult WithNotParsed(this ParserResult result, Acti return result; } +#if !NET40 + /// /// Executes an async if lacks /// parsed values and contains errors. @@ -123,6 +129,8 @@ public static async Task> WithNotParsedAsync(this ParserResul return result; } +#endif + /// /// Provides a way to transform result data into another value. ///