chocolatey/choco · error · OptionException

Missing required value for option '{0}'.

Error message

Missing required value for option '{0}'.

What it means

Thrown by Mono.Options OptionSet when an option declared with OptionValueType.Required is accessed but fewer values were supplied on the command line than MaxValueCount requires. AssertValid detects that the required value slot (index) is beyond what was actually provided and raises an OptionException naming the offending option.

Source

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

		void IList.RemoveAt (int index)             {(values as IList).RemoveAt (index);}
		bool IList.IsFixedSize                      {get {return false;}}
		object IList.this [int index]               {get {return this [index];} set {(values as IList)[index] = value;}}
		#endregion

		#region IList<T>
		public int IndexOf (string item)            {return values.IndexOf (item);}
		public void Insert (int index, string item) {values.Insert (index, item);}
		public void RemoveAt (int index)            {values.RemoveAt (index);}

		private void AssertValid (int index)
		{
			if (c.Option == null)
				throw new InvalidOperationException ("OptionContext.Option is null.");
			if (index >= c.Option.MaxValueCount)
				throw new ArgumentOutOfRangeException ("index");
			if (c.Option.OptionValueType == OptionValueType.Required &&
					index >= values.Count)
				throw new OptionException (string.Format (
							c.OptionSet.MessageLocalizer ("Missing required value for option '{0}'."), c.OptionName),
						c.OptionName);
		}

		public string this [int index] {
			get {
				AssertValid (index);
				return index >= values.Count ? null : values [index];
			}
			set {
				values [index] = value;
			}
		}
		#endregion

		public List<string> ToList ()
		{
			return new List<string> (values);

View on GitHub (pinned to 0d5abdd10c)

Solutions

  1. Re-run with the missing value supplied directly after the option: --cache-location <path>.
  2. Quote the value as a single argument if it contains spaces: "--cache-location=<path with spaces>".
  3. Inspect the full command line for flags accidentally placed where they consume the value (e.g. --limitoutput directly before a value-needing option).

Example fix

// before
choco install pkg --cache-location

// after
choco install pkg --cache-location C:/choco/cache
Defensive patterns

Strategy: try-catch

Validate before calling

// Before invoking choco as a process, validate that every required-value option
// is followed by a non-flag token.
static bool HasValueForRequiredOption(string[] args, string flag)
{
    for (int i = 0; i < args.Length; i++)
        if (args[i] == flag || args[i].StartsWith(flag + "="))
            return args[i].Contains("=") || (i + 1 < args.Length && !args[i + 1].StartsWith("-"));
    return true; // flag absent, nothing to satisfy
}

Try / catch

try
{
    optionSet.Parse(args);
}
catch (OptionException ex) when (ex.Message.Contains("Missing required value"))
{
    Console.Error.WriteLine("Argument error: " + ex.Message);
    Console.Error.WriteLine("Supply a value for each listed option.");
    Environment.Exit(1);
}

Prevention

When it happens

Trigger: Invoking a Chocolatey command line like `--cache-location` (a required-value option) at the end of the argument list with no following value, or where the value was consumed by another token. The indexer reaches a required position with values.Count not large enough.

Common situations: Quoting/escaping mistakes that detach a value from its flag, or option ordering where a later flag swallows the intended value. Auto-generated command lines from scripts that omit a value under certain conditions.

Related errors


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