-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (122 loc) · 4.88 KB
/
Program.cs
File metadata and controls
137 lines (122 loc) · 4.88 KB
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
using FlatBufferEx;
using FlatBufferEx.Configuration;
using FlatBufferEx.Services;
using Microsoft.Extensions.DependencyInjection;
using NDesk.Options;
namespace FlatBufferExample
{
/// <summary>
/// Service enumeration (extensible)
/// </summary>
public enum Service : uint
{ }
/// <summary>
/// Main program class for FlatBuffer extension tool
/// Parses FlatBuffer schema files (.fbs) and generates code for various languages.
/// </summary>
class Program
{
/// <summary>
/// Application entry point
/// Parses command line arguments and executes FlatBuffer compiler to generate code.
/// </summary>
/// <param name="args">Command line arguments</param>
static async Task<int> Main(string[] args)
{
try
{
var config = ParseCommandLineArguments(args);
if (config == null)
{
ShowUsage();
return 1;
}
using var serviceProvider = CreateServiceProvider();
var processor = serviceProvider.GetRequiredService<FlatBufferProcessor>();
await processor.ProcessAsync(config);
Console.WriteLine("FlatBuffer code generation completed successfully.");
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error: {ex.Message}");
Console.Error.WriteLine($"Stack trace: {ex.StackTrace}");
return 1;
}
}
/// <summary>
/// Parses command line arguments and returns configuration
/// </summary>
/// <param name="args">Command line arguments</param>
/// <returns>Configuration object or null if parsing failed</returns>
private static AppConfiguration ParseCommandLineArguments(string[] args)
{
var config = new AppConfiguration();
var showHelp = false;
var options = new OptionSet
{
{ "p|path=", "input directory containing .fbs files", v => config.InputPath = v },
{ "l|lang=", "target languages (e.g., \"c++|c#\")", v => config.Languages = v },
{ "o|output=", "output directory for generated code", v => config.OutputPath = v },
{ "i|include=", "include directory path", v => config.IncludePath = v },
{ "h|help", "show this help message", v => showHelp = v != null },
};
try
{
options.Parse(args);
if (showHelp)
{
ShowUsage(options);
return null;
}
if (!config.IsValid(out var validationErrors))
{
Console.Error.WriteLine("Configuration validation failed:");
foreach (var error in validationErrors)
{
Console.Error.WriteLine($" - {error}");
}
return null;
}
return config;
}
catch (OptionException ex)
{
Console.Error.WriteLine($"Command line parsing error: {ex.Message}");
return null;
}
}
/// <summary>
/// Creates and configures service dependencies using ASP.NET Core DI
/// </summary>
/// <returns>Service provider with all services registered</returns>
private static ServiceProvider CreateServiceProvider()
{
var services = new ServiceCollection();
// Register services as singletons
services.AddSingleton<FileService>();
services.AddSingleton<TemplateService>();
services.AddSingleton<FlatBufferCompilerService>();
services.AddSingleton<CodeGenerationService>();
services.AddSingleton<FlatBufferProcessor>();
return services.BuildServiceProvider();
}
/// <summary>
/// Shows usage information
/// </summary>
/// <param name="options">Option set for detailed help</param>
private static void ShowUsage(OptionSet options = null)
{
Console.WriteLine("FlatBufferEx - Enhanced FlatBuffer code generator");
Console.WriteLine();
Console.WriteLine("Usage: dotnet run -- [OPTIONS]");
Console.WriteLine();
Console.WriteLine("Options:");
options?.WriteOptionDescriptions(Console.Out);
Console.WriteLine();
Console.WriteLine("Examples:");
Console.WriteLine(" dotnet run -- --path ./schemas --lang \"c#\" --output ./generated");
Console.WriteLine(" dotnet run -- --path ./schemas --lang \"c++|c#\" --output ./generated --include ./common");
}
}
}