Replies: 3 comments 1 reply
-
In 2.0.0-beta4.22272.1, use an overload that gives the InvocationContext to the handler: command-line-api/src/System.CommandLine/Handler.Action.cs Lines 14 to 20 in 209b724 command-line-api/src/System.CommandLine/Handler.Func.cs Lines 23 to 29 in 209b724 Then make the handler read InvocationContext.ParseResult and query the values from the ParseResult:command-line-api/src/System.CommandLine/Parsing/ParseResult.cs Lines 162 to 168 in 209b724 In later versions of System.CommandLine (which are not yet at nuget.org), the SetHandler overloads that take IValueDescriptor<T> parameters have already been deleted, but the InvocationContext approach is still available (although the type isn't called InvocationContext any more). |
Beta Was this translation helpful? Give feedback.
-
Sample from docs (#1750): rootCommand.SetHandler(
(context) => // InvocationContext, but it's inferred so you don't need to specify it.
{
var i = context.ParseResult.GetValueForOption(option);
var s = context.ParseResult.GetValueForArgument(argument);
var someDependency = context.GetService(typeof(ILogger)) as ILogger;
/* Do something exciting! */
}); |
Beta Was this translation helpful? Give feedback.
-
How about this? Your whole app: Cli.Ext.ConfigureServices(services =>
{
var loggerFactory = LoggerFactory.Create(
builder => builder
.AddConsole()
.SetMinimumLevel(LogLevel.Information));
services.AddSingleton(loggerFactory);
});
return await Cli.RunAsync<ThumbnailCommand>(args);
[CliCommand]
public class ThumbnailCommand
{
private readonly ILoggerFactory loggerFactory;
public ThumbnailCommand(ILoggerFactory loggerFactory)
{
this.loggerFactory = loggerFactory;
}
[CliOption]
public TimeSpan StartTime { get; set; }
[CliOption]
public TimeSpan Interval { get; set; }
[CliOption]
public TimeSpan EndTime { get; set; }
[CliOption]
public FileInfo OutputFile { get; set; }
[CliOption]
public FileInfo InputFile { get; set; }
[CliOption]
public bool FastMode { get; set; }
[CliOption]
public FileInfo WebVTT { get; set; }
[CliOption]
public string ImagePath { get; set; }
[CliOption]
public int FrameWidth { get; set; }
[CliOption]
public int FrameHeight { get; set; }
[CliOption]
public int Columns { get; set; }
[CliOption]
public int Rows { get; set; }
[CliOption]
public int BorderWidth { get; set; }
[CliOption]
public string BackgroundGradientStart { get; set; }
[CliOption]
public string BackgroundGradientEnd { get; set; }
public async Task<int> RunAsync()
{
var opts = new ThumbGenOptions()
.WithStartTime(StartTime);
//...
await thumbnailGenerator.ExecuteAsync(opts);
return 0;
}
} This is possible with DotMake.CommandLine which makes System.CommandLine easy to use and maintain. Check it out! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Right now SetHandler only supports up to eight options. Is there a way to exceed this limit? Or will it be increased in the future?
Beta Was this translation helpful? Give feedback.
All reactions