Last active
March 19, 2024 08:31
-
-
Save fredrikhr/c9509ff5ce2071880f987d770097c843 to your computer and use it in GitHub Desktop.
Using BindCommandLine for options in dotnet/command-line-api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.CommandLine; | |
using System.CommandLine.Binding; | |
using System.CommandLine.Builder; | |
using System.CommandLine.Hosting; | |
using System.CommandLine.Invocation; | |
using System.CommandLine.IO; | |
using System.CommandLine.Parsing; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using Microsoft.Extensions.Options; | |
namespace DotnetCommandLineApiBindCommandLine | |
{ | |
public static class Program | |
{ | |
public static Task<int> Main(string[] args) | |
{ | |
var app = new RootCommand { Handler = Handler }; | |
var settingOption = new Option<string>("--setting"); | |
app.AddOption(settingOption); | |
var optionsBinder = new ModelBinder<MyOptions> | |
{ EnforceExplicitBinding = true }; | |
optionsBinder.BindMemberFromValue(o => o.MySetting, settingOption); | |
var parser = new CommandLineBuilder(app) | |
.UseDefaults() | |
.UseHost(Host.CreateDefaultBuilder, host => | |
{ | |
host.ConfigureServices(services => | |
{ | |
services.AddSingleton(optionsBinder); | |
services.AddOptions<MyOptions>() | |
.BindCommandLine(); | |
}); | |
}) | |
.Build(); | |
return parser.InvokeAsync(args ?? Array.Empty<string>()); | |
} | |
internal static ICommandHandler Handler { get; } = CommandHandler.Create( | |
(IConsole console, IHost host, CancellationToken cancelToken) => | |
{ | |
var options = host.Services.GetRequiredService<IOptions<MyOptions>>() | |
.Value; | |
console.Out.WriteLine(options.MySetting); | |
}); | |
} | |
public class MyOptions | |
{ | |
public string MySetting { get; set; } | |
} | |
} |
millokeller
commented
Mar 19, 2024
via email
Hi Fredrik
Not at all. This Gist is the very best example I could find, that properly shows how to use dependency injection in combination with the System.CommandLine API. The demonstrated pattern is still valid for the latest pre-release (2.0.0-beta4.22272.1).
The mistake was on my side. For some reason, the name of the property of the custom options class had to match the name of the alias of the option. God knows why.
Thanks for your help.
Millo
…On Monday, March 18th, 2024 at 22:25, Fredrik Høisæther Rasch ***@***.***> wrote:
@fredrikhr commented on this gist.
---------------------------------------------------------------
Hi,
I am not sure, but this is likely because the System.CommandLine API has evolved since I originally wrote this Gist. It would not surprise me if the code is quite outdated. Maybe I should figure out an update to match the current state of the System.CommandLine API.
From: millokeller ***@***.***>
Sent: søndag 17. mars 2024 16:58
To: millokeller ***@***.***>
Cc: Author ***@***.***>
Subject: Re: fredrikhr/DotnetCommandLineApiBindCommandLine.cs
@millokeller commented on this gist.
_____
Great code!
For some reason I can't use BindMemberFromValue() for boolean options. I tried a couple of hours and I can't figure out why. Do you have an idea what I am missing?
—
Reply to this email directly, view it on GitHub <https://gist.github.com/fredrikhr/c9509ff5ce2071880f987d770097c843#gistcomment-4991215> or unsubscribe <https://github.com/notifications/unsubscribe-auth/ACC2TDK7GXVXNOZYPZRG23LYYW4PTBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTANJUGQ4TCMZXU52HE2LHM5SXFJTDOJSWC5DF> .
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS <https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675> or Android <https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub> .
—
Reply to this email directly, [view it on GitHub](https://gist.github.com/fredrikhr/c9509ff5ce2071880f987d770097c843#gistcomment-4992725) or [unsubscribe](https://github.com/notifications/unsubscribe-auth/AD67RI2LJAXE4W3JZPQB7ZDYY5LT5BFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVEYTANJUGQ4TCMZXU52HE2LHM5SXFJTDOJSWC5DF).
You are receiving this email because you were mentioned.
Triage notifications on the go with GitHub Mobile for [iOS](https://apps.apple.com/app/apple-store/id1477376905) or [Android](https://play.google.com/store/apps/details?id=com.github.android).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment