iOfficeAI/OfficeCLI · error · ArgumentException

Invalid range '{spec}'. Expected 'start:end' with 0-based in

Error message

Invalid range '{spec}'. Expected 'start:end' with 0-based integer character offsets (e.g. '6:11').

What it means

Thrown by ParseCharRange when the spec is not exactly two colon-separated integer tokens. A character range must be 'start:end' with 0-based integer offsets (e.g. '6:11'). Wrong separators, missing a side, or non-integer tokens all trigger this.

Source

Thrown at src/officecli/Core/ParseHelpers.cs:437

    {
        if (!int.TryParse(value, CultureInfo.InvariantCulture, out var result))
            throw new ArgumentException($"Invalid '{propertyName}' value '{value}'. Expected an integer.");
        return result;
    }

    /// <summary>
    /// Parse a "start:end" character-range spec into 0-based, half-open offsets.
    /// Colon separator mirrors the officecli range convention (Excel A1:B2).
    /// Shared by the pptx and docx run-range formatting paths so the two never
    /// diverge (CONSISTENCY(char-range)).
    /// </summary>
    public static (int Start, int End) ParseCharRange(string spec)
    {
        var parts = spec.Split(':');
        if (parts.Length != 2
            || !int.TryParse(parts[0].Trim(), CultureInfo.InvariantCulture, out var start)
            || !int.TryParse(parts[1].Trim(), CultureInfo.InvariantCulture, out var end))
            throw new ArgumentException(
                $"Invalid range '{spec}'. Expected 'start:end' with 0-based integer " +
                "character offsets (e.g. '6:11').");
        if (start < 0 || end < 0)
            throw new ArgumentException($"Invalid range '{spec}': offsets must be non-negative.");
        if (end < start)
            throw new ArgumentException($"Invalid range '{spec}': end ({end}) must be >= start ({start}).");
        return (start, end);
    }

    /// <summary>
    /// Parse a comma-separated list of "start:end" character ranges into 0-based,
    /// half-open offset pairs (e.g. "6:11,20:25" → [(6,11),(20,25)]). A single
    /// range needs no comma. This lets one range= command target several disjoint
    /// spans — the same shape find's format path produces from multiple matches —
    /// so range is a complete addressing alternative wherever the caller already
    /// knows the offsets. Order is preserved; the caller applies them (format-only
    /// run splitting does not shift character offsets, so any order is safe).
    /// </summary>

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Format the spec as start:end with a single colon, e.g. $"{start}:{end}".
  2. Ensure both sides are integers (no units, no decimals).
  3. If you have a list, use the multi-range parser with commas between ranges.

Example fix

// before
var spec = $"{start}-{end}";
ParseCharRange(spec); // throws 298

// after
var spec = $"{start}:{end}";
ParseCharRange(spec);
Defensive patterns

Strategy: validation

Validate before calling

var parts = spec.Split(':');
if (parts.Length != 2
    || !int.TryParse(parts[0].Trim(), CultureInfo.InvariantCulture, out _)
    || !int.TryParse(parts[1].Trim(), CultureInfo.InvariantCulture, out _))
    spec = $"0:0";

Type guard

static bool IsCharRange(string spec)
{ var p = spec.Split(':');
  return p.Length == 2
      && int.TryParse(p[0].Trim(), CultureInfo.InvariantCulture, out _)
      && int.TryParse(p[1].Trim(), CultureInfo.InvariantCulture, out _); }

Try / catch

try { range = ParseCharRange(spec); }
catch (ArgumentException ex) when (ex.Message.Contains("Expected 'start:end'"))
{ /* rebuild spec with colon separator */ }

Prevention

When it happens

Trigger: Passing "6-11" (dash), "6..11", "6" (missing end), "6:11:20" (too many parts), or "abc:def". Also "6 : 11" is fine because each side is trimmed, but "6;11" is not.

Common situations: Copy-pasting a range from a tool that uses a different delimiter; building the spec by joining with the wrong character; off-by formatting from find's match offsets expressed differently.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/158e4066ed42349e. Report an issue: GitHub.