{"record":{"id":"05c02d525b05be76","repo":"OrchardCMS/OrchardCore","slug":"error-converting-value-0-to-1-for-switch-2","errorCode":null,"errorMessage":"Error converting value \"{0}\" to \"{1}\" for switch \"{2}\"","messagePattern":"Error converting value \"(.+?)\" to \"(.+?)\" for switch \"(.+?)\"","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Infrastructure.Abstractions/Commands/DefaultCommandHandler.cs","lineNumber":61,"sourceCode":"        {\n            throw new InvalidOperationException(S[\"A property \\\"{0}\\\" exists but is not decorated with \\\"{1}\\\"\", commandSwitch.Key, nameof(OrchardSwitchAttribute)]);\n        }\n\n        // Set the value.\n        try\n        {\n            var value = ConvertToType(propertyInfo.PropertyType, commandSwitch.Value);\n            propertyInfo.SetValue(this, value, null /*index*/);\n        }\n        catch (Exception ex) when (!ex.IsFatal())\n        {\n            // TODO: (ngm) fix this message.\n            var message = S[\"Error converting value \\\"{0}\\\" to \\\"{1}\\\" for switch \\\"{2}\\\"\",\n                commandSwitch.Value,\n                propertyInfo.PropertyType.FullName,\n                commandSwitch.Key];\n\n            throw new InvalidOperationException(message, ex);\n        }\n    }\n\n    private async Task InvokeAsync(CommandContext context)\n    {\n        CheckMethodForSwitches(context.CommandDescriptor.MethodInfo, context.Switches);\n\n        var arguments = (context.Arguments ?? []).ToArray();\n        var invokeParameters = GetInvokeParametersForMethod(context.CommandDescriptor.MethodInfo, arguments)\n            ?? throw new InvalidOperationException(S[\"Command arguments \\\"{0}\\\" don't match command definition\", string.Join(\" \", arguments)]);\n\n        Context = context;\n\n        if (context.CommandDescriptor.MethodInfo.ReturnType == typeof(Task<string>))\n        {\n            var taskResult = await (Task<string>)context.CommandDescriptor.MethodInfo.Invoke(this, invokeParameters);\n            await context.Output.WriteAsync(taskResult);\n            return;","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Infrastructure.Abstractions/Commands/DefaultCommandHandler.cs#L43-L79","documentation":"The switch value string is converted to the property's target type via ConvertToType; if conversion fails (FormatException/InvalidCastException etc.), the handler wraps it in this message including the raw value, target type, and switch name. It surfaces type errors from the CLI surface, e.g. passing \"abc\" to a bool switch.","triggerScenarios":"Passing a value that cannot be parsed by the property type in SetSwitchValue: non-numeric text for an int property, an unparseable string for bool/enum/DateTime, or empty value for a required numeric switch.","commonSituations":"Shell scripts passing unvalidated variables into commands, culture-sensitive date/number formats, or users quoting values incorrectly.","solutions":["Fix the command-line value so it parses as the property type shown in the message.","Convert booleans with true/false and enums with exact member names.","If you own the handler, validate/normalize the raw string, or change the property type to string and parse leniently yourself.","Check culture: run with invariant parsing or use culture-neutral formats for numbers/dates."],"exampleFix":"// before\nmycommand -count abc\n// after\nmycommand -count 42","handlingStrategy":"validation","validationCode":"if (!int.TryParse(rawValue, out var count)) throw new FormatException($\"Value '{rawValue}' is not a valid integer for switch '{switchName}'\");","typeGuard":null,"tryCatchPattern":"try { await handler.ExecuteAsync(context); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Error converting value\")) { logger.LogError(ex, \"Switch value type error: {Message}\", ex.Message); }","preventionTips":["Validate user input types before building the command line","Use invariant formats for numbers and dates in scripts","Keep switch property types simple (bool, int, string)"],"tags":["commands","type-conversion","cli"],"backgroundTag":"invalid-argument-value","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}