jackwener/OpenCLI · error · ArgumentError

--index must be a positive integer

Error message

--index must be a positive integer

What it means

setInstagramPostLike requires --index to be an integer >= 1 (1-based position in the profile's recent posts). This ArgumentError fires when index is missing, not an integer (e.g. a string '2' or float 1.5), or is less than 1, because such a value can never address a valid post.

Source

Thrown at clis/instagram/_shared/post-like.js:182

    }
    if (confirmed.liked !== shouldLike) {
        throw new CommandExecutionError(
            `Instagram did not persist the ${shouldLike ? 'like' : 'unlike'} on ${expectedPost.code}`,
            'The action may have been rejected. Retry later, or check the post in the browser.',
        );
    }
    return confirmed;
}

export async function setInstagramPostLike(page, kwargs, shouldLike) {
    const username = String(kwargs.username || '').trim();
    const index = kwargs.index;
    const command = shouldLike ? 'instagram like' : 'instagram unlike';
    if (!username) {
        throw new ArgumentError('username is required');
    }
    if (!Number.isInteger(index) || index < 1) {
        throw new ArgumentError('--index must be a positive integer', 'e.g. --index 2 for the second most recent post');
    }

    const snapshot = await readPostSnapshot(page, username, index, command);
    const post = pickPost(snapshot, username, index, command);
    const label = post.caption || `(post #${index})`;
    const settled = [{ status: shouldLike ? 'Already liked' : 'Already unliked', user: username, post: label }];
    if (post.liked === shouldLike) return settled;

    await page.goto(`https://www.instagram.com/p/${post.code}/`, { settleMs: 2000 });
    let click = null;
    for (let attempt = 0; attempt < 5 && !click?.found; attempt += 1) {
        if (attempt > 0) await page.sleep(1);
        click = unwrapEvaluateResult(await page.evaluate(buildToggleLikeJs(shouldLike)));
    }
    if (!click?.found) {
        throw new CommandExecutionError(
            `Could not find the like control on ${post.code}`,
            'Open the post in the browser and check whether Instagram is asking you to log in, or set the interface language to English.',

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass --index as a positive integer (1 = most recent post)
  2. Coerce and validate in wrappers: Number.isInteger(Number.parseInt(v, 10)) && v >= 1
  3. Remember the index is 1-based, not 0-based

Example fix

// before
await setInstagramPostLike(page, { username: 'user', index: 0 }, true);
// after
await setInstagramPostLike(page, { username: 'user', index: 1 }, true);
Defensive patterns

Strategy: validation

Validate before calling

const idx = Number.parseInt(kwargs.index, 10);
if (!Number.isInteger(idx) || idx < 1) throw new Error('--index must be a positive integer (1-based)');

Type guard

function isValidIndexArg(v) { return Number.isInteger(v) && v >= 1; }

Try / catch

try { await setInstagramPostLike(page, kwargs, true); } catch (e) { if (e instanceof ArgumentError && /--index/.test(e.message)) { console.error(`Bad --index: ${JSON.stringify(kwargs.index)}; pass an integer >= 1`); } else throw e; }

Prevention

When it happens

Trigger: Omitting --index; passing --index 0 or a negative number; passing a non-numeric or string value from shell wrappers that don't coerce types; float values from JSON configs.

Common situations: Thinking the index is 0-based and passing 0; shell scripts interpolating unquoted/empty variables; YAML/JSON configs storing the index as a string.

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/881063a0075b93e0. Report an issue: GitHub.