{"record":{"id":"3b22f3fdc29188e1","repo":"jackwener/OpenCLI","slug":"weread-official-label-must-be-a-positive-integ","errorCode":null,"errorMessage":"weread-official: ${label} must be a positive integer","messagePattern":"weread-official: (.+?) must be a positive integer","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/weread-official/utils.js","lineNumber":268,"sourceCode":"\nexport function requireBookId(value, label = 'bookId') {\n    const text = requireText(value, label);\n    if (!/^[A-Za-z0-9_-]+$/.test(text)) {\n        throw new ArgumentError(`weread-official: ${label} contains invalid characters`, 'Pass a bookId from `weread-official search`.');\n    }\n    return text;\n}\n\nexport function requirePositiveInt(value, label, { defaultValue, max } = {}) {\n    if (value === undefined || value === null || value === '') {\n        if (defaultValue === undefined) {\n            throw new ArgumentError(`weread-official: ${label} is required`);\n        }\n        return defaultValue;\n    }\n    const text = String(value).trim();\n    if (!/^\\d+$/.test(text)) {\n        throw new ArgumentError(`weread-official: ${label} must be a positive integer`);\n    }\n    const n = Number(text);\n    if (!Number.isSafeInteger(n) || n < 1) {\n        throw new ArgumentError(`weread-official: ${label} must be a positive integer`);\n    }\n    if (max !== undefined && n > max) {\n        throw new ArgumentError(`weread-official: ${label} must be <= ${max}`);\n    }\n    return n;\n}\n\nexport function requireChoice(value, choices, label, defaultValue) {\n    const text = String(value ?? defaultValue ?? '').trim();\n    if (!choices.includes(text)) {\n        throw new ArgumentError(`weread-official: ${label} must be one of: ${choices.join(', ')}`);\n    }\n    return text;\n}","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/weread-official/utils.js#L250-L286","documentation":"requirePositiveInt throws this when the supplied value is not made solely of digits (/^\\d+$/ fails) — e.g. letters, negative numbers, floats, or units like '10px'. The validator intentionally lumps non-numeric text and integers < 1 into the same message to keep CLI feedback simple.","triggerScenarios":"Passing --count abc, --count -3, --count 2.5, or a value with whitespace/units to count or listNotebooks; the regex test on the trimmed string fails.","commonSituations":"Typoed flags picking up string values ('ten'); scripts interpolating negative sentinel values (-1 meaning 'all'); locale-formatted numbers ('1,000'); stale scripts using old option semantics.","solutions":["Supply a plain positive integer literal, e.g. --count 10 (no signs, decimals, commas, or units).","Sanitize the value in scripts: strip non-digits or coerce with parseInt and re-check before calling.","If you need an 'unlimited' sentinel, use the documented max/default options instead of -1.","Confirm you are passing the value to the right flag (a string flag instead of the count flag)."],"exampleFix":"// before\ncli(['listNotebooks', '--count', '-1']);\n// after\ncli(['listNotebooks', '--count', '10']);","handlingStrategy":"validation","validationCode":"const n = Number(String(value).trim());\nif (!Number.isInteger(n) || n < 1) throw new Error(`count must be a positive integer, got: ${value}`);","typeGuard":"function isPositiveIntString(v) { return typeof v === 'number' ? Number.isSafeInteger(v) && v >= 1 : /^\\d+$/.test(String(v).trim()) && Number(v) >= 1; }","tryCatchPattern":"try {\n  await cli.run(['count', '--count', value]);\n} catch (e) {\n  if (e instanceof ArgumentError && /must be a positive integer/.test(e.message)) {\n    console.error(`Bad --count value '${value}'; use an integer >= 1`); process.exitCode = 2;\n  } else throw e;\n}","preventionTips":["Quote and normalize numeric CLI args in shell scripts; strip commas and units first.","Never use -1 or 0 as 'unlimited' sentinels with this library.","Unit-test script wrappers with edge values (0, -1, 'abc', '1.5')."],"tags":["argument-validation","cli","type-mismatch"],"backgroundTag":"invalid-argument-type","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}