RicoSuter/NSwag · error · InvalidOperationException
Could not retrieve command from arguments
Error message
Could not retrieve command from arguments {string.Join(", ", args)} What it means
GetCommandNameAndArguments extracts the command name and its arguments from the raw command-line args. When args are supplied but no command name can be determined (and interactive mode could not prompt), it throws this InvalidOperationException listing the arguments it received.
Solutions
- Pass the command name as the first CLI argument, e.g. 'nswag run nswag.json'.
- Enable interactive mode if you want to be prompted for the command.
- If invoking programmatically, verify the args array starts with the command name string.
Example fix
// before
var args = new[] { "/input:swagger.json", "/output:out.cs" };
// after
var args = new[] { "openapi2csharpclient", "/input:swagger.json", "/output:out.cs" }; Defensive patterns
Strategy: validation
Validate before calling
if (args == null || args.Length == 0 || args[0].StartsWith("/"))
throw new ArgumentException("First argument must be the NSwag command name (e.g. 'run')."); Try / catch
try { var command = await processor.ProcessSingleAsync(args, null); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Could not retrieve command"))
{ Console.Error.WriteLine("Usage: nswag <command> [args]. See 'nswag help'."); } Prevention
- Always pass the command name as the first argument.
- For flag-only invocations, use 'nswag run nswag.json /input:...'.
- Assert args[0] does not start with '/' in programmatic callers.
When it happens
Trigger: ProcessSingleAsync passes an args array that is empty or whose content yields no command name while interactive mode is disabled, so neither the parsed name nor the interactive ReadCommandNameInteractive path produces a value.
Common situations: Running 'nswag' with only flags like /input but no command name; calling the processor programmatically with an empty args array; piping args that lost the first token.
Understand the failure class
Background: "no subcommand specified" and "... is required": CLI errors when a required argument is missing — this error's family across 13 libraries.
Related errors
- The argument 'Input' was empty.
- The command '" + commandName + "' could not be found.
- The specified runtime in the document
- Project outputs could not be located in
- The ouput of is a 32-bit application and requires…
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/04384f7ff14f5320.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Commands/NConsole/CommandLineProcessor.cs:256
protected void GetCommandNameAndArguments(string[] args, out string commandName, out IEnumerable<string> commandArguments)
{
commandName = string.Empty;
commandArguments = new List<string>();
bool hasArguments = (args.Length > 0) && (args[0].Length > 0) && (char.IsLetter(args[0][0]));
if (hasArguments)
{
commandName = args[0];
commandArguments = args.Skip(1);
}
else if (_consoleHost.InteractiveMode)
{
commandName = ReadCommandNameInteractive();
commandArguments = args;
}
else
{
throw new InvalidOperationException($"Could not retrieve command from arguments {string.Join(", ", args)}");
}
}
/// <exception cref="InvalidOperationException">No dependency resolver available to create a command without default constructor.</exception>
private IConsoleCommand CreateCommand(Type commandType)
{
var constructors = commandType.GetTypeInfo().DeclaredConstructors;
IConsoleCommand command;
if (constructors.Any())
{
var constructor = constructors.First(c => !c.IsStatic);
if (constructor.GetParameters().Length > 0 && _dependencyResolver == null)
throw new InvalidOperationException("No dependency resolver available to create a command without default constructor.");
var parameters = constructor.GetParameters()
.Select(param => _dependencyResolver.GetService(param.ParameterType))View on GitHub (pinned to 63daf8fcc3)