Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
internal static class Program
{
/// <summary>
/// The entry point for the program.
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns>When complete, an integer representing success (0) or failure (non-0).</returns>
public static async Task<int> Main(string[] args)
{
ServiceProvider serviceProvider = BuildServiceProvider();
Parser parser = BuildParser(serviceProvider);
return await parser.InvokeAsync(args).ConfigureAwait(false);
}
private static Parser BuildParser(ServiceProvider serviceProvider)
{
var commandLineBuilder = new CommandLineBuilder();
foreach (Command command in serviceProvider.GetServices<Command>())
{
commandLineBuilder.AddCommand(command);
}
return commandLineBuilder.UseDefaults().Build();
}
private static ServiceProvider BuildServiceProvider()
{
var services = new ServiceCollection();
IConfigurationRoot config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
services.AddSingleton<IConfiguration>(config);
services.AddCliCommands();
return services.BuildServiceProvider();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment