RicoSuter/NSwag · error · InvalidOperationException

The command '" + commandName + "' could not be found.

Error message

The command '" + commandName + "' could not be found.

What it means

CommandLineProcessor.ProcessSingleAsync looks up the command name given on the command line in the host's registered command table and, when no registration matches, throws this InvalidOperationException. It is NSwag's 'unknown subcommand' error: the CLI parsed arguments but the first token does not name any known command (e.g. 'openapi2jsonschema', 'asyncapi', 'help').

Solutions

  1. Check the command name spelling against 'nswag help' output and retry.
  2. Update the NSwag CLI (dotnet tool update nswag or download latest) if using a recently added command.
  3. If hosting programmatically, ensure the custom command type is registered with the CommandLineProcessor/host before processing.

Example fix

// before
nswag openapi2csahrp /input:swagger.json
// after
nswag openapi2csharpclient /input:swagger.json
Defensive patterns

Strategy: try-catch

Validate before calling

var known = new[] { "run", "openapi2csharpclient", "openapi2csharpcontroller", "openapi2typescriptclient", "openapi2jsonschema", "asyncapi", "version", "help" };
if (!known.Contains(args.FirstOrDefault(), StringComparer.OrdinalIgnoreCase))
    throw new ArgumentException($"Unknown NSwag command: {args.FirstOrDefault()}");

Try / catch

try { await processor.ProcessSingleAsync(args, null); }
catch (InvalidOperationException ex) when (ex.Message.Contains("could not be found"))
{ Console.Error.WriteLine($"Unknown command. Run 'nswag help'. Details: {ex.Message}"); }

Prevention

When it happens

Trigger: ProcessSingleAsync is called with args whose first token does not match any registered IConsoleCommand name; typically the user typed a misspelled or non-existent command name, or the assembly containing the command type was not scanned/registered in the host.

Common situations: Typo on the command line (e.g. 'nswag openapi2csahrp'), running an old nswag binary that does not yet know a newer command, or custom command types not registered with the NConsole host.

Related errors


AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14). Data as JSON: /api/errors/bb1761d8ffea5694. Report an issue: GitHub.

Appendix: source

Thrown at src/NSwag.Commands/NConsole/CommandLineProcessor.cs:192

                    {
                        if (!usedArgs.Contains(arg))
                        {
                            unusedArgs.Add(arg);
                        }
                    }

                    throw new UnusedArgumentException($"Used arguments ({usedArgs.Count}) != Provided arguments ({commandArguments.Count()}) -> Check [{string.Join(", ", unusedArgs)}]");
                }

                var output = await command.RunAsync(this, _consoleHost);
                return new CommandResult
                {
                    Command = command,
                    Output = output
                };
            }
            else
                throw new InvalidOperationException("The command '" + commandName + "' could not be found.");
        }

        /// <summary>Processes the command in the given command line arguments.</summary>
        /// <param name="args">The arguments.</param>
        /// <param name="input">The output from the previous command.</param>
        /// <returns>The exeucuted command.</returns>
        /// <exception cref="InvalidOperationException">The command could not be found.</exception>
        /// <exception cref="InvalidOperationException">No dependency resolver available to create a command without default constructor.</exception>
        public IList<CommandResult> Process(string[] args, object input = null)
        {
            return ProcessAsync(args, input).GetAwaiter().GetResult();
        }

        /// <summary>Processes the command in the given command line arguments.</summary>
        /// <param name="args">The arguments.</param>
        /// <param name="input">The output from the previous command.</param>
        /// <returns>The exeucuted command.</returns>
        public IList<CommandResult> ProcessWithExceptionHandling(string[] args, object input = null)

View on GitHub (pinned to 63daf8fcc3)