Skip to content

Instantly share code, notes, and snippets.

@carmeleve
Last active August 10, 2020 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carmeleve/e53687fc02d2d8e94b4ad3a746811dfa to your computer and use it in GitHub Desktop.
Save carmeleve/e53687fc02d2d8e94b4ad3a746811dfa to your computer and use it in GitHub Desktop.
public class GreetCommand : Command
{
private readonly GreetOptions options;
public GreetCommand(GreetOptions options)
: base("greet", "Says a greeting to the specified person.")
{
var name = new Option<string>("--name")
{
Name = "name",
Description = "The name of the person to greet.",
IsRequired = true
};
this.AddOption(name);
this.Handler = CommandHandler.Create(
(string name) => this.HandleCommand(name));
this.options = options;
}
private int HandleCommand(string name)
{
try
{
Console.WriteLine($"{this.options.Greeting} {name}!");
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 0;
}
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment