jackwener/OpenCLI · error · ArgumentError

使用 --to 时必须同时指定 --from

Error message

使用 --to 时必须同时指定 --from

What it means

resolveRange treats --from/--to as the highest-priority range spec, but they only make sense as a pair. Supplying --to without --from is ambiguous, so it throws ArgumentError immediately.

Source

Thrown at clis/mubu/notes.js:72

  validateMonth(month);
  return { year, month };
}

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 },

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Add the matching --from flag (e.g. --from 2024-01-01 --to 2024-06-30).
  2. Drop --to to use the default range (or --year).
  3. In code, only set kwargs.to when kwargs.from is also set.

Example fix

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

Strategy: validation

Validate before calling

if ((kwargs.to !== undefined) !== (kwargs.from !== undefined)) {
  throw new Error('--from and --to must be used together');
}

Type guard

const hasFullRange = (k) => k.from != null && k.to != null;

Try / catch

try {
  await notes(kwargs);
} catch (e) {
  if (e.message.includes('使用 --to 时必须同时指定 --from')) {
    console.error('Provide both --from and --to together.');
  } else throw e;
}

Prevention

When it happens

Trigger: Running the notes command with only --to 2024-06-30, or kwargs containing { to: '2024-06-30' } with no from.

Common situations: Users assume --to alone means 'everything until that date'; scripts build kwargs conditionally and set to without from.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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