chocolatey/choco · error · OptionException

Could not convert string `{0}' to type {1} for option `{2}'.

Error message

Could not convert string `{0}' to type {1} for option `{2}'.

What it means

Thrown by Mono.Options' generic Parse<T> helper when the TypeConverter for type T cannot convert the supplied string. The wrapper catches any conversion exception and rethrows it as an OptionException that names the value, the target type, and the option, preserving the original as an inner exception.

Source

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

        }

        public string[] GetValueSeparators()
        {
            if (_separators == null)
                return new string[0];
            return (string[])_separators.Clone();
        }

		protected static T Parse<T> (string value, OptionContext c)
		{
			TypeConverter conv = TypeDescriptor.GetConverter (typeof (T));
			T t = default (T);
			try {
				if (value != null)
					t = (T) conv.ConvertFromString (value);
			}
			catch (Exception e) {
				throw new OptionException (
						string.Format (
							c.OptionSet.MessageLocalizer ("Could not convert string `{0}' to type {1} for option `{2}'."),
							value, typeof (T).Name, c.OptionName),
						c.OptionName, e);
			}
			return t;
		}

        internal string[] Names { get { return _names; } }
        internal string[] ValueSeparators { get { return _separators; } }

        static readonly char[] _nameTerminator = new char[] { '=', ':' };

        private OptionValueType ParsePrototype()
        {
            char type = '\0';
            List<string> seps = new List<string>();
            for (int i = 0; i < _names.Length; ++i)

View on GitHub (pinned to 0d5abdd10c)

Solutions

  1. Check the option's expected type in Chocolatey help output and supply a value of that type (e.g. an integer for --timeout).
  2. For numeric flags, remove unit suffixes and locale separators: use --timeout 300 not --timeout 300s or --timeout 300,0.
  3. If scripting, validate the value with the target .NET type (int.TryParse, etc.) before appending it to the argument list.

Example fix

// before
choco install pkg --timeout soon

// after
choco install pkg --timeout 300
Defensive patterns

Strategy: validation

Validate before calling

string timeoutArg = configValue;
if (!int.TryParse(timeoutArg, NumberStyles.Integer, CultureInfo.InvariantCulture, out _))
{
    throw new ArgumentException("--timeout expects an integer, got '" + timeoutArg + "'.");
}

Try / catch

try
{
    optionSet.Parse(args);
}
catch (OptionException ex) when (ex.Message.Contains("Could not convert string"))
{
    Console.Error.WriteLine(ex.Message);
    Console.Error.WriteLine("Ensure numeric/typed flags receive a value of the correct type.");
    Environment.Exit(1);
}

Prevention

When it happens

Trigger: A typed option (e.g. an int-valued flag like --timeout or a bool/enum) receives a string that is not parseable by the registered TypeConverter, e.g. --timeout soon or --debug maybe. conv.ConvertFromString throws and is wrapped.

Common situations: Typos in numeric flags, locale-specific number formats (comma vs period decimal separators), or passing a boolean keyword where an integer is expected after a Chocolatey version change widened a flag's accepted type.

Related errors


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