Skip to content

Commit d5a1210

Browse files
Brahim Hadrichetritao
authored andcommitted
Write error messages to stderr, renamed messages to errorMessages
1 parent 8e9cb70 commit d5a1210

File tree

2 files changed

+47
-40
lines changed

2 files changed

+47
-40
lines changed

src/CLI/CLI.cs

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ class CLI
1010
private static OptionSet optionSet = new OptionSet();
1111
private static Options options = new Options();
1212

13-
static bool ParseCommandLineArgs(string[] args, List<string> messages, ref bool helpShown)
13+
static bool ParseCommandLineArgs(string[] args, List<string> errorMessages, ref bool helpShown)
1414
{
1515
var showHelp = false;
1616

17-
optionSet.Add("I=", "the {PATH} of a folder to search for include files", (i) => { AddIncludeDirs(i, messages); });
17+
optionSet.Add("I=", "the {PATH} of a folder to search for include files", (i) => { AddIncludeDirs(i, errorMessages); });
1818
optionSet.Add("l=", "{LIBRARY} that that contains the symbols of the generated code", l => options.Libraries.Add(l) );
1919
optionSet.Add("L=", "the {PATH} of a folder to search for additional libraries", l => options.LibraryDirs.Add(l) );
20-
optionSet.Add("D:", "additional define with (optional) value to add to be used while parsing the given header files", (n, v) => AddDefine(n, v, messages) );
21-
optionSet.Add("A=", "additional Clang arguments to pass to the compiler while parsing the given header files", (v) => AddArgument(v, messages) );
20+
optionSet.Add("D:", "additional define with (optional) value to add to be used while parsing the given header files", (n, v) => AddDefine(n, v, errorMessages) );
21+
optionSet.Add("A=", "additional Clang arguments to pass to the compiler while parsing the given header files", (v) => AddArgument(v, errorMessages) );
2222

23-
optionSet.Add("o=|output=", "the {PATH} for the generated bindings file (doesn't need the extension since it will depend on the generator)", v => HandleOutputArg(v, messages) );
23+
optionSet.Add("o=|output=", "the {PATH} for the generated bindings file (doesn't need the extension since it will depend on the generator)", v => HandleOutputArg(v, errorMessages) );
2424
optionSet.Add("on=|outputnamespace=", "the {NAMESPACE} that will be used for the generated code", on => options.OutputNamespace = on );
2525

2626
optionSet.Add("iln=|inputlibraryname=|inputlib=", "the {NAME} of the shared library that contains the symbols of the generated code", iln => options.InputLibraryName = iln );
2727
optionSet.Add("d|debug", "enables debug mode which generates more verbose code to aid debugging", v => options.Debug = true);
2828
optionSet.Add("c|compile", "enables automatic compilation of the generated code", v => options.Compile = true);
29-
optionSet.Add("g=|gen=|generator=", "the {TYPE} of generated code: 'chsarp' or 'cli' ('cli' supported only for Windows)", g => { GetGeneratorKind(g, messages); } );
30-
optionSet.Add("p=|platform=", "the {PLATFORM} that the generated code will target: 'win', 'osx' or 'linux'", p => { GetDestinationPlatform(p, messages); } );
31-
optionSet.Add("a=|arch=", "the {ARCHITECTURE} that the generated code will target: 'x86' or 'x64'", a => { GetDestinationArchitecture(a, messages); } );
29+
optionSet.Add("g=|gen=|generator=", "the {TYPE} of generated code: 'chsarp' or 'cli' ('cli' supported only for Windows)", g => { GetGeneratorKind(g, errorMessages); } );
30+
optionSet.Add("p=|platform=", "the {PLATFORM} that the generated code will target: 'win', 'osx' or 'linux'", p => { GetDestinationPlatform(p, errorMessages); } );
31+
optionSet.Add("a=|arch=", "the {ARCHITECTURE} that the generated code will target: 'x86' or 'x64'", a => { GetDestinationArchitecture(a, errorMessages); } );
3232

3333
optionSet.Add("exceptions", "enables support for C++ exceptions in the parser", v => { options.Arguments.Add("-fcxx-exceptions"); });
3434

@@ -59,7 +59,7 @@ static bool ParseCommandLineArgs(string[] args, List<string> messages, ref bool
5959
}
6060

6161
foreach(string s in additionalArguments)
62-
HandleAdditionalArgument(s, messages);
62+
HandleAdditionalArgument(s, errorMessages);
6363

6464
return true;
6565
}
@@ -94,15 +94,15 @@ static void ShowHelp()
9494
Console.WriteLine(" contain only the bindings for that header file.");
9595
}
9696

