{"record":{"id":"a2189aaaf4da221c","repo":"chocolatey/choco","slug":"could-not-convert-string-0-to-type-1-for-opt","errorCode":null,"errorMessage":"Could not convert string `{0}' to type {1} for option `{2}'.","messagePattern":"Could not convert string `(.+?)' to type (.+?) for option `(.+?)'\\.","errorType":"exception","errorClass":"OptionException","httpStatus":null,"severity":"error","filePath":"src/chocolatey/infrastructure/commandline/Options.cs","lineNumber":360,"sourceCode":"        }\r\n\r\n        public string[] GetValueSeparators()\r\n        {\r\n            if (_separators == null)\r\n                return new string[0];\r\n            return (string[])_separators.Clone();\r\n        }\r\n\r\n\t\tprotected static T Parse<T> (string value, OptionContext c)\r\n\t\t{\r\n\t\t\tTypeConverter conv = TypeDescriptor.GetConverter (typeof (T));\r\n\t\t\tT t = default (T);\r\n\t\t\ttry {\r\n\t\t\t\tif (value != null)\r\n\t\t\t\t\tt = (T) conv.ConvertFromString (value);\r\n\t\t\t}\r\n\t\t\tcatch (Exception e) {\r\n\t\t\t\tthrow new OptionException (\r\n\t\t\t\t\t\tstring.Format (\r\n\t\t\t\t\t\t\tc.OptionSet.MessageLocalizer (\"Could not convert string `{0}' to type {1} for option `{2}'.\"),\r\n\t\t\t\t\t\t\tvalue, typeof (T).Name, c.OptionName),\r\n\t\t\t\t\t\tc.OptionName, e);\r\n\t\t\t}\r\n\t\t\treturn t;\r\n\t\t}\r\n\r\n        internal string[] Names { get { return _names; } }\r\n        internal string[] ValueSeparators { get { return _separators; } }\r\n\r\n        static readonly char[] _nameTerminator = new char[] { '=', ':' };\r\n\r\n        private OptionValueType ParsePrototype()\r\n        {\r\n            char type = '\\0';\r\n            List<string> seps = new List<string>();\r\n            for (int i = 0; i < _names.Length; ++i)\r","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/chocolatey/choco/blob/0d5abdd10cc177a141e69547cad6935b419b6c17/src/chocolatey/infrastructure/commandline/Options.cs#L342-L378","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the option's expected type in Chocolatey help output and supply a value of that type (e.g. an integer for --timeout).","For numeric flags, remove unit suffixes and locale separators: use --timeout 300 not --timeout 300s or --timeout 300,0.","If scripting, validate the value with the target .NET type (int.TryParse, etc.) before appending it to the argument list."],"exampleFix":"// before\nchoco install pkg --timeout soon\n\n// after\nchoco install pkg --timeout 300","handlingStrategy":"validation","validationCode":"string timeoutArg = configValue;\nif (!int.TryParse(timeoutArg, NumberStyles.Integer, CultureInfo.InvariantCulture, out _))\n{\n    throw new ArgumentException(\"--timeout expects an integer, got '\" + timeoutArg + \"'.\");\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    optionSet.Parse(args);\n}\ncatch (OptionException ex) when (ex.Message.Contains(\"Could not convert string\"))\n{\n    Console.Error.WriteLine(ex.Message);\n    Console.Error.WriteLine(\"Ensure numeric/typed flags receive a value of the correct type.\");\n    Environment.Exit(1);\n}","preventionTips":["Validate typed flag values with the corresponding TryParse before building the command line.","Use invariant-culture formatting for numbers to avoid locale-related conversion failures."],"tags":["commandline","mono-options","type-conversion","argument-parsing"],"backgroundTag":null,"analyzedSha":"0d5abdd10cc177a141e69547cad6935b419b6c17","analyzedAt":"2026-08-13T18:33:03.301Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}