OrchardCMS/OrchardCore · error · ArgumentException

Invalid switch syntax

Error message

Invalid switch syntax: "{0}". Valid syntax is /<switchName>[:<switchValue>].

What it means

CommandParametersParser.Parse accepts switches in the form /<switchName>[:<switchValue>]. An argument starting with '/' that yields an empty switch name (e.g. a bare '/' or '/:value') fails this format and throws ArgumentException.

Solutions

  1. Correct the argument so it follows /name or /name:value syntax.
  2. Guard programmatically built args: skip or fix any arg where the segment after '/' is empty.
  3. Quote or escape slashes that are path prefixes rather than switches if the parser should treat them as values.

Example fix

// before
var args = new[] { "/", "Foo" }; // empty switch name
parser.Parse(args);
// after
var args = new[] { "/Foo" };
parser.Parse(args);
Defensive patterns

Strategy: validation

Validate before calling

foreach (var arg in args.Where(a => a.StartsWith('/')))
    if (arg.Length < 2 || arg[1] == ':') throw new FormatException($"Invalid switch: {arg}");

Try / catch

try { parser.Parse(args); }
catch (ArgumentException ex) when (ex.Message.StartsWith("Invalid switch syntax")) { /* print usage */ }

Prevention

When it happens

Trigger: Calling Parse with a command-line args array containing an empty switch like "/", "/:", or "/:foo".

Common situations: Typo in a command line; programmatic construction of args where a variable switch name is empty; scripted command invocations producing stray slashes.

Understand the failure class

Background: "Unknown argument", "Invalid value", and "must be one of": invalid CLI argument errors explained — this error's family across 35 libraries.

Related errors


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/6be84617db960d33. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore/OrchardCore.Infrastructure/Commands/Parameters/CommandParametersParser.cs:24

{
    [SecurityCritical]
    public CommandParameters Parse(IEnumerable<string> args)
    {
        var arguments = new List<string>();
        var switches = new Dictionary<string, string>();

        foreach (var arg in args)
        {
            // Switch?
            if (arg[0] == '/')
            {
                var index = arg.IndexOf(':');
                var switchName = index < 0 ? arg[1..] : arg[1..index];
                var switchValue = index < 0 || index >= arg.Length ? string.Empty : arg[(index + 1)..];

                if (string.IsNullOrEmpty(switchName))
                {
                    throw new ArgumentException(string.Format("Invalid switch syntax: \"{0}\". Valid syntax is /<switchName>[:<switchValue>].", arg));
                }

                switches.Add(switchName, switchValue);
            }
            else
            {
                arguments.Add(arg);
            }
        }

        return new CommandParameters
        {
            Arguments = arguments,
            Switches = switches,
        };
    }
}

View on GitHub (pinned to 4306c0717f)