jackwener/OpenCLI · error · ArgumentError

Invalid Stack Overflow question id: ${args.id}

Error message

Invalid Stack Overflow question id: ${args.id}

What it means

The read command's func throws this ArgumentError when the positional question id is not a purely numeric string (fails /^\d+$/ after trim). Stack Exchange question ids are positive integers, so any other input is rejected up front with a hint to pass a numeric id like 79935770.

Source

Thrown at clis/stackoverflow/read.js:228

cli({
    site: 'stackoverflow',
    name: 'read',
    access: 'read',
    description: 'Read a Stack Overflow question with answers and comments',
    domain: 'stackoverflow.com',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'id', required: true, positional: true, help: 'Stack Overflow question id (numeric, e.g. 79935770)' },
        { name: 'answers-limit', type: 'int', default: 10, help: 'Max answers to include (1-100; accepted answer always included first)' },
        { name: 'comments-limit', type: 'int', default: 5, help: 'Max comments per question/answer (1-100)' },
        { name: 'max-length', type: 'int', default: 4000, help: 'Max characters per body / answer / comment (min 100)' },
    ],
    columns: ['type', 'author', 'score', 'accepted', 'text'],
    func: async (args) => {
        const id = String(args.id || '').trim();
        if (!/^\d+$/.test(id)) {
            throw new ArgumentError(`Invalid Stack Overflow question id: ${args.id}`, 'Pass a numeric id like 79935770');
        }
        const answersLimit = requireBoundedInt(args['answers-limit'] ?? 10, 1, SE_MAX_PAGE_SIZE, 'stackoverflow read --answers-limit');
        const commentsLimit = requireBoundedInt(args['comments-limit'] ?? 5, 1, SE_MAX_PAGE_SIZE, 'stackoverflow read --comments-limit');
        const maxLength = requireMinInt(args['max-length'] ?? 4000, 100, 'stackoverflow read --max-length');

        const label = `stackoverflow/${id}`;
        const qUrl = `${SE_API_BASE}/questions/${id}?site=${SE_SITE}&filter=withbody`;
        const qData = await fetchJson(qUrl, label);
        const question = (qData.items || [])[0];
        if (!question) {
            throw new EmptyResultError(label, 'Question not found');
        }

        // Fetch question comments and answers in parallel.
        const [qCommentsData, answersData] = await Promise.all([
            fetchJson(
                `${SE_API_BASE}/questions/${id}/comments?site=${SE_SITE}&filter=withbody&order=asc&sort=creation&pagesize=${commentsLimit}`,
                `${label}/comments`,

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass just the numeric id: stackoverflow read 79935770
  2. Extract the id from a question URL — the digits after /questions/
  3. Trim whitespace and ensure no leading + or surrounding characters reach the CLI

Example fix

// before
stackoverflow read https://stackoverflow.com/questions/79935770/how-do-i-x
// after
stackoverflow read 79935770
Defensive patterns

Strategy: validation

Validate before calling

const id = String(rawId || '').trim();
if (!/^\d+$/.test(id)) throw new Error(`Pass a numeric question id, got: ${rawId}`);

Type guard

function isStackOverflowQuestionId(v) {
  return typeof v === 'string' && /^\d+$/.test(v.trim());
}

Try / catch

try {
  await runCommand(['stackoverflow', 'read', id]);
} catch (e) {
  if (e instanceof ArgumentError && e.message.startsWith('Invalid Stack Overflow question id')) {
    const m = /questions\/(\d+)/.exec(String(id));
    if (m) return runCommand(['stackoverflow', 'read', m[1]]); // extract id from pasted URL
    console.error('Pass a numeric id like 79935770');
  } else throw e;
}

Prevention

When it happens

Trigger: Running `stackoverflow read` with a URL pasted instead of an id (e.g. https://stackoverflow.com/questions/79935770/...), a slug, an empty string, or a id containing letters or signs like '+79935770'.

Common situations: Copy-pasting the whole question URL from a browser instead of extracting the numeric path segment; shell variable interpolating empty; passing an answer id with a prefix; forgetting to quote an id containing special characters.

Related errors


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