CLI Settings Validation #217
-
Does the CLI framework contain a way to validate required settings properties before a command is executed? I have a number of commands with required and optional parameters, but it's not clear how to enforce that behavior. Any guidance would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I did find that there's a
|
Beta Was this translation helpful? Give feedback.
-
@turp Here is an example of how to validate arguments/options, and how to set arguments as optional/required and option values as optional/required. Reach out if you have any questions. Below the code example, there are some images showing the results of different operations. Codeusing Spectre.Console;
using Spectre.Console.Cli;
using System;
using System.ComponentModel;
namespace ConsoleApp9
{
public class Program
{
public static int Main(string[] args)
{
var app = new CommandApp<MyCommand>();
return app.Run(args);
}
}
public sealed class MyCommand : Command<MyCommandSettings>
{
// Validate as part of the command.
// This is a good way of validating options if you
// require any injected services.
public override ValidationResult Validate(CommandContext context, MyCommandSettings settings)
{
if (settings.RequiredArgument == "Hello")
{
return ValidationResult.Error("Required argument cannot be 'Hello'");
}
return ValidationResult.Success();
}
public override int Execute(CommandContext context, MyCommandSettings settings)
{
Console.WriteLine("RequiredArgument: {0}", settings.RequiredArgument);
Console.WriteLine("OptionalArgument: {0}", settings.OptionalArgument);
Console.WriteLine("OptionWithRequiredValue: {0}", settings.OptionWithRequiredValue);
Console.WriteLine("OptionWithOptionalValue: {0}", settings.OptionWithOptionalValue);
return 0;
}
}
public sealed class MyCommandSettings : CommandSettings
{
[CommandArgument(0, "<FOO>")]
[Description("An argument that is required")]
public string RequiredArgument { get; set; }
[CommandArgument(1, "[BAR]")]
[Description("An argument that is completely optional.")]
public string OptionalArgument { get; set; }
[CommandOption("--required <BAZ>")]
[Description("An option with a required value. The option is not required per se.")]
public string OptionWithRequiredValue { get; set; }
[CommandOption("--optional [QUX]")]
[Description("An option with an optional value.")]
public FlagValue<string> OptionWithOptionalValue { get; set; }
// Validate as part of the setting.
// This is a good way of validating options if you don't
// require any injected services.
public override ValidationResult Validate()
{
if (OptionWithRequiredValue == "Hello")
{
return ValidationResult.Error(
"Required option ([yellow]--required[/]) cannot be 'Hello'");
}
return ValidationResult.Success();
}
}
} Examples |
Beta Was this translation helpful? Give feedback.
@turp Here is an example of how to validate arguments/options, and how to set arguments as optional/required and option values as optional/required. Reach out if you have any questions. Below the code example, there are some images showing the results of different operations.
Code