97-
static void AddIncludeDirs(string dir, List<string> messages)
97+
static void AddIncludeDirs(string dir, List<string> errorMessages)
9898
{
9999
if (Directory.Exists(dir))
100100
options.IncludeDirs.Add(dir);
101101
else
102-
messages.Add(string.Format("Directory '{0}' doesn't exist. Ignoring as include directory.", dir));
102+
errorMessages.Add(string.Format("Directory '{0}' doesn't exist. Ignoring as include directory.", dir));
103103
}
104104

105-
static void HandleOutputArg(string arg, List<string> messages)
105+
static void HandleOutputArg(string arg, List<string> errorMessages)
106106
{
107107
try
108108
{
@@ -114,30 +114,30 @@ static void HandleOutputArg(string arg, List<string> messages)
114114
}
115115
catch(Exception e)
116116
{
117-
messages.Add(e.Message);
117+
errorMessages.Add(e.Message);
118118

119119
options.OutputDir = "";
120120
options.OutputFileName = "";
121121
}
122122
}
123123

124-
static void AddArgument(string value, List<string> messages)
124+
static void AddArgument(string value, List<string> errorMessages)
125125
{
126126
if (value == null)
127-
messages.Add("Invalid compiler argument name for option -A.");
127+
errorMessages.Add("Invalid compiler argument name for option -A.");
128128
else
129129
options.Arguments.Add(value);
130130
}
131131

132-
static void AddDefine(string name, string value, List<string> messages)
132+
static void AddDefine(string name, string value, List<string> errorMessages)
133133
{
134134
if (name == null)
135-
messages.Add("Invalid definition name for option -D.");
135+
errorMessages.Add("Invalid definition name for option -D.");
136136
else
137137
options.Defines.Add(name, value);
138138
}
139139

140-
static void HandleAdditionalArgument(string args, List<string> messages)
140+
static void HandleAdditionalArgument(string args, List<string> errorMessages)
141141
{
142142
if (!Path.IsPathRooted(args))
143143
args = Path.Combine(Directory.GetCurrentDirectory(), args);
@@ -146,21 +146,21 @@ static void HandleAdditionalArgument(string args, List<string> messages)
146146
{
147147
bool searchQuery = args.IndexOf('*') >= 0 || args.IndexOf('?') >= 0;
148148
if (searchQuery || Directory.Exists(args))
149-
GetFilesFromPath(args, messages);
149+
GetFilesFromPath(args, errorMessages);
150150
else if (File.Exists(args))
151151
options.HeaderFiles.Add(args);
152152
else
153153
{
154-
messages.Add(string.Format("File '{0}' could not be found.", args));
154+
errorMessages.Add(string.Format("File '{0}' could not be found.", args));
155155
}
156156
}
157157
catch(Exception)
158158
{
159-
messages.Add(string.Format("Error while looking for files inside path '{0}'. Ignoring.", args));
159+
errorMessages.Add(string.Format("Error while looking for files inside path '{0}'. Ignoring.", args));
160160
}
161161
}
162162

163-
static void GetFilesFromPath(string path, List<string> messages)
163+
static void GetFilesFromPath(string path, List<string> errorMessages)
164164
{
165165
path = path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
166166

@@ -195,11 +195,11 @@ static void GetFilesFromPath(string path, List<string> messages)
195195
}
196196
catch (Exception)
197197
{
198-
messages.Add(string.Format("Error while looking for files inside path '{0}'. Ignoring.", path));
198+
errorMessages.Add(string.Format("Error while looking for files inside path '{0}'. Ignoring.", path));
199199
}
200200
}
201201

202-
static void GetGeneratorKind(string generator, List<string> messages)
202+
static void GetGeneratorKind(string generator, List<string> errorMessages)
203203
{
204204
switch (generator.ToLower())
205205
{
@@ -211,10 +211,10 @@ static void GetGeneratorKind(string generator, List<string> messages)
211211
return;
212212
}
213213

214-
messages.Add(string.Format("Unknown generator kind: {0}. Defaulting to {1}", generator, options.Kind.ToString()));
214+
errorMessages.Add(string.Format("Unknown generator kind: {0}. Defaulting to {1}", generator, options.Kind.ToString()));
215215
}
216216

217-
static void GetDestinationPlatform(string platform, List<string> messages)
217+
static void GetDestinationPlatform(string platform, List<string> errorMessages)
218218
{
219219
switch (platform.ToLower())
220220
{
@@ -229,10 +229,10 @@ static void GetDestinationPlatform(string platform, List<string> messages)
229229
return;
230230
}
231231

232-
messages.Add(string.Format("Unknown target platform: {0}. Defaulting to {1}", platform, options.Platform.ToString()));
232+
errorMessages.Add(string.Format("Unknown target platform: {0}. Defaulting to {1}", platform, options.Platform.ToString()));
233233
}
234234

235-
static void GetDestinationArchitecture(string architecture, List<string> messages)
235+
static void GetDestinationArchitecture(string architecture, List<string> errorMessages)
236236
{
237237
switch (architecture.ToLower())
238238
{
@@ -244,43 +244,43 @@ static void GetDestinationArchitecture(string architecture, List<string> message
244244
return;
245245
}
246246

247-
messages.Add(string.Format("Unknown target architecture: {0}. Defaulting to {1}", architecture, options.Architecture.ToString()));
247+
errorMessages.Add(string.Format("Unknown target architecture: {0}. Defaulting to {1}", architecture, options.Architecture.ToString()));
248248
}
249249

250-
static void PrintMessages(List<string> messages)
250+
static void PrintErrorMessages(List<string> errorMessages)
251251
{
252-
foreach (string m in messages)
253-
Console.WriteLine(m);
252+
foreach (string m in errorMessages)
253+
Console.Error.WriteLine(m);
254254
}
255255

256256
static void Main(string[] args)
257257
{
258-
List<string> messages = new List<string>();
258+
List<string> errorMessages = new List<string>();
259259
bool helpShown = false;
260260

261261
try
262262
{
263-
if (!ParseCommandLineArgs(args, messages, ref helpShown))
263+
if (!ParseCommandLineArgs(args, errorMessages, ref helpShown))
264264
{
265-
PrintMessages(messages);
265+
PrintErrorMessages(errorMessages);
266266

267267
// Don't need to show the help since if ParseCommandLineArgs returns false the help has already been shown
268268
return;
269269
}
270270

271271
Generator gen = new Generator(options);
272272

273-
bool validOptions = gen.ValidateOptions(messages);
273+
bool validOptions = gen.ValidateOptions(errorMessages);
274274

275-
PrintMessages(messages);
275+
PrintErrorMessages(errorMessages);
276276

277277
if (validOptions)
278278
gen.Run();
279279
}
280280
catch (Exception ex)
281281
{
282-
PrintMessages(messages);
283-
Console.WriteLine(ex.Message);
282+
PrintErrorMessages(errorMessages);
283+
Console.Error.WriteLine(ex.Message);
284284
}
285285
}
286286
}

src/Core/Diagnostics.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ public void Emit(DiagnosticInfo info)
120120
var currentIndent = Indents.Sum();
121121
var message = new string(' ', currentIndent) + info.Message;
122122

123-
Console.WriteLine(message);
123+
if(info.Kind == DiagnosticKind.Error)
124+
{
125+
Console.Error.WriteLine(message);
126+
}
127+
else
128+
{
129+
Console.WriteLine(message);
130+
}
124131
Debug.WriteLine(message);
125132
}
126133

0 commit comments

Comments
 (0)