36 lines
1.2 KiB
C#
36 lines
1.2 KiB
C#
using System.CommandLine;
|
|
using MycroForge.Core;
|
|
using MycroForge.Core.Contract;
|
|
using RootCommand = MycroForge.Core.RootCommand;
|
|
|
|
namespace MycroForge.PluginTemplate;
|
|
|
|
public class ExampleCommand : Command, ISubCommandOf<RootCommand>
|
|
{
|
|
private readonly Argument<string> NameArgument =
|
|
new(name: "name", description: "The name of the person to greet");
|
|
|
|
private readonly Option<bool> AllCapsOption =
|
|
new(aliases: ["-a", "--all-caps"], description: "Print the name in all caps");
|
|
|
|
private readonly ProjectContext _context;
|
|
|
|
public ExampleCommand(ProjectContext context) :
|
|
base("example", "A basic command plugin generated by the 'm4g plugin init' command")
|
|
{
|
|
_context = context;
|
|
AddArgument(NameArgument);
|
|
AddOption(AllCapsOption);
|
|
this.SetHandler(ExecuteAsync, NameArgument, AllCapsOption);
|
|
}
|
|
|
|
private async Task ExecuteAsync(string name, bool allCaps)
|
|
{
|
|
name = allCaps ? name.ToUpper() : name;
|
|
|
|
await _context.CreateFile("example.txt",
|
|
$"Hello {name}!",
|
|
"This file was generated by the 'm4g example' command!"
|
|
);
|
|
}
|
|
} |