-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
146 lines (124 loc) · 3.63 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Reflection;
#if !NET5_0_OR_GREATER
using System.IO;
using System.Linq;
using System.Collections.Generic;
#endif
namespace ChangeLogGenerator
{
public static class Program
{
static void Main(string[] args)
{
#region commandline args
var type = ReportGenerator.OutputType.None;
if (args.ArgBool("html"))
type = ReportGenerator.OutputType.Html;
if (args.ArgBool("rtf"))
type = ReportGenerator.OutputType.Rtf;
if (args.ArgBool("md"))
type = ReportGenerator.OutputType.Md;
if (args.ArgBool("txt") || args.ArgBool("text"))
type = ReportGenerator.OutputType.Txt;
var repoPath = args.Arg("repo") ?? ".";
var branch = args.Arg("branch");
var noCredit = args.ArgBool("nocredit");
var showVersion = args.ArgBool("version");
var outFile = args.Arg("output");
if(args.Length == 0)
{
ShowUsage();
return;
}
// validate
if (!args.Check(out string message))
{
Console.Error.WriteLine($"Error: {message}");
ShowUsage();
return;
}
void ShowUsage()
{
Console.Error.WriteLine($"Usage: {typeof(Program).Namespace} --txt | --rtf | --md | --html [--nocredit] [--repo path] [--branch name] [--output filename]");
}
if (showVersion)
{
var name = Assembly.GetExecutingAssembly().GetName();
Console.Error.WriteLine($"{name.Name} {name.Version.Major}.{name.Version.Build}.{name.Version.Minor}.{name.Version.MinorRevision}");
return;
}
if (type == ReportGenerator.OutputType.None)
{
Console.Error.WriteLine("Error: Specify file format --txt | --rtf | --md | --html");
return;
}
if(outFile != null)
{
var ext = Path.GetExtension(outFile);
if (ext == string.Empty)
{
outFile += "." + type.ToString().ToLower();
Console.Error.WriteLine($"Outfile: {outFile}");
}
else if(!ext.Substring(1).Equals(type.ToString(), StringComparison.InvariantCultureIgnoreCase))
{
Console.Error.WriteLine($"Invalid extension {ext}");
return;
}
}
#endregion
try
{
var parser = new ReportGenerator(repoPath, branch) { NoCredit = noCredit };
using (TextWriter outStream = outFile == null ? Console.Out : new StreamWriter(outFile))
{
parser.Generate(type, outStream);
}
}
catch (Exception e)
{
Console.Error.WriteLine($"Error: {e.Message}");
}
}
}
internal static class ArgsExtensions
{
private static readonly Dictionary<string, (bool Manditory, bool HasValue)> _known = new Dictionary<string, (bool, bool)>();
// for "-searchKey value" return "value"
internal static string Arg(this string[] args, string name, bool manditory = false)
{
_known.Add($"--{name}", (manditory, true));
var index = Array.IndexOf(args, $"--{name}");
return index != -1 && index + 1 < args.Length ? args[index + 1] : null;
}
// Simple boolean arg "-someoption" return true
internal static bool ArgBool(this string[] args, string name, bool manditory = false)
{
_known.Add($"--{name}", (manditory, false));
return args.Contains($"--{name}");
}
/// <summary>
/// Check
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
public static bool Check(this string[] args, out string message)
{
var unknown = args.Where(x => x.StartsWith("-")).Except(_known.Keys);
if (unknown.Any())
{
message = $"Unknown argument(s): {string.Join(", ", unknown)}";
return false;
}
var missing = _known.Where(x => x.Value.Manditory).Select(x => x.Key).Except(args);
if (missing.Any())
{
message = $"Missing argument(s): {string.Join(", ", missing)}";
return false;
}
message = "";
return true;
}
}
}