command-line-api
command-line-api copied to clipboard
How to get `Command` when setting up host.
What is the easiest way to get Command and its Options or Arguments when configuring the host?
is this the best way?
public static bool TryGetGlobalInputOption(this IHostBuilder hostBuilder, [NotNullWhen(true)] out string? filename)
{
var invocation = hostBuilder.Properties[typeof(InvocationContext)] as InvocationContext;
var command = invocation!.ParseResult.CommandResult.Command;
var inputOptions = command.Options.FirstOrDefault(f => f.Name == "input");
filename = null;
if (inputOptions != null)
filename = invocation.ParseResult.GetValueForOption(inputOptions) as string;
return filename != null;
}