jackwener/OpenCLI · error · ArgumentError

--from 不能晚于 --to

Error message

--from 不能晚于 --to

What it means

After parsing both --from and --to, resolveRange compares their date keys and rejects an inverted range where the start date is after the end date, since that would produce an empty/nonsensical export.

Source

Thrown at clis/mubu/notes.js:75

function dateToKey(d) {
  return `${d.year}-${String(d.month).padStart(2, '0')}-${String(d.day).padStart(2, '0')}`;
}

/** 将各种时间参数统一解析为 {start, end} */
function resolveRange(kwargs) {
  const dateStr = kwargs.date;
  const monthStr = kwargs.month;
  const yearArg = kwargs.year;
  const fromStr = kwargs.from;
  const toStr = kwargs.to;

  // --from / --to 优先级最高
  if (fromStr || toStr) {
    if (!fromStr) throw new ArgumentError('使用 --to 时必须同时指定 --from');
    const start = parseDate(fromStr);
    const end = toStr ? parseDate(toStr) : localToday();
    if (dateToKey(start) > dateToKey(end)) throw new ArgumentError('--from 不能晚于 --to');
    return { start, end };
  }

  if (yearArg !== undefined && yearArg !== null) {
    validateYear(yearArg, '--year');
    return {
      start: { year: yearArg, month: 1, day: 1 },
      end: { year: yearArg, month: 12, day: 31 },
    };
  }

  if (monthStr) {
    const { year, month } = parseMonth(monthStr);
    return {
      start: { year, month, day: 1 },
      end: { year, month, day: lastDayOfMonth(year, month) },
    };
  }

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Swap the values so --from <= --to.
  2. If --to defaults to today, check the machine's date/timezone (system clock may be wrong).
  3. Clamp in code: if (from > to) [from, to] = [to, from] before calling.

Example fix

// before
await notes({ from: '2024-06-01', to: '2024-01-01' });
// after
await notes({ from: '2024-01-01', to: '2024-06-01' });
Defensive patterns

Strategy: validation

Validate before calling

const key = (d) => d.replace(/-/g, '');
if (key(from) > key(to)) throw new Error('--from must not be later than --to');

Type guard

const isOrderedRange = (from, to) => from.replace(/-/g, '') <= to.replace(/-/g, '');

Try / catch

try {
  await notes({ from, to });
} catch (e) {
  if (e.message.includes('--from 不能晚于 --to')) {
    [from, to] = [to, from]; // or report to user
  } else throw e;
}

Prevention

When it happens

Trigger: Running with --from 2024-06-01 --to 2024-01-01, or when --to defaults to localToday while --from is in the future (clock skew / wrong system date).

Common situations: Copy-paste swapped dates, timezone differences making 'today' earlier than the user's local date, or scripts generating ranges with swapped variables.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/9ec7e3f0b488bfa1. Report an issue: GitHub.