show help if validate fail #1451
-
Hello everyone, I was unable to find an example of argument validation that fits my needs. I need to define some CommandOptions as required and others as optional. In case the user does not enter a required option, I would like to show them the help for using the entire command. Is there a way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Yes, it's done out of the box by simply using spectre.console Here's an example application with a single command and a mandatory argument: namespace ConsoleApp1
{
public sealed class DogSettings : CommandSettings
{
[CommandArgument(0, "<AGE>")]
public int Age { get; set; }
}
public class DefaultCommand : Command<DogSettings>
{
public override int Execute(CommandContext context, DogSettings settings)
{
AnsiConsole.WriteLine($"Dog age {settings.Age}");
return 0;
}
}
public class Program
{
static void Main(string[] args)
{
var app = new CommandApp<DefaultCommand>();
app.Run(args);
}
}
} I run it from the console by executing There are heaps of other examples located here, that you might find useful: https://github.com/spectreconsole/spectre.console/tree/main/examples |
Beta Was this translation helpful? Give feedback.
Yes, it's done out of the box by simply using spectre.console
Here's an example application with a single command and a mandatory argument: