RicoSuter/NSwag · error · InvalidOperationException
Cannot read value from command line because interactive…
Error message
Cannot read value from command line because interactive mode is disabled.
What it means
ConsoleHost.ReadValue prompts the user and reads a line from the console via reflection-invoked Console.Write/ReadLine. In non-interactive mode (e.g. GUI-driven usage where Console interaction is not allowed) it refuses and throws this InvalidOperationException instead of blocking on input.
Solutions
- Supply all required command parameters on the command line or in the .nswag document so no prompt is needed.
- Enable InteractiveMode on the ConsoleHost if a console is actually available.
- Wrap the call in try-catch when non-interactive behavior is intentional.
Example fix
// before
host.InteractiveMode = false; host.ReadValue("Enter url:");
// after
host.InteractiveMode = true; var url = host.ReadValue("Enter url:"); Defensive patterns
Strategy: try-catch
Validate before calling
// ensure all required options are provided so no interactive prompt is needed
var missing = command.Where(o => o.IsRequired && o.Value == null).Select(o => o.Name).ToList();
if (missing.Count > 0 && !host.InteractiveMode)
throw new ArgumentException($"Missing required options: {string.Join(", ", missing)}"); Try / catch
try { var value = host.ReadValue("Enter url:"); }
catch (InvalidOperationException ex) when (ex.Message.Contains("interactive mode is disabled"))
{ var value = fallbackValue; /* or surface a clear 'parameter required' error */ } Prevention
- Supply every required command-line option so prompts never occur.
- Only enable InteractiveMode when a real console is attached.
- In services/CI, never rely on prompts; fail fast with explicit errors.
When it happens
Trigger: Calling ReadValue (directly or via a command prompting for missing parameters) while ConsoleHost.InteractiveMode is false.
Common situations: Running NSwag inside a GUI tool, CI service, or from code with interactive prompts disabled, when a required parameter was omitted and the host tries to prompt for it.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- The specified runtime in the document
- Project outputs could not be located in
- The ouput of is a 32-bit application and requires…
- The ouput of is a 64-bit application and requires…
- No project (.csproj) file could be found under directory
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/a807f99701e2c177.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Commands/NConsole/ConsoleHost.cs:57
/// <summary>Writes an error message to the console.</summary>
/// <param name="message">The message.</param>
public void WriteError(string message)
{
var color = _consoleForegroundColorProperty.GetValue(null);
_consoleForegroundColorProperty.SetValue(null, 12); // red
_consoleWriteMethod.Invoke(null, new object[] { message });
_consoleForegroundColorProperty.SetValue(null, color);
}
/// <summary>Reads a value from the console.</summary>
/// <param name="message">The message.</param>
/// <returns>The value.</returns>
/// <exception cref="InvalidOperationException">Cannot read value from command line because interactive mode is disabled.</exception>
public string ReadValue(string message)
{
if (!InteractiveMode)
throw new InvalidOperationException("Cannot read value from command line because interactive mode is disabled.");
_consoleWriteMethod.Invoke(null, new object[] { "\n" + message });
return (string)_consoleReadLineMethod.Invoke(null, []);
}
}
}View on GitHub (pinned to 63daf8fcc3)