CSharp · Console
// @nuget: CommandLineParser -Version 2.8.0
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLine;
using CommandLine.Text;
using System.Reflection;
public class VerbDemo
{
public static void Main()
{
var line=new String('-',40);
Console.WriteLine("---show help for verbs ------");
var args = "--help".Split();
Start(args);
Console.WriteLine("---show help for clone verb ------");
args = "clone --help".Split();
Start(args);
Console.WriteLine(line);
args = "clone git://git.kernel.org/pub/scm/.../linux.git".Split();
Start(args);
Console.WriteLine(line);
args = new[] { "commit", "-m", "update parser setting" };
Start(args);
}
public static void Start(string[] args)
{
Console.WriteLine($"Args: {string.Join(' ',args.Select(x => $"\"{x}\""))}");
//Type[] types = {typeof(AddOptions), typeof(CommitOptions), typeof(CloneOptions)};
//OR
//get alltypes using reflection /plugin loader /Ioc container
var types = LoadVerbs();
Parser.Default.ParseArguments(args, types)
.WithParsed(Run)
.WithNotParsed(HandleErrors);
}
//load all Verb types using Reflection
static Type[] LoadVerbs()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.GetCustomAttribute<VerbAttribute>() != null).ToArray();
}
private static void Run(object obj)
{
switch (obj)
{
case CloneOptions c:
RunClone(c);
break;
case CommitOptions o:
RunCommit(o);
break;
case AddOptions a:
RunAdd(a);
break;
}
//process CloneOptions
void RunClone(CloneOptions opts)
{
Console.WriteLine("Processing Verb 'Clone'");
Console.WriteLine();
}
//process CommitOptions
void RunCommit(CommitOptions opts)
{
Console.WriteLine("Processing Verb 'Commit'");
Console.WriteLine($"Message= {opts.Message}");
Console.WriteLine();
}
//process AddOptions
void RunAdd(AddOptions opts)
{
Console.WriteLine("Processing Verb 'Add'");
Console.WriteLine();
}
}
private static void HandleErrors(IEnumerable<Error> obj)
{
}
}
[Verb("clone", HelpText = "Clone a repository into a new directory.")]
internal class CloneOptions
{
[Option('l', "local", HelpText = HelpConstant.Clone_local)]
public bool Local { get; set; }
[Option('q', "quiet", HelpText = HelpConstant.Clone_quiet)]
public bool Quiet { get; set; }
[Value(0, MetaName = "Reoisitory", HelpText = HelpConstant.Clone_repository)]
public string Repository { get; set; }
[Usage(ApplicationAlias = "yourapp")]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Normal scenario", new CloneOptions { Local = true });
}
}
}
[Verb("commit", HelpText = "Record changes to the repository.")]
internal class CommitOptions
{
[Option('i', "interactive")]
public bool Interactive { get; set; }
[Option('a', "all")]
public bool All { get; set; }
[Option('m', "message", Required = true)]
public string Message { get; set; }
}
[Verb("add", HelpText = "Add file contents to the index.")]
internal class AddOptions
{
[Option('n', "dry-run")]
public bool DryRun { get; set; }
[Option('f', "force")]
public bool Force { get; set; }
}
class HelpConstant
{
public const string Clone_local = "When the repository to clone from is on a local machine, this flag bypasses the normal \"Git aware\" transport mechanism and clones the repository by making a copy of HEAD and everything under objects and refs directories.";
public const string Clone_quiet = "Operate quietly. Progress is not reported to the standard error stream.";
public const string Clone_repository = "The (possibly remote) repository to clone from. See the GIT URLS section below for more information on specifying repositories.";
//commit
public const string Commit_message = "Use the given <msg> as the commit message. If multiple -m options are given, their values are concatenated as separate paragraphs.";
}
[AttributeUsage(AttributeTargets.All)]
class CustomAttribute: Attribute {}