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

  1. Supply all required command parameters on the command line or in the .nswag document so no prompt is needed.
  2. Enable InteractiveMode on the ConsoleHost if a console is actually available.
  3. 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

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


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)