Skip to content

Instantly share code, notes, and snippets.

@neuecc
Created December 29, 2021 17:11
Show Gist options
  • Select an option

  • Save neuecc/a503e34d92611710f7affc65957b47a1 to your computer and use it in GitHub Desktop.

Select an option

Save neuecc/a503e34d92611710f7affc65957b47a1 to your computer and use it in GitHub Desktop.
// You can use full feature of Generic Host(same as ASP.NET Core).
var builder = ConsoleApp.CreateBuilder(args);
builder.ConfigureServices((ctx,services) =>
{
// Register EntityFramework database context
services.AddDbContext<MyDbContext>();
// Register appconfig.json to IOption<MyConfig>
services.Configure<MyConfig>(ctx.Configuration);
});
var app = builder.Build();
// setup many command, async, short-name/description option, subcommand, DI
app.AddCommand("calc-sum", (int x, int y) => Console.WriteLine(x + y));
app.AddCommand("sleep", async ([Option("t", "seconds of sleep time.")] int time) =>
{
await Task.Delay(TimeSpan.FromSeconds(time));
});
app.AddSubCommand("verb", "childverb", () => Console.WriteLine("called via 'verb childverb'"));
// You can insert all public methods as sub command.
app.AddSubCommands<DatabaseApp>();
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment