chocolatey/choco · error · OptionException

Error: Found {0} option values when expecting {1}.

Error message

Error: Found {0} option values when expecting {1}.

What it means

Thrown by Mono.Options during value splitting when more option values are collected than the option's MaxValueCount allows. After splitting on ValueSeparators, if c.OptionValues.Count exceeds MaxValueCount (and the option is not Optional), the parser rejects the surplus as malformed input.

Source

Thrown at src/chocolatey/infrastructure/commandline/Options.cs:858

			return false;
		}

        private void ParseValue(string option, OptionContext c)
        {
            if (option != null)
                foreach (string o in c.Option.ValueSeparators != null
                        ? option.Split(c.Option.ValueSeparators, StringSplitOptions.None)
                        : new string[] { option })
                {
                    c.OptionValues.Add(o);
                }
            if (c.OptionValues.Count == c.Option.MaxValueCount ||
                    c.Option.OptionValueType == OptionValueType.Optional)
                c.Option.Invoke(c);
            else if (c.OptionValues.Count > c.Option.MaxValueCount)
            {
                throw new OptionException(_localizer(string.Format(
                                "Error: Found {0} option values when expecting {1}.",
                                c.OptionValues.Count, c.Option.MaxValueCount)),
                        c.OptionName);
            }
        }

		private bool ParseBool (string option, string n, OptionContext c)
		{
			Option p;
			string rn;
			if (n.Length >= 1 && (n [n.Length-1] == '+' || n [n.Length-1] == '-') &&
					Contains ((rn = n.Substring (0, n.Length-1)))) {
				p = this [rn];
				string v = n [n.Length-1] == '+' ? option : null;
				c.OptionName  = option;
				c.Option      = p;
				c.OptionValues.Add (v);
				p.Invoke (c);

View on GitHub (pinned to 0d5abdd10c)

Solutions

  1. Pass only as many values as the option accepts; check the option's MaxValueCount/separator definition in Chocolatey docs.
  2. Remove extra separators or wrap multi-token values so they are a single argument (quote them, or remove the separator character).
  3. If you need multiple values, use the option the framework intends (e.g. repeat the flag) rather than comma-packing.

Example fix

// before (option expects single value, comma is a separator)
choco install pkg --some-opt=a,b

// after
choco install pkg --some-opt=a
Defensive patterns

Strategy: try-catch

Validate before calling

// If an option has MaxValueCount N, ensure you pass at most N separated values.
int max = option.MaxValueCount;
int provided = value.Split(option.ValueSeparators ?? Array.Empty<string>(), StringSplitOptions.None).Length;
if (provided > max)
    throw new ArgumentException("Option expects at most " + max + " values, got " + provided + ".");

Try / catch

try
{
    optionSet.Parse(args);
}
catch (OptionException ex) when (ex.Message.Contains("option values when expecting"))
{
    Console.Error.WriteLine(ex.Message);
    Console.Error.WriteLine("Reduce the number of values/separator-packed tokens for that option.");
    Environment.Exit(1);
}

Prevention

When it happens

Trigger: An option that expects a fixed count of values receives extra separators, e.g. an option with MaxValueCount=1 given --flag=a,b where a comma is a value separator, producing two values for a single-value option. Also triggered by repeating a value after = repeatedly.

Common situations: Defining an option with a separator list and then passing more separated tokens than expected, or a Chocolatey flag whose arity changed between versions now rejecting formerly-accepted multi-value input.

Related errors


AI-assisted analysis of chocolatey/choco@0d5abdd10c (2026-08-13). Data as JSON: /api/errors/512a6c75f23b8683. Report an issue: GitHub.