dotnet / dotnet/command-line-api

How to get `Command` when setting up host.

Open
#2,459 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

What is the easiest way to get `Command` and its `Options` or `Arguments` when configuring the host?

is this the best way?
```csharp
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;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.