{"record":{"id":"6b76103858d256d0","repo":"jackwener/OpenCLI","slug":"semanticscholar-label-must-be-a-positive-intege","errorCode":null,"errorMessage":"semanticscholar ${label} must be a positive integer","messagePattern":"semanticscholar (.+?) must be a positive integer","errorType":"validation","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"clis/semanticscholar/utils.js","lineNumber":38,"sourceCode":"// arXiv ids: modern `YYMM.NNNNN` form or legacy `archive/YYMMNNN`.\nconst ARXIV_MODERN = /^\\d{4}\\.\\d{4,5}(?:v\\d+)?$/;\nconst ARXIV_LEGACY = /^[a-z-]+\\/\\d{7}(?:v\\d+)?$/i;\n// Other Semantic Scholar accepted prefixes that we pass through verbatim.\nconst PREFIXED = /^(ARXIV|MAG|ACL|PMID|PMCID|URL|CorpusId|DBLP):/i;\n\nexport function requireString(value, label) {\n    const s = String(value ?? '').trim();\n    if (!s) {\n        throw new ArgumentError(`semanticscholar ${label} cannot be empty`);\n    }\n    return s;\n}\n\nexport function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {\n    const raw = value ?? defaultValue;\n    const n = typeof raw === 'number' ? raw : Number(raw);\n    if (!Number.isInteger(n) || n <= 0) {\n        throw new ArgumentError(`semanticscholar ${label} must be a positive integer`);\n    }\n    if (n > maxValue) {\n        throw new ArgumentError(`semanticscholar ${label} must be <= ${maxValue}`);\n    }\n    return n;\n}\n\n/**\n * Resolve a user-supplied paper reference to a Semantic Scholar paper id\n * segment. Accepts:\n *\n *   - bare Semantic Scholar paperId (40-char hex)\n *   - DOI (with or without `doi:` / `https://doi.org/` prefix)\n *   - arXiv id (`1706.03762` / `1706.03762v3` / `cs/0501067`)\n *   - typed prefixes Semantic Scholar accepts verbatim (`ARXIV:`, `MAG:`,\n *     `ACL:`, `PMID:`, `PMCID:`, `URL:`, `CorpusId:`, `DBLP:`)\n *   - full `https://www.semanticscholar.org/paper/<paperId>` URL\n */","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/semanticscholar/utils.js#L20-L56","documentation":"requireBoundedInt coerces the `limit` argument (or another labeled int) to a number and throws ArgumentError when the value is not a positive integer — e.g. 0, negative numbers, NaN, or non-numeric strings. This ensures the API request always carries a valid page size.","triggerScenarios":"Passing `--limit 0`, `--limit -5`, `--limit abc`, `--limit 2.5`, or `--limit ''` to a semanticscholar command; also a variable holding a non-numeric string passed as limit.","commonSituations":"Scripts computing limit from arithmetic that yields 0 or NaN; users assuming limit accepts 'all' or floats; locale-formatted numbers ('1,000') that Number() rejects.","solutions":["Pass a whole number >= 1, e.g. `--limit 20`.","Omit the flag entirely to use the default (20 for search).","In scripts, validate before calling: `Number.isInteger(Number(raw)) && Number(raw) > 0`."],"exampleFix":"// before\nopencli semanticscholar search \"transformers\" --limit 0\n// after\nopencli semanticscholar search \"transformers\" --limit 20","handlingStrategy":"validation","validationCode":"function parseLimit(raw, { def = 20, max = 100 } = {}) {\n    if (raw == null || raw === '') return def;\n    const n = Number(raw);\n    if (!Number.isInteger(n) || n <= 0) throw new Error(`limit must be a positive integer, got ${raw}`);\n    return n;\n}","typeGuard":"function isPositiveInt(v) { return typeof v === 'number' && Number.isInteger(v) && v > 0; }","tryCatchPattern":"try {\n    await runSearch({ limit: args.limit });\n} catch (err) {\n    if (err instanceof ArgumentError && /positive integer/.test(err.message)) {\n        console.error(`Bad --limit: ${err.message} (use e.g. --limit 20)`);\n        process.exit(2);\n    }\n    throw err;\n}","preventionTips":["Only pass whole numbers >= 1 for limit; never 'all', floats, or formatted numbers.","Omit the flag to accept the library default instead of hand-rolling values.","Sanitize script-computed limits with Number.isInteger checks before use.","Unit-test argument parsers with 0, -1, NaN, and '' inputs."],"tags":["argument-validation","cli","input"],"backgroundTag":"invalid-argument-value","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}