{"record":{"id":"a807f99701e2c177","repo":"RicoSuter/NSwag","slug":"cannot-read-value-from-command-line-because-interactive-mode","errorCode":null,"errorMessage":"Cannot read value from command line because interactive mode is disabled.","messagePattern":"Cannot read value from command line because interactive mode is disabled\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Commands/NConsole/ConsoleHost.cs","lineNumber":57,"sourceCode":"\n        /// <summary>Writes an error message to the console.</summary>\n        /// <param name=\"message\">The message.</param>\n        public void WriteError(string message)\n        {\n            var color = _consoleForegroundColorProperty.GetValue(null);\n            _consoleForegroundColorProperty.SetValue(null, 12); // red\n            _consoleWriteMethod.Invoke(null, new object[] { message });\n            _consoleForegroundColorProperty.SetValue(null, color);\n        }\n\n        /// <summary>Reads a value from the console.</summary>\n        /// <param name=\"message\">The message.</param>\n        /// <returns>The value.</returns>\n        /// <exception cref=\"InvalidOperationException\">Cannot read value from command line because interactive mode is disabled.</exception>\n        public string ReadValue(string message)\n        {\n            if (!InteractiveMode)\n                throw new InvalidOperationException(\"Cannot read value from command line because interactive mode is disabled.\");\n\n            _consoleWriteMethod.Invoke(null, new object[] { \"\\n\" + message });\n            return (string)_consoleReadLineMethod.Invoke(null, []);\n        }\n    }\n}","sourceCodeStart":39,"sourceCodeEnd":63,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NConsole/ConsoleHost.cs#L39-L63","documentation":"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.","triggerScenarios":"Calling ReadValue (directly or via a command prompting for missing parameters) while ConsoleHost.InteractiveMode is false.","commonSituations":"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.","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."],"exampleFix":"// before\nhost.InteractiveMode = false; host.ReadValue(\"Enter url:\");\n// after\nhost.InteractiveMode = true; var url = host.ReadValue(\"Enter url:\");","handlingStrategy":"try-catch","validationCode":"// ensure all required options are provided so no interactive prompt is needed\nvar missing = command.Where(o => o.IsRequired && o.Value == null).Select(o => o.Name).ToList();\nif (missing.Count > 0 && !host.InteractiveMode)\n    throw new ArgumentException($\"Missing required options: {string.Join(\", \", missing)}\");","typeGuard":null,"tryCatchPattern":"try { var value = host.ReadValue(\"Enter url:\"); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"interactive mode is disabled\"))\n{ var value = fallbackValue; /* or surface a clear 'parameter required' error */ }","preventionTips":["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."],"tags":["console","interactive-mode","cli"],"backgroundTag":"unsupported-operation","analyzedSha":"63daf8fcc3a25151b62eb4b326a1e8ea048a0d41","analyzedAt":"2026-09-14T11:38:15.205Z","contentChangedAt":"2026-09-14T11:38:15.205Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}