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