namespace Spectre.Console.Cli; /// /// Contains extensions for /// and . /// public static class ConfiguratorExtensions { /// /// Sets the help provider for the application. /// /// The configurator. /// The help provider to use. /// A configurator that can be used to configure the application further. public static IConfigurator SetHelpProvider(this IConfigurator configurator, IHelpProvider helpProvider) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.SetHelpProvider(helpProvider); return configurator; } /// /// Sets the help provider for the application. /// /// The configurator. /// The type of the help provider to instantiate at runtime and use. /// A configurator that can be used to configure the application further. public static IConfigurator SetHelpProvider(this IConfigurator configurator) where T : IHelpProvider { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.SetHelpProvider(); return configurator; } /// /// Sets the culture for the application. /// /// The configurator. /// The culture. /// A configurator that can be used to configure the application further. /// /// Text displayed by can be localised, but defaults to English. /// Setting the application culture informs the resource manager which culture to use when fetching strings. /// English will be used when a culture has not been specified /// or a string has not been localised for the specified culture. /// public static IConfigurator SetApplicationCulture(this IConfigurator configurator, CultureInfo? culture) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.Culture = culture; return configurator; } /// /// Sets the name of the application. /// /// The configurator. /// The name of the application. /// A configurator that can be used to configure the application further. public static IConfigurator SetApplicationName(this IConfigurator configurator, string name) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.ApplicationName = name; return configurator; } /// /// Overrides the auto-detected version of the application. /// /// The configurator. /// The version of application. /// A configurator that can be used to configure the application further. public static IConfigurator SetApplicationVersion(this IConfigurator configurator, string version) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.ApplicationVersion = version; return configurator; } /// /// Hides the DEFAULT column that lists default values coming from the /// in the options help text. /// /// The configurator. /// A configurator that can be used to configure the application further. public static IConfigurator HideOptionDefaultValues(this IConfigurator configurator) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.ShowOptionDefaultValues = false; return configurator; } /// /// Configures the console. /// /// The configurator. /// The console. /// A configurator that can be used to configure the application further. public static IConfigurator ConfigureConsole(this IConfigurator configurator, IAnsiConsole console) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.Console = console; return configurator; } /// /// Sets the parsing mode to strict. /// /// The configurator. /// A configurator that can be used to configure the application further. public static IConfigurator UseStrictParsing(this IConfigurator configurator) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.StrictParsing = true; return configurator; } /// /// Tells the help writer whether or not to trim trailing period. /// /// The configurator. /// True to trim trailing period (default), false to not. /// A configurator that can be used to configure the application further. public static IConfigurator TrimTrailingPeriods(this IConfigurator configurator, bool trimTrailingPeriods) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.TrimTrailingPeriod = trimTrailingPeriods; return configurator; } /// /// Tells the command line application to propagate all /// exceptions to the user. /// /// The configurator. /// A configurator that can be used to configure the application further. public static IConfigurator PropagateExceptions(this IConfigurator configurator) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.PropagateExceptions = true; return configurator; } /// /// Configures case sensitivity. /// /// The configuration. /// The case sensitivity. /// A configurator that can be used to configure the application further. public static IConfigurator CaseSensitivity(this IConfigurator configurator, CaseSensitivity sensitivity) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.CaseSensitivity = sensitivity; return configurator; } /// /// Tells the command line application to validate all /// examples before running the application. /// /// The configurator. /// A configurator that can be used to configure the application further. public static IConfigurator ValidateExamples(this IConfigurator configurator) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.ValidateExamples = true; return configurator; } /// /// Sets the command interceptor to be used. /// /// The configurator. /// A . /// A configurator that can be used to configure the application further. public static IConfigurator SetInterceptor(this IConfigurator configurator, ICommandInterceptor interceptor) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.Interceptor = interceptor; return configurator; } /// /// Adds a command branch. /// /// The configurator. /// The name of the command branch. /// The command branch configuration. /// A branch configurator that can be used to configure the branch further. public static IBranchConfigurator AddBranch( this IConfigurator configurator, string name, Action> action) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } return configurator.AddBranch(name, action); } /// /// Adds a command branch. /// /// The command setting type. /// The configurator. /// The name of the command branch. /// The command branch configuration. /// A branch configurator that can be used to configure the branch further. public static IBranchConfigurator AddBranch( this IConfigurator configurator, string name, Action> action) where TSettings : CommandSettings { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } return configurator.AddBranch(name, action); } /// /// Adds a command without settings that executes a delegate. /// /// The configurator. /// The name of the command. /// The delegate to execute as part of command execution. /// A command configurator that can be used to configure the command further. public static ICommandConfigurator AddDelegate( this IConfigurator configurator, string name, Func func) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } return configurator.AddDelegate(name, (c, _) => func(c)); } /// /// Adds a command without settings that executes an async delegate. /// /// The configurator. /// The name of the command. /// The delegate to execute as part of command execution. /// A command configurator that can be used to configure the command further. public static ICommandConfigurator AddAsyncDelegate( this IConfigurator configurator, string name, Func> func) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } return configurator.AddAsyncDelegate(name, (c, _) => func(c)); } /// /// Adds a command without settings that executes a delegate. /// /// The command setting type. /// The configurator. /// The name of the command. /// The delegate to execute as part of command execution. /// A command configurator that can be used to configure the command further. public static ICommandConfigurator AddDelegate( this IConfigurator configurator, string name, Func func) where TSettings : CommandSettings { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } return configurator.AddDelegate(name, (c, _) => func(c)); } /// /// Adds a command without settings that executes an async delegate. /// /// The command setting type. /// The configurator. /// The name of the command. /// The delegate to execute as part of command execution. /// A command configurator that can be used to configure the command further. public static ICommandConfigurator AddAsyncDelegate( this IConfigurator configurator, string name, Func> func) where TSettings : CommandSettings { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } return configurator.AddAsyncDelegate(name, (c, _) => func(c)); } /// /// Sets the ExceptionsHandler. /// Setting this way will use the /// default exit code of -1. /// /// The configurator. /// The Action that handles the exception. /// A configurator that can be used to configure the application further. public static IConfigurator SetExceptionHandler(this IConfigurator configurator, Action exceptionHandler) { return configurator.SetExceptionHandler(ex => { exceptionHandler(ex); return -1; }); } /// /// Sets the ExceptionsHandler. /// /// The configurator. /// The Action that handles the exception. /// A configurator that can be used to configure the application further. public static IConfigurator SetExceptionHandler(this IConfigurator configurator, Func? exceptionHandler) { if (configurator == null) { throw new ArgumentNullException(nameof(configurator)); } configurator.Settings.ExceptionHandler = exceptionHandler; return configurator; } }