jackwener/OpenCLI · error · CliError

INVALID_ARGUMENT

INVALID_ARGUMENT

Error message

Unknown group "${group}". Valid: main, hk, us, all

What it means

This CliError with code INVALID_ARGUMENT is thrown before any network call when the group argument is not one of the defined index groups. Valid values are exactly main, hk, us, or all (case-sensitive). The library uses INDEX_GROUPS as a lookup table and fails fast on unknown keys.

Source

Thrown at clis/eastmoney/index-board.js:60

  args: [
    {
      name: 'group',
      type: 'string',
      default: 'main',
      help: '指数分组:main (A股主要), hk (港股), us (美股), all',
    },
  ],
  columns: ['code', 'name', 'price', 'changePercent', 'change', 'open', 'high', 'low', 'prevClose'],
  func: async (args) => {
    const group = String(args.group ?? 'main').toLowerCase();
    /** @type {[string,string][]} */
    let entries;
    if (group === 'all') {
      entries = [...INDEX_GROUPS.main, ...INDEX_GROUPS.hk, ...INDEX_GROUPS.us];
    } else if (INDEX_GROUPS[group]) {
      entries = INDEX_GROUPS[group];
    } else {
      throw new CliError('INVALID_ARGUMENT', `Unknown group "${group}". Valid: main, hk, us, all`);
    }

    const secids = entries.map(([secid]) => secid).join(',');
    const url = new URL('https://push2.eastmoney.com/api/qt/ulist.np/get');
    url.searchParams.set('secids', secids);
    url.searchParams.set('fltt', '2');
    url.searchParams.set('fields', FIELDS);
    url.searchParams.set('ut', 'bd1d9ddb04089700cf9c27f6f7426281');

    const resp = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0', Accept: 'application/json' } });
    if (!resp.ok) throw new CliError('HTTP_ERROR', `eastmoney index-board failed: HTTP ${resp.status}`);
    const data = await resp.json();
    const diff = Array.isArray(data?.data?.diff) ? data.data.diff : [];
    if (diff.length === 0) throw new CliError('NO_DATA', 'eastmoney returned no index data');

    // Preserve the order defined in INDEX_GROUPS regardless of API ordering
    const byCode = new Map(diff.map((it) => [String(it.f12), it]));
    return entries

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use one of the exact valid values: main, hk, us, or all
  2. Normalize input first: group.trim().toLowerCase() before calling, keeping in mind only lowercase keys are valid
  3. Check the CLI help / INDEX_GROUPS definition for the currently supported group names
  4. Validate the group parameter in your own config loading and reject unknown values early

Example fix

// before
await indexBoard({ group: 'Main' }) // throws INVALID_ARGUMENT
// after
const group = String(rawGroup ?? 'all').trim().toLowerCase();
await indexBoard({ group }) // 'main' | 'hk' | 'us' | 'all'
Defensive patterns

Strategy: validation

Validate before calling

const VALID_GROUPS = new Set(['main', 'hk', 'us', 'all']);
function validateGroup(group) {
  const g = String(group ?? 'all').trim().toLowerCase();
  if (!VALID_GROUPS.has(g)) {
    throw new Error(`Unknown group "${g}". Valid: main, hk, us, all`);
  }
  return g;
}

Type guard

function isValidGroup(g) {
  return typeof g === 'string' && ['main', 'hk', 'us', 'all'].includes(g);
}

Try / catch

try {
  const board = await getIndexBoard({ group: validateGroup(userInput) });
} catch (err) {
  if (err.code === 'INVALID_ARGUMENT') {
    console.error(`Bad group: ${err.message}. Defaulting to 'all'.`);
    return getIndexBoard({ group: 'all' });
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling index-board with group values such as 'Main', 'MAIN', 'china', 'hs300', '', undefined-as-string, or any key absent from INDEX_GROUPS. Note 'Main' with different casing also fails because the lookup is case-sensitive.

Common situations: Typos or wrong casing in CLI flags/config files; passing a user-supplied group string without normalizing; documentation drift after group names changed between versions; scripts passing region names like 'asia' that never existed.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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