{"record":{"id":"e9378bfa0422fc1a","repo":"OrchardCMS/OrchardCore","slug":"switch-0-was-not-found","errorCode":null,"errorMessage":"Switch \"{0}\" was not found","messagePattern":"Switch \"(.+?)\" was not found","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Infrastructure.Abstractions/Commands/DefaultCommandHandler.cs","lineNumber":40,"sourceCode":"    }\n\n    private void SetSwitchValues(CommandContext context)\n    {\n        if (context.Switches != null && context.Switches.Count > 0)\n        {\n            foreach (var commandSwitch in context.Switches)\n            {\n                SetSwitchValue(commandSwitch);\n            }\n        }\n    }\n\n    private void SetSwitchValue(KeyValuePair<string, string> commandSwitch)\n    {\n        // Find the property.\n        var propertyInfo = GetType()\n            .GetProperty(commandSwitch.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase)\n            ?? throw new InvalidOperationException(S[\"Switch \\\"{0}\\\" was not found\", commandSwitch.Key]);\n\n        if (propertyInfo.GetCustomAttributes(typeof(OrchardSwitchAttribute), false).Length == 0)\n        {\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,","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Infrastructure.Abstractions/Commands/DefaultCommandHandler.cs#L22-L58","documentation":"Command handlers in OrchardCore map command-line switches to public properties decorated with [OrchardSwitch]. SetSwitchValue uses reflection to find the property named by the switch; if no such public instance property exists on the handler, it throws. This keeps command invocations strict against typo'd or unsupported switches.","triggerScenarios":"Invoking a command like `help -foo bar` (via SetSwitchValues) where -foo does not correspond to any public property on the command handler class, or with wrong casing despite IgnoreCase binding.","commonSituations":"Typing a switch that belongs to a different command, running an outdated command from docs after a rename, or expecting a switch that the handler never defined.","solutions":["Correct the switch name on the command line to one defined on the target command handler.","Check the handler class for public properties decorated with [OrchardSwitch] and use one of those names.","Run the command's help to list supported switches.","If you own the command, add the missing property plus [OrchardSwitch] attribute to the handler."],"exampleFix":"// before\npublic class MyCommand { /* no property for switch 'verbose' */ }\n// after\npublic class MyCommand {\n  [OrchardSwitch]\n  public bool Verbose { get; set; }\n}","handlingStrategy":"validation","validationCode":"var prop = handlerType.GetProperty(switchName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);\nif (prop is null) throw new InvalidOperationException($\"Unknown switch '{switchName}' for {handlerType.Name}\");","typeGuard":null,"tryCatchPattern":"try { await handler.ExecuteAsync(context); }\ncatch (InvalidOperationException ex) { logger.LogError(ex, \"Invalid switch '{Switch}' for command {Command}\", switchName, commandName); }","preventionTips":["List supported switches from help output before scripting","Keep command-line switch names in sync with handler [OrchardSwitch] properties","Wrap ad-hoc command invocations in try-catch with clear logging"],"tags":["commands","reflection","cli"],"backgroundTag":"resource-not-found","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"}