CSharp · Console
// @nuget: CommandLineParser -Version 2.6.0
using System;
using System.Collections.Generic;
using CommandLine;
using CommandLine.Text;
//Print Help Screen when Parser success
public class Program
{
static ParserResult<Options> parserResult;
public static void Main()
{
string[] args = new[]{"--stdin", "123"};
Console.WriteLine("Call help screen when Parser success");
var parser = new CommandLine.Parser(with => with.HelpWriter = null);
parserResult = Parser.Default.ParseArguments<Options>(args);
parserResult.WithParsed(Run).WithNotParsed(errs => HandleErrors(errs));
}
static void HandleErrors(IEnumerable<Error> errs)
{
Console.WriteLine("Parser Fail");
}
private static void Run(Options options)
{
Console.WriteLine("parser SUCCESS");
if (!validate(options))
{
Console.WriteLine("Validation fail");
var helpText = GetHelp<Options>(parserResult);
Console.WriteLine(helpText);
}
}
//Generate Help text
static string GetHelp<T>(ParserResult<T> result)
{
// use default configuration
// you can customize HelpText and pass different configuratins
//see wiki
// https://github.com/commandlineparser/commandline/wiki/How-To#q1
// https://github.com/commandlineparser/commandline/wiki/HelpText-Configuration
return HelpText.AutoBuild(result, h => h, e => e);
}
//validate options
static bool validate(Options options)
{
// do validation
if (options.FileName == null)
return false;
return true;
}
}
class Options
{
[Option(Default = false, HelpText = "Prints all messages to standard output.")]
public bool Verbose
{
get;
set;
}
[Option("stdin", Default = false, HelpText = "Read from stdin")]
public bool stdin
{
get;
set;
}
[Option("file", HelpText = "File name")]
public string FileName
{
get;
set;
}
[Value(0, MetaName = "offset", HelpText = "File offset.")]
public long? Offset
{
get;
set;
}
}