Skip to content

Commit 150561b

Browse files
authored
Making the debug directive opt-in (#1141)
* Making the debug directive opt-in * Adding short-circuit to debug directive * Adding better example for output message.
1 parent 20ef1b4 commit 150561b

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ namespace System.CommandLine.Builder
2121
public static class CommandLineBuilderExtensions
2222
{
2323
private static readonly Lazy<string> _assemblyVersion =
24-
new Lazy<string>(() => {
24+
new Lazy<string>(() =>
25+
{
2526
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
2627
var assemblyVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
2728
if (assemblyVersionAttribute is null)
@@ -65,7 +66,7 @@ public static TBuilder AddOption<TBuilder>(
6566
}
6667

6768
public static TBuilder AddGlobalOption<TBuilder>(
68-
this TBuilder builder,
69+
this TBuilder builder,
6970
Option option)
7071
where TBuilder : CommandBuilder
7172
{
@@ -204,21 +205,45 @@ await feature.EnsureRegistered(async () =>
204205
}
205206

206207
public static CommandLineBuilder UseDebugDirective(
207-
this CommandLineBuilder builder)
208+
this CommandLineBuilder builder,
209+
int? errorExitCode = null)
208210
{
209211
builder.AddMiddleware(async (context, next) =>
210212
{
211213
if (context.ParseResult.Directives.Contains("debug"))
212214
{
213-
var process = Diagnostics.Process.GetCurrentProcess();
214-
215-
var processId = process.Id;
215+
const string environmentVariableName = "DOTNET_COMMANDLINE_DEBUG_PROCESSES";
216216

217-
context.Console.Out.WriteLine($"Attach your debugger to process {processId} ({process.ProcessName}).");
218-
219-
while (!Debugger.IsAttached)
217+
var process = Diagnostics.Process.GetCurrentProcess();
218+
string debuggableProcessNames = GetEnvironmentVariable(environmentVariableName);
219+
if (string.IsNullOrWhiteSpace(debuggableProcessNames))
220220
{
221-
await Task.Delay(500);
221+
context.Console.Error.WriteLine("Debug directive specified, but no process names are listed as allowed for debug.");
222+
context.Console.Error.WriteLine($"Add your process name to the '{environmentVariableName}' environment variable.");
223+
context.Console.Error.WriteLine($"The value of the variable should be the name of the processes, separated by a semi-colon ';', for example '{environmentVariableName}={process.ProcessName}'");
224+
context.ExitCode = errorExitCode ?? 1;
225+
return;
226+
}
227+
else
228+
{
229+
string[] processNames = debuggableProcessNames.Split(';');
230+
if (processNames.Contains(process.ProcessName, StringComparer.Ordinal))
231+
{
232+
var processId = process.Id;
233+
234+
context.Console.Out.WriteLine($"Attach your debugger to process {processId} ({process.ProcessName}).");
235+
var startTime = DateTime.Now;
236+
while (!Debugger.IsAttached)
237+
{
238+
await Task.Delay(500);
239+
}
240+
}
241+
else
242+
{
243+
context.Console.Error.WriteLine($"Process name '{process.ProcessName}' is not included in the list of debuggable process names in the {environmentVariableName} environment variable ('{debuggableProcessNames}')");
244+
context.ExitCode = errorExitCode ?? 1;
245+
return;
246+
}
222247
}
223248
}
224249

@@ -250,7 +275,7 @@ public static CommandLineBuilder UseEnvironmentVariableDirective(
250275

251276
return next(context);
252277
}, MiddlewareOrderInternal.EnvironmentVariableDirective);
253-
278+
254279
return builder;
255280
}
256281

@@ -316,7 +341,7 @@ internal static CommandLineBuilder UseHelp(
316341
{
317342
if (builder.HelpOption is null)
318343
{
319-
builder.HelpOption = helpOption;
344+
builder.HelpOption = helpOption;
320345
builder.Command.TryAddGlobalOption(helpOption);
321346

322347
builder.AddMiddleware(async (context, next) =>
@@ -443,7 +468,7 @@ public static CommandLineBuilder UseTypoCorrections(
443468
context.ParseResult.CommandResult.Command.TreatUnmatchedTokensAsErrors)
444469
{
445470
var typoCorrection = new TypoCorrection(maxLevenshteinDistance);
446-
471+
447472
typoCorrection.ProvideSuggestions(context.ParseResult, context.Console);
448473
}
449474
await next(context);

0 commit comments

Comments
 (0)