jackwener/OpenCLI · error
index must be a positive integer
Error message
index must be a positive integer
What it means
Inside the browser-evaluated pipeline, the requested post index (after converting to 1-based offset idx = index - 1) is not an integer or is negative. The CLI validates args.index before fetching so that only a valid positive integer selects a post from the user's feed.
Source
Thrown at clis/instagram/comment.js:25
domain: 'www.instagram.com',
args: [
{
name: 'username',
required: true,
positional: true,
help: 'Username of the post author',
},
{ name: 'text', required: true, positional: true, help: 'Comment text' },
{ name: 'index', type: 'int', default: 1, help: 'Post index (1 = most recent)' },
],
columns: ['status', 'user', 'text'],
pipeline: [
{ navigate: 'https://www.instagram.com' },
{ evaluate: `(async () => {
const username = \${{ args.username | json }};
const commentText = \${{ args.text | json }};
const idx = \${{ args.index }} - 1;
if (!Number.isInteger(idx) || idx < 0) throw new Error('index must be a positive integer');
const headers = { 'X-IG-App-ID': '936619743392459' };
const opts = { credentials: 'include', headers };
async function readInstagramJson(response, label) {
try {
return await response.json();
} catch {
throw new Error(label + ' returned invalid JSON');
}
}
function getPostFromFeed(feed, label) {
if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items)) {
throw new Error(label + ' returned malformed items payload');
}
if (idx >= feed.items.length) throw new Error('Post index ' + (idx + 1) + ' not found');
const post = feed.items[idx];
const pkRaw = post?.pk ?? post?.id;
const pk = typeof pkRaw === 'number' ? String(pkRaw) : (typeof pkRaw === 'string' ? pkRaw.trim() : '');
if (!/^\\d+$/.test(pk)) throw new Error(label + ' returned malformed post row');View on GitHub (pinned to 49907e53dc)
Solutions
- Pass a whole-number index starting at 1 (1 = most recent post)
- Strip whitespace and validate with Number.isInteger(Number(index)) && Number(index) >= 1 before invoking
- Fix the config/CLI flag so index is a number, not a string with units or formatting
Example fix
// before instagram comment --username target_user --index 0 --text "nice" // after instagram comment --username target_user --index 1 --text "nice"
Defensive patterns
Strategy: validation
Validate before calling
function isValidIndex(n) {
return Number.isInteger(Number(n)) && Number(n) >= 1;
}
if (!isValidIndex(args.index)) throw new Error('index must be a whole number >= 1'); Type guard
const isPositiveInt = (v) => Number.isInteger(v) && v >= 1;
Try / catch
try {
await commentOnPost({ username, index, text });
} catch (e) {
if (/index must be a positive integer/.test(e.message)) {
console.error('Pass --index as a whole number starting at 1');
} else throw e;
} Prevention
- Always pass index as a 1-based whole number
- Validate CLI flags before invoking
- Avoid copying 0-based indices from other tools
- Don't pass strings with units or formatting as index
When it happens
Trigger: Calling the comment command with index=0, a negative number, a float (e.g. 2.5), or a non-numeric string that resolves to NaN, making idx not a non-negative integer.
Common situations: Copy-pasting a 0-based index from another tool; passing '1st' or an empty --index flag; a config file where index is a string like "3" that fails templating into the pipeline correctly.
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
- Instagram URL is required
- index must be a positive integer
- Collection not found: ' + collectionArg + '. Available: ' +
- archive search sort must be one of ${SORT_OPTIONS.join(', ')
- archive search mediatype must be one of ${MEDIATYPES.join(',
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/a5951fa304472f6a.
Report an issue: GitHub.