/// <summary> /// Attempts to collect user input as a strongly-typed value and passes it to /// the specified <paramref name="action"/>, or reports an error if the method fails. /// </summary> /// <typeparam name="T">The conversion type.</typeparam> /// <param name="console">The used <see cref="SmartConsole"/>.</param> /// <param name="action">The action that the retrieved value is passed to.</param> /// <param name="onError">A callback delegate to invoke when an exception is catched.</param> /// <param name="validator">A function that further restricts or validates user input.</param> /// <param name="validationMessage">A message to display if the user enters an invalid response.</param> /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns> public static SmartConsole TrySetResponse <T>(this SmartConsole console, Action <T> action, Action <Exception> onError = null, Func <T, bool> validator = null, string validationMessage = null) { try { return(console.SetResponse(action, validator, validationMessage)); } catch (Exception ex) { onError?.Invoke(ex); return(console); } }
/// <summary> /// Attempts to collect user input as a strongly-typed value and passes it /// to the specified <paramref name="action"/>, or reports an error of type /// <typeparamref name="TException"/> if the method fails. Other exception /// types are rethrown. /// </summary> /// <typeparam name="T">The conversion type.</typeparam> /// <typeparam name="TException">The type of exception to handle.</typeparam> /// <param name="console">The used <see cref="SmartConsole"/>.</param> /// <param name="action">The action that the retrieved value is passed to.</param> /// <param name="onError">A callback delegate to invoke when an exception is catched.</param> /// <param name="validator">A function that further restricts or validates user input.</param> /// <param name="validationMessage">A message to display if the user enters an invalid response.</param> /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns> public static SmartConsole TrySetResponse <T, TException>(this SmartConsole console, Action <T> action, Action <TException> onError = null, Func <T, bool> validator = null, string validationMessage = null) where TException : Exception { try { return(console.SetResponse(action, validator, validationMessage)); } catch (Exception ex) { if (ex is TException error) { onError?.Invoke(error); return(console); } else { throw; } } }
/// <summary> /// Collects user input as a string and passes it to the specified /// <paramref name="action"/>. /// </summary> /// <param name="console">The used <see cref="SmartConsole"/>.</param> /// <param name="action">The action that the retrieved value is passed to.</param> /// <param name="validator">A function that further restricts or validates user input.</param> /// <param name="validationMessage">A message to display if the user enters an invalid response.</param> /// <returns>A reference to the current <see cref="SmartConsole" /> instance.</returns> public static SmartConsole SetResponse(this SmartConsole console, Action <string> action, Func <string, bool> validator = null, string validationMessage = null) => console.SetResponse(action, validator, validationMessage);