|
| 1 | +using CommandLine; |
| 2 | +using CommandLine.Text; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.IO; |
| 7 | + |
| 8 | +namespace Excel2TextDiff |
| 9 | +{ |
| 10 | + class CommandLineOptions |
| 11 | + { |
| 12 | + [Option('t', SetName = "transform", HelpText = "transform excel to text file")] |
| 13 | + public bool IsTransform { get; set; } |
| 14 | + |
| 15 | + [Option('d', SetName = "diff", HelpText = "transform and diff file")] |
| 16 | + public bool IsDiff { get; set; } |
| 17 | + |
| 18 | + [Option('p', SetName = "diff", Required = false, HelpText = "3rd diff program. default TortoiseMerge")] |
| 19 | + public string DiffProgram { get; set; } |
| 20 | + |
| 21 | + [Option('f', SetName = "diff", Required = false, HelpText = "3rd diff program argument format. default is TortoiseMerge format:'/base:{0} /mine:{1}'")] |
| 22 | + public string DiffProgramArgumentFormat { get; set; } |
| 23 | + |
| 24 | + [Value(0)] |
| 25 | + public IList<string> Files { get; set; } |
| 26 | + |
| 27 | + [Usage()] |
| 28 | + public static IEnumerable<Example> Examples => new List<Example> |
| 29 | + { |
| 30 | + new Example("tranfrom to text", new CommandLineOptions { IsTransform = true, Files = new List<string>{"a.xlsx", "a.txt" } }), |
| 31 | + new Example("diff two excel file", new CommandLineOptions{ IsDiff = true, Files = new List<string>{"a.xlsx", "b.xlsx"}}), |
| 32 | + new Example("diff two excel file with TortoiseMerge", new CommandLineOptions{ IsDiff = true, DiffProgram = "TortoiseMerge",DiffProgramArgumentFormat = "/base:{0} /mine:{1}", Files = new List<string>{"a.xlsx", "b.xlsx"}}), |
| 33 | + }; |
| 34 | + } |
| 35 | + |
| 36 | + class Program |
| 37 | + { |
| 38 | + static void Main(string[] args) |
| 39 | + { |
| 40 | + var options = ParseOptions(args); |
| 41 | + |
| 42 | + System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); |
| 43 | + var writer = new Excel2TextWriter(); |
| 44 | + |
| 45 | + if (options.IsTransform) |
| 46 | + { |
| 47 | + if (options.Files.Count != 2) |
| 48 | + { |
| 49 | + Console.WriteLine("Usage: Excel2TextDiff -t <excel file> <text file>"); |
| 50 | + Environment.Exit(1); |
| 51 | + } |
| 52 | + |
| 53 | + writer.TransformToTextAndSave(options.Files[0], options.Files[1]); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + if (options.Files.Count != 2) |
| 58 | + { |
| 59 | + Console.WriteLine("Usage: Excel2TextDiff -d <excel file 1> <excel file 2> "); |
| 60 | + Environment.Exit(1); |
| 61 | + } |
| 62 | + |
| 63 | + var diffProgame = options.DiffProgram ?? "TortoiseMerge.exe"; |
| 64 | + |
| 65 | + var tempTxt1 = Path.GetTempFileName(); |
| 66 | + writer.TransformToTextAndSave(options.Files[0], tempTxt1); |
| 67 | + |
| 68 | + var tempTxt2 = Path.GetTempFileName(); |
| 69 | + writer.TransformToTextAndSave(options.Files[1], tempTxt2); |
| 70 | + |
| 71 | + ProcessStartInfo startInfo = new ProcessStartInfo(); |
| 72 | + startInfo.FileName = diffProgame; |
| 73 | + string argsFormation = options.DiffProgramArgumentFormat ?? "/base:{0} /mine:{1}"; |
| 74 | + startInfo.Arguments = string.Format(argsFormation, tempTxt1, tempTxt2); |
| 75 | + Process.Start(startInfo); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static CommandLineOptions ParseOptions(String[] args) |
| 80 | + { |
| 81 | + var helpWriter = new StringWriter(); |
| 82 | + var parser = new Parser(ps => |
| 83 | + { |
| 84 | + ps.HelpWriter = helpWriter; |
| 85 | + }); |
| 86 | + |
| 87 | + var result = parser.ParseArguments<CommandLineOptions>(args); |
| 88 | + if (result.Tag == ParserResultType.NotParsed) |
| 89 | + { |
| 90 | + Console.Error.WriteLine(helpWriter.ToString()); |
| 91 | + Environment.Exit(1); |
| 92 | + } |
| 93 | + return ((Parsed<CommandLineOptions>)result).Value; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments