Closed Ptkatz closed 6 months ago
I tried it and I can do it using WithNotParsed.
using CommandLine;
using CommandLine.Text;
using System;
using System.Collections.Generic;
public class Options
{
[Option('v', "verbose", Required = false, HelpText = "Set output to verbose messages.")]
public bool Verbose { get; set; }
[Option('n', "name", Required = true, HelpText = "Input name.")]
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
var parser = new Parser(with => with.HelpWriter = null);
var parserResult = parser.ParseArguments<Options>(args);
string helpText = null;
parserResult
.WithParsed(opts => RunWithOptions(opts))
.WithNotParsed(errs => helpText = HandleParseError(parserResult, errs));
if (helpText != null)
{
Console.WriteLine("Help text was generated:");
Console.WriteLine(helpText);
}
}
static void RunWithOptions(Options opts)
{
Console.WriteLine($"Verbose: {opts.Verbose}, Name: {opts.Name}");
}
static string HandleParseError<T>(ParserResult<T> parserResult, IEnumerable<Error> errs)
{
var helpText = HelpText.AutoBuild(parserResult, h =>
{
return HelpText.DefaultParsingErrorsHandler(parserResult, h);
}, e => e);
return helpText.ToString();
}
}
I want to save the help information into a variable, but there seems to be no such operation