command-line-api icon indicating copy to clipboard operation
command-line-api copied to clipboard

How to get `Command` when setting up host.

Open cschuchardt88 opened this issue 1 year ago • 0 comments

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;
}

cschuchardt88 avatar Jul 22 '24 09:07 cschuchardt88