natemcmaster / CommandLineUtils

Command line parsing and utilities for .NET

Home Page:https://natemcmaster.github.io/CommandLineUtils/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add ResponseFileHandling to CommandLineContext so it can be passed through the static CommandLineApp.Execute

danmoseley opened this issue · comments

It's convenient to use the static method CommandLineApplication.Execute<ProgramArguments>(console, args); (or async equivalent) but it does not allow ResponseFileHandling to be set to true.

One must use the instance methods, which likely involve pasting a bunch of code in, ie.,

public static async Task<int> ExecuteAsync<TApp>(CommandLineContext context, CancellationToken cancellationToken = default)
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
where TApp : class
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.Arguments == null)
{
throw new ArgumentNullException(nameof(context) + "." + nameof(context.Arguments));
}
if (context.WorkingDirectory == null)
{
throw new ArgumentNullException(nameof(context) + "." + nameof(context.WorkingDirectory));
}
if (context.Console == null)
{
throw new ArgumentNullException(nameof(context) + "." + nameof(context.Console));
}
try
{
using var app = new CommandLineApplication<TApp>();
app.SetContext(context);
app.Conventions.UseDefaultConventions();
return await app.ExecuteAsync(context.Arguments, cancellationToken);
}
catch (OperationCanceledException)
{
return s_exitCodeOperationCanceled;
}
catch (CommandParsingException ex)
{
context.Console.Error.WriteLine(ex.Message);
if (ex is UnrecognizedCommandParsingException uex && uex.NearestMatches.Any())
{
context.Console.Error.WriteLine();
context.Console.Error.WriteLine("Did you mean this?");
context.Console.Error.WriteLine(" " + uex.NearestMatches.First());
}
return ValidationErrorExitCode;

It would be nice to have a way to pass ResponseFileHandling through the static method. It seems it could be conveniently added to CommandLineContext and no new API need be added. If a value is set on CommandLineContext, it should "win" over anything on CommandLineApplication.

linking microsoft/slngen#426

Actually, CommandLineContext classes are all internal, so it would need public CommandLineApplication(IConsole console, string workingDirectory, ResponseFileHandling responseFileHandling) to be added. And of course there are other parameters on the instance CommandLineApplication that may be interesting to others, which would make a lot of overloads.

Disregard, I can use the declarative way.