Skip to content

Instantly share code, notes, and snippets.

@carmeleve
Created August 3, 2020 08:55
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/daed7370b8dbfe7781c6dbab2d52bf96 to your computer and use it in GitHub Desktop.
Save carmeleve/daed7370b8dbfe7781c6dbab2d52bf96 to your computer and use it in GitHub Desktop.
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