{"record":{"id":"9d416f508b63a741","repo":"RicoSuter/NSwag","slug":"either-the-argument-name-or-position-can-be-set-but-not-both","errorCode":null,"errorMessage":"Either the argument Name or Position can be set, but not both.","messagePattern":"Either the argument Name or Position can be set, but not both\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/NSwag.Commands/NConsole/ArgumentAttribute.cs","lineNumber":42,"sourceCode":"\n        /// <summary>Gets or sets a value indicating whether to prompt the user for the value.</summary>\n        public bool ShowPrompt { get; set; } = true;\n\n        /// <summary>Gets the argument value.</summary>\n        /// <param name=\"consoleHost\">The command line host.</param>\n        /// <param name=\"args\">The arguments.</param>\n        /// <param name=\"property\">The property.</param>\n        /// <param name=\"command\">The command.</param>\n        /// <param name=\"input\">The output from the previous command in the chain.</param>\n        /// <param name=\"used\">Indicates whether a value for the property was found in the given arguments.</param>\n        /// <returns>The value.</returns>\n        /// <exception cref=\"System.InvalidOperationException\">Either the argument Name or Position can be set, but not both.</exception>\n        /// <exception cref=\"InvalidOperationException\">Either the argument Name or Position can be set, but not both.</exception>\n        /// <exception cref=\"InvalidOperationException\">The parameter has no default value.</exception>\n        public override object GetValue(IConsoleHost consoleHost, string[] args, PropertyInfo property, IConsoleCommand command, object input, out string used)\n        {\n            if (!string.IsNullOrEmpty(Name) && Position > 0)\n                throw new InvalidOperationException(\"Either the argument Name or Position can be set, but not both.\");\n\n            used = null;\n            string value = null;\n\n            if (TryGetPositionalArgumentValue(args, ref used, out value))\n            {\n                return ArgumentAttributeBase.ConvertToType(value, property.PropertyType);\n            }\n                \n            if (TryGetNamedArgumentValue(args, ref used, out value))\n            {\n                return ArgumentAttributeBase.ConvertToType(value, property.PropertyType);\n            }\n                \n            if (AcceptsCommandInput && input != null)\n                return input;\n\n            if (!ArgumentAttribute.IsInteractiveMode(args) && !IsRequired)","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/RicoSuter/NSwag/blob/63daf8fcc3a25151b62eb4b326a1e8ea048a0d41/src/NSwag.Commands/NConsole/ArgumentAttribute.cs#L24-L60","documentation":"NConsole's ArgumentAttribute binds a command property either by a named option (Name) or by positional order (Position). GetValue validates the attribute configuration up front: specifying both a non-empty Name and a Position > 0 is contradictory and throws InvalidOperationException. This is an attribute-definition bug in the command class, not a user input error.","triggerScenarios":"A command property is decorated like [Argument(Name = \"output\", Position = 1)] — both a name and a position — and the processor invokes GetValue while parsing the command line.","commonSituations":"Copy-pasting attribute declarations from positional arguments onto named arguments (or vice versa) without removing the other property; adding Position when converting a positional arg to a named flag.","solutions":["Remove Position from the attribute if the argument should be a named option (/name:value).","Remove Name (keep Position >= 1) if the argument should be positional.","Search the command class for other [Argument(...)] declarations combining Name and Position to fix them all."],"exampleFix":"// before\n[Argument(Name = \"output\", Position = 1)]\npublic string Output { get; set; }\n\n// after (named)\n[Argument(Name = \"output\")]\npublic string Output { get; set; }","handlingStrategy":"validation","validationCode":"var attr = property.GetCustomAttribute<ArgumentAttribute>();\nif (attr != null && !string.IsNullOrEmpty(attr.Name) && attr.Position > 0)\n    throw new InvalidOperationException($\"{property.DeclaringType}.{property.Name}: ArgumentAttribute cannot set both Name and Position.\");","typeGuard":null,"tryCatchPattern":"try { processor.RegisterCommand(typeof(MyCommand)); processor.Process(args); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Name or Position can be set, but not both\")) { Console.Error.WriteLine(\"Fix the [Argument] attribute — choose Name OR Position.\"); }","preventionTips":["Decide per argument: named (Name only) or positional (Position only)","When copy-pasting attribute declarations, delete the unused property","Add a unit test that registers all command types to catch bad attributes early"],"tags":["attributes","cli-parsing","configuration"],"backgroundTag":"mutually-exclusive-options","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"}