{"record":{"id":"47860c93d0edcd24","repo":"jackwener/OpenCLI","slug":"index-must-be-between-1-and-max-for-this-job","errorCode":null,"errorMessage":"--index must be between 1 and ${max} for this job","messagePattern":"--index must be between 1 and (.+?) for this job","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/midjourney/utils.js","lineNumber":86,"sourceCode":"      return match[1].toLowerCase();\n    }\n  } catch {}\n  throw new ArgumentError(\n    'job-id must be a Midjourney UUID or https://www.midjourney.com/jobs/<uuid> URL',\n    'Example: opencli midjourney status d5664250-5f1f-4cd0-9637-2ce0153dd30a',\n  );\n}\n\nexport function parseImageIndices(value, batchSize = 4) {\n  const max = Number.isInteger(batchSize) && batchSize > 0 ? batchSize : 4;\n  const raw = String(value ?? 'all').trim().toLowerCase();\n  if (!raw || raw === 'all') return Array.from({ length: max }, (_, index) => index);\n  if (!/^\\d+$/.test(raw)) {\n    throw new ArgumentError(`--index must be \"all\" or an integer from 1 to ${max}`);\n  }\n  const userIndex = Number(raw);\n  if (userIndex < 1 || userIndex > max) {\n    throw new ArgumentError(`--index must be between 1 and ${max} for this job`);\n  }\n  return [userIndex - 1];\n}\n\nexport function normalizePrompt(value) {\n  const prompt = String(value ?? '').replace(/\\s+/g, ' ').trim();\n  if (!prompt) {\n    throw new ArgumentError(\n      'prompt cannot be empty',\n      'Example: opencli midjourney generate \"a blue ceramic teapot --ar 1:1\"',\n    );\n  }\n  return prompt;\n}\n\nexport function promptFromFullCommand(value) {\n  return String(value ?? '')\n    .replace(/^\\s*\\/?imagine\\s*(?:prompt\\s*:)?\\s*/i, '')","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/midjourney/utils.js#L68-L104","documentation":"This is the range branch of parseImageIndices: the --index value is a valid integer but falls outside 1..max, where max equals the job's batch size (default 4). It throws \"--index must be between 1 and ${max} for this job\". The bound is dynamic per job, so a smaller batch lowers the allowed maximum.","triggerScenarios":"Passing --index 0 (0-based habit), or --index 5+ when the job produced a 4-image batch, or any index larger than the actual number of images the job generated (e.g. a job that produced fewer images).","commonSituations":"Users forgetting Midjourney indices are 1-based, targeting an image that doesn't exist because the generation failed partway or returned fewer images, or reusing an index from a job with a different batch size.","solutions":["Use an index between 1 and the batch size shown in the error message (e.g. 1-4 for a standard job)","Use --index all if you want every image in the job","Verify how many images the job actually produced before selecting an index","If the target image should exist, re-check the job status first — the batch may be smaller than expected"],"exampleFix":"// before\nopencli midjourney upscale <job-id> --index 0\n// after\nopencli midjourney upscale <job-id> --index 1","handlingStrategy":"validation","validationCode":"function clampIndex(value, batchSize = 4) {\n  const n = Number(value);\n  if (!Number.isInteger(n) || n < 1 || n > batchSize) {\n    throw new Error(`index must be between 1 and ${batchSize}`);\n  }\n  return n;\n}\nconst index = clampIndex(opts.index, job.batchSize ?? 4);","typeGuard":"function isIndexInRange(v, max) {\n  return Number.isInteger(v) && v >= 1 && v <= max;\n}","tryCatchPattern":"try {\n  await upscale(jobId, { index });\n} catch (err) {\n  if (err.name === 'ArgumentError' && /between 1 and/.test(err.message)) {\n    console.error(`${err.message} — check the job's actual image count first`);\n    process.exitCode = 2;\n  } else throw err;\n}","preventionTips":["Use 1-based indexing (1-4 for a standard 4-image job)","Check job status/image count before choosing an index","Use --index all to avoid range mistakes entirely","Don't reuse indices across jobs with different batch sizes"],"tags":["cli","argument-validation","range-check","midjourney"],"backgroundTag":"index-out-of-range","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}