{"record":{"id":"5bfb7c3a583963cf","repo":"RicoSuter/NSwag","slug":"the-command-name-has-already-been-added","errorCode":null,"errorMessage":"The command '\" + name + \"' has already been added.","messagePattern":"The command '\" \\+ name \\+ \"' has already been added\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Commands/NConsole/CommandLineProcessor.cs","lineNumber":89,"sourceCode":"        /// <exception cref=\"InvalidOperationException\">The command has already been added.</exception>\n        /// <exception cref=\"InvalidOperationException\">The command class is missing the CommandAttribute attribute.</exception>\n        public void RegisterCommand(Type commandType)\n        {\n            var commandAttribute = commandType.GetTypeInfo().GetCustomAttribute<CommandAttribute>();\n            if (commandAttribute == null)\n                throw new InvalidOperationException(\"The command class is missing the CommandAttribute attribute.\");\n\n            RegisterCommand(commandAttribute.Name, commandType);\n        }\n\n        /// <summary>Adds a command.</summary>\n        /// <param name=\"name\">The name of the command.</param>\n        /// <param name=\"commandType\">Type of the command.</param>\n        /// <exception cref=\"InvalidOperationException\">The command has already been added.</exception>\n        public void RegisterCommand(string name, Type commandType)\n        {\n            if (_commands.ContainsKey(name))\n                throw new InvalidOperationException(\"The command '\" + name + \"' has already been added.\");\n\n            _commands.Add(name.ToLowerInvariant(), commandType);\n        }\n\n        /// <summary>Processes the command in the given command line arguments.</summary>\n        /// <param name=\"args\">The arguments.</param>\n        /// <param name=\"input\">The input for the first command.</param>\n        /// <returns>The executed command.</returns>\n        /// <exception cref=\"InvalidOperationException\">The command could not be found.</exception>\n        /// <exception cref=\"InvalidOperationException\">No dependency resolver available to create a command without default constructor.</exception>\n        public async Task<IList<CommandResult>> ProcessAsync(string[] args, object input = null)\n        {\n            var results = new List<CommandResult>();\n\n            var commands = new List<string[]>();\n            var commandArgs = new List<string>();\n            foreach (var arg in args)\n            {","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NConsole/CommandLineProcessor.cs#L71-L107","documentation":"RegisterCommand(name, type) keeps a dictionary of command names; adding a name that already exists throws InvalidOperationException. It's a duplicate-registration guard, typically hit when the same command is registered twice.","triggerScenarios":"Calling processor.RegisterCommand(\"openapi\", ...), or RegisterCommand(typeof(X)) for a type whose CommandAttribute name is already registered — including case/registry-level duplicates where the exact same key string is added twice during processor setup.","commonSituations":"Registering commands in code that runs twice (double initialization, re-executed bootstrap); two command classes sharing the same CommandAttribute name; plugin loading that re-registers built-in NSwag commands.","solutions":["Guard with processor.ContainsCommand(name) (or check the registry) before calling RegisterCommand.","Fix initialization so command registration runs only once.","Rename one of the conflicting CommandAttribute names.","If overwriting is intended, use the registry's replace/remove API or a try/catch around duplicate registration."],"exampleFix":"// before\nprocessor.RegisterCommand(\"openapi2csclient\", typeof(OpenApiToCSharpClientCommand));\nprocessor.RegisterCommand(\"openapi2csclient\", typeof(MyClientCommand)); // throws\n\n// after\nif (!processor.ContainsCommand(\"openapi2csclient\"))\n    processor.RegisterCommand(\"openapi2csclient\", typeof(MyClientCommand));","handlingStrategy":"validation","validationCode":"if (processor.ContainsCommand(name))\n    Console.Error.WriteLine($\"Command '{name}' already registered; skipping.\");\nelse\n    processor.RegisterCommand(name, commandType);","typeGuard":null,"tryCatchPattern":"try { processor.RegisterCommand(name, commandType); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"has already been added\")) { /* idempotent registration: ignore */ }","preventionTips":["Register commands once during a single bootstrap phase","Guard duplicate registrations with ContainsCommand","Ensure each command's CommandAttribute name is unique across the assembly"],"tags":["cli","command-registration","duplicate-key"],"backgroundTag":"duplicate-registration","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"}