dotnet / dotnet/command-line-api

Suggestion: what about a minimal API like configuration?

Open
#1,637 0 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

Hi,
this is a suggestion! But with some minimal set of extensions, I could use your library and produce a setup like this. The only difference or caveat compared with the host builder extensions/setup you already have, is that the setup/command builder will not filter out the argument it has mapped, before passing it to the host builder. But for me, this was no issue. Let me know what you think. This is just a suggestion, no critique on the great work the team is delivering.

```
var builder = Host.CreateDefaultBuilder(args)
.ConfigureServices((c, s) => s.AddYourServicesHere(c));
var app = builder.Build();

await app.AddCommandLine(builder => builder
.AddCommand("command", "Description etc...", command => command
.HandledBy((SomeService service) => service.ExecuteAsync()))
.AddCommand("command2", "Description etc...", command => command
.WithOptions(new
{
option1 = new Option(new[] { "-o", "--option1" }, description: "Some option"),
option2 = new Option(new[] { "-f", "--option2" }, getDefaultValue: () => false, description: "More info")
})
.HandledBy((SomeService2 service, [FromCommand] string option1, [FromCommand] bool option2) => service.Execute(option1, option2)))
.AddCommand("command3", "Description etc... ", command => command
.WithGlobalOptions(new
{
input = new Option(new[] { "-i", "--input" }, () => "BulkOperations\\new-auditors.csv", "The csv file with the required information for the operation"),
output = new Option(new[] { "-o", "--output" }, () => "result", "A drop folder for generated stuff")
})
.AddCommand("subcommand", "Description", subCommand => subCommand
.HandledBy(([FromCommand] string input, [FromCommand] string output) => { throw new NotImplementedException(); }))))
.UseDefaults()
.Build()
.InvokeAsync(args);
```

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.