CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLine;
public class Program
{
public static void Main()
{
int runcount = 1;
//run a bunch of different commandline args through a simulated Maine entry point
foreach(var args in new List<string[]> {
//experiment with different command line values here
new string[0]
,new string[] { "AssemblyInfo.cs", "myproject.csproj" }
,new string[] { "-a", "App_Data/cachefile.json" }
,new string[] { "-m", "App_Data/cachefile.json", "App_Data/cachefile2.json" }
}) {
Console.WriteLine("*** RUN #{0}, args: {1}",runcount++,String.Join(" ",args));
__YourMain(args);
Console.WriteLine("");
}
}
public static void __YourMain(string[] args)
{
Console.WriteLine("");
var parserResult = CommandLine.Parser.Default
.ParseArguments<TouchOptions>(args)
.WithParsed((TouchOptions opts) => { RunCommand(opts); })
.WithNotParsed((errs) => { errs.Dump(); });
}
static int RunCommand(TouchOptions TouchOptions)
{
Console.WriteLine("only change access time: {0}", IfTrue(TouchOptions.OnlyAccessTime));
Console.WriteLine("only change modification time: {0}", IfTrue(TouchOptions.OnlyModificationTime));
Console.WriteLine("do not create any files: {0}", IfTrue(TouchOptions.DontCreate));
Console.WriteLine("files:");
Console.WriteLine("=>" + String.Join(",", TouchOptions.Filenames.Select(_=>"\""+_+"\"")));
return 0;
}
static string IfTrue(bool value) { if(value) return value.ToString(); else return "--"; }
}
public class TouchOptions
{
[Value(0)]
public IEnumerable<string> Filenames { get; set; }
[Option('c',"no-create",HelpText="")]
public bool DontCreate { get; set; }
[Option('a',HelpText="change only the access time")]
public bool OnlyAccessTime { get; set; }
[Option('m',HelpText="")]
public bool OnlyModificationTime { get; set; }
}