RicoSuter/NSwag · error · InvalidOperationException
Either the argument Name or Position can be set, but not…
Error message
Either the argument Name or Position can be set, but not both.
What it means
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.
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.
Example fix
// before
[Argument(Name = "output", Position = 1)]
public string Output { get; set; }
// after (named)
[Argument(Name = "output")]
public string Output { get; set; } Defensive patterns
Strategy: validation
Validate before calling
var attr = property.GetCustomAttribute<ArgumentAttribute>();
if (attr != null && !string.IsNullOrEmpty(attr.Name) && attr.Position > 0)
throw new InvalidOperationException($"{property.DeclaringType}.{property.Name}: ArgumentAttribute cannot set both Name and Position."); Try / catch
try { processor.RegisterCommand(typeof(MyCommand)); processor.Process(args); }
catch (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."); } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- This UI does not support multiple documents per UI: Do not…
- The OpenAPI/Swagger document
- No registered OpenAPI/Swagger document found for the…
- Some operations are both in included and excluded operation…
- PropertyNameGenerator not set.
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/9d416f508b63a741.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Commands/NConsole/ArgumentAttribute.cs:42
/// <summary>Gets or sets a value indicating whether to prompt the user for the value.</summary>
public bool ShowPrompt { get; set; } = true;
/// <summary>Gets the argument value.</summary>
/// <param name="consoleHost">The command line host.</param>
/// <param name="args">The arguments.</param>
/// <param name="property">The property.</param>
/// <param name="command">The command.</param>
/// <param name="input">The output from the previous command in the chain.</param>
/// <param name="used">Indicates whether a value for the property was found in the given arguments.</param>
/// <returns>The value.</returns>
/// <exception cref="System.InvalidOperationException">Either the argument Name or Position can be set, but not both.</exception>
/// <exception cref="InvalidOperationException">Either the argument Name or Position can be set, but not both.</exception>
/// <exception cref="InvalidOperationException">The parameter has no default value.</exception>
public override object GetValue(IConsoleHost consoleHost, string[] args, PropertyInfo property, IConsoleCommand command, object input, out string used)
{
if (!string.IsNullOrEmpty(Name) && Position > 0)
throw new InvalidOperationException("Either the argument Name or Position can be set, but not both.");
used = null;
string value = null;
if (TryGetPositionalArgumentValue(args, ref used, out value))
{
return ArgumentAttributeBase.ConvertToType(value, property.PropertyType);
}
if (TryGetNamedArgumentValue(args, ref used, out value))
{
return ArgumentAttributeBase.ConvertToType(value, property.PropertyType);
}
if (AcceptsCommandInput && input != null)
return input;
if (!ArgumentAttribute.IsInteractiveMode(args) && !IsRequired)View on GitHub (pinned to 63daf8fcc3)