jackwener/OpenCLI · error · ArgumentError

wikipedia lang must be a language code like en, zh, ja (got

Error message

wikipedia lang must be a language code like en, zh, ja (got "${args.lang}")

What it means

The lang argument must match /^[a-z]{2,3}(?:-[a-z0-9]+)?$/ after lowercase/trim, because it is interpolated directly into the hostname https://<lang>.wikipedia.org. If it does not look like an ISO language code (optionally with a region suffix), an ArgumentError is thrown before any fetch.

Source

Thrown at clis/wikipedia/page.js:34

    access: 'read',
    description: 'Full plain-text extract of a Wikipedia article (optional paragraph cap).',
    domain: 'wikipedia.org',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'title', positional: true, required: true, type: 'string', help: 'Article title (e.g. "Transformer (machine learning model)")' },
        { name: 'lang', type: 'string', default: 'en', help: 'Language code (en, zh, ja, de, ...).' },
        { name: 'paragraphs', type: 'int', default: 0, help: 'Cap to first N paragraphs (0 = full article).' },
    ],
    columns: ['title', 'description', 'pageId', 'paragraphs', 'extract', 'url'],
    func: async (args) => {
        const title = String(args.title ?? '').trim();
        if (!title) {
            throw new ArgumentError('wikipedia page title cannot be empty');
        }
        const lang = String(args.lang ?? 'en').trim().toLowerCase();
        if (!/^[a-z]{2,3}(?:-[a-z0-9]+)?$/.test(lang)) {
            throw new ArgumentError(`wikipedia lang must be a language code like en, zh, ja (got "${args.lang}")`);
        }
        const paragraphsCap = Number(args.paragraphs ?? 0);
        if (!Number.isInteger(paragraphsCap) || paragraphsCap < 0) {
            throw new ArgumentError('paragraphs must be a non-negative integer (0 = full article)');
        }

        const url = new URL(`https://${lang}.wikipedia.org/w/api.php`);
        url.searchParams.set('action', 'query');
        url.searchParams.set('format', 'json');
        url.searchParams.set('formatversion', '2');
        url.searchParams.set('prop', 'extracts|info|description');
        url.searchParams.set('inprop', 'url');
        url.searchParams.set('explaintext', '1');
        url.searchParams.set('redirects', '1');
        url.searchParams.set('titles', title);

        let resp;
        try {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a 2-3 letter lowercase ISO 639 code, optionally with a short region suffix (en, zh, ja, zh-cn)
  2. Normalize OS locales: strip encoding and convert _ to - and lowercase before passing
  3. Omit lang entirely to use the default 'en'

Example fix

// before
const lang = process.env.LANG; // "en_US.UTF-8"
await run(['wikipedia', 'page', title, '--lang', lang]);
// after
const lang = (process.env.LANG || 'en').split('.')[0].replace('_', '-').toLowerCase(); // "en-us" — or map to "en"
await run(['wikipedia', 'page', title, '--lang', lang]);
Defensive patterns

Strategy: validation

Validate before calling

const lang = String(rawLang ?? 'en').trim().toLowerCase();
if (!/^[a-z]{2,3}(?:-[a-z0-9]+)?$/.test(lang)) throw new Error(`bad lang: ${rawLang}`);

Type guard

function isWikiLang(v) { return typeof v === 'string' && /^[a-z]{2,3}(?:-[a-z0-9]+)?$/.test(v.trim().toLowerCase()); }

Try / catch

try {
  return await pageCommand({ ...args, lang: normalizedLang });
} catch (err) {
  if (err instanceof ArgumentError && err.message.includes('lang')) {
    console.error(`invalid lang "${args.lang}"; using "en"`);
    return await pageCommand({ ...args, lang: 'en' });
  }
  throw err;
}

Prevention

When it happens

Trigger: Passing a full locale like 'en_US' or 'pt-BR' with underscore/uppercase; passing a display name like 'english' or 'Chinese'; passing an empty or numeric lang value; casing like 'EN' (fixed by trim/lowercase, but 'en-US' is fine while 'eng-US' is not).

Common situations: Deriving lang from an OS locale (e.g. en_US.UTF-8) without normalizing; users typing language names instead of codes; i18n frameworks exporting BCP-47 tags with script subtags (zh-Hans fails here).

Related errors


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