-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
147 lines (121 loc) · 3.45 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
147
using System.Runtime.InteropServices;
using Microsoft.Extensions.Configuration;
namespace FSCmd;
internal static class Program
{
public static IConfigurationRoot Configuration { get; private set; }
//=============================================================================
/// <summary></summary>
[STAThread]
static int Main (string[] args)
{
var toolModules = enumerateAllClassesImplementing<IToolModule> ();
// no arguments, show help
if (args.Length < 1)
{
showHelp (toolModules);
return -1;
}
// the first argument must be the tool name
string toolName = args[0];
// special case: help
if (toolName == "help")
{
if (args.Length > 1)
{
// particular help
toolName = args[1];
showHelp (toolModules, toolName);
}
else
{
showHelp (toolModules);
}
return -1;
}
// create configuration
var builder = new ConfigurationBuilder ();
builder.AddCommandLine (args);
builder.AddEnvironmentVariables ();
Configuration = builder.Build ();
// find the tool
var tool = getToolByName (toolModules, toolName);
// tool not found
if (tool == null)
{
Console.WriteLine ($"Tool '{toolName}' not found.");
showHelp (toolModules);
return -1;
}
// run the tool
if (!tool.Run (toolName))
{
showHelp (toolModules, toolName);
return -1;
}
return 0;
}
//=============================================================================
/// <summary></summary>
static IToolModule getToolByName (List<IToolModule> toolModules, string name)
{
foreach (var tool in toolModules)
{
var toolNames = tool.Info.Select (s => s.Name);
if (toolNames.Contains (name)) return tool;
}
return null;
}
//=============================================================================
/// <summary></summary>
static List<T> enumerateAllClassesImplementing<T> ()
{
var types = AppDomain.CurrentDomain.GetAssemblies ()
.SelectMany (s => s.GetTypes ())
.Where (p => typeof (T).IsAssignableFrom (p) && !p.IsInterface)
.Select (p => (T)Activator.CreateInstance (p))
;
return types.ToList ();
}
//=============================================================================
/// <summary></summary>
private static void showHelp (List<IToolModule> toolModules)
{
Console.WriteLine ("Usage: FSCmd <tool> [tool options]");
Console.WriteLine ("Available tools: ");
Console.WriteLine ("\tFSCmd help [tool]");
foreach (var tool in toolModules)
{
foreach (var info in tool.Info)
{
string str = $"\tFSCmd {info.Name} ";
while (str.Length < 30) str += " ";
str += $"{info.SingleLineHelp}";
Console.WriteLine (str);
}
}
}
//=============================================================================
/// <summary></summary>
private static void showHelp (List<IToolModule> toolModules, string toolName)
{
var tool = getToolByName (toolModules, toolName);
if (tool == null) return;
var info = tool.Info.FirstOrDefault (p => p.Name == toolName);
Console.WriteLine ($"FSCmd {info.Name} : {info.SingleLineHelp}\n");
if (info.Parameters.Count <= 0)
{
Console.WriteLine ($"Usage: FSCmd {toolName} [no parameters]");
return;
}
Console.WriteLine ($"Usage: FSCmd {toolName} [tool options]");
foreach (var parm in info.Parameters)
{
string str= $" {parm.ParameterName}";
str += $"=\"{parm.ParameterValueDescription}\"";
while (str.Length < 48) str += " ";
str += $"{parm.ParameterHelp}";
Console.WriteLine (str);
}
}
}