using System;
namespace Spectre.Console.Cli
{
///
/// Contains extensions for
/// and .
///
public static class ConfiguratorExtensions
{
///
/// 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;
}
///
/// 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 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.
public static void AddBranch(
this IConfigurator configurator,
string name,
Action> action)
{
if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
}
configurator.AddBranch(name, action);
}
///
/// Adds a command branch.
///
/// The command setting type.
/// The configurator.
/// The name of the command branch.
/// The command branch configuration.
public static void AddBranch(
this IConfigurator configurator,
string name,
Action> action)
where TSettings : CommandSettings
{
if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
}
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 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));
}
}
}