{"id":"57c72723510f878e","repo":"tj/commander.js","slug":"not-a-number","errorCode":null,"errorMessage":"Not a number.","messagePattern":"Not a number\\.","errorType":"validation","errorClass":"InvalidArgumentError","httpStatus":null,"severity":"error","filePath":"examples/arguments-custom-processing.js","lineNumber":14,"sourceCode":"#!/usr/bin/env node\n\n// This is used as an example in the README for:\n//    Custom argument processing\n//    You may specify a function to do custom processing of argument values.\n\nimport { Command, InvalidArgumentError } from 'commander';\nconst program = new Command();\n\nfunction myParseInt(value) {\n  // parseInt takes a string and a radix\n  const parsedValue = parseInt(value, 10);\n  if (isNaN(parsedValue)) {\n    throw new InvalidArgumentError('Not a number.');\n  }\n  return parsedValue;\n}\n\n// The previous value passed to the custom processing is used when processing variadic values.\nfunction mySum(value, total) {\n  return total + myParseInt(value);\n}\n\nprogram\n  .command('add')\n  .argument('<first>', 'integer argument', myParseInt)\n  .argument('[second]', 'integer argument', myParseInt, 1000)\n  .action((first, second) => {\n    console.log(`${first} + ${second} = ${first + second}`);\n  });\n\nprogram","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/tj/commander.js/blob/ba6d13ddb4243e5913367734f8c159089ffe7834/examples/arguments-custom-processing.js#L1-L32","documentation":"Thrown by a custom argument parser supplied via Argument.argParser (or the third argument to .argument()) when the user-supplied command-line value fails validation. In the example at examples/arguments-custom-processing.js:14, myParseInt calls parseInt(value, 10) and throws InvalidArgumentError('Not a number.') when the result is NaN. It is an InvalidArgumentError (code 'commander.invalidArgument'), so Commander formats it as a user-facing CLI error rather than a crash, and exits with code 1.","triggerScenarios":"Running `node arguments-custom-processing add foo` (or `sum silly`) where the `<first>` / `[second]` / `<value...>` argument cannot be parsed by myParseInt. Any string that yields NaN under parseInt(_, 10) — e.g. 'abc', '', '1.2.3' — trips the `isNaN` branch at line 13 and re-throws as InvalidArgumentError at line 14.","commonSituations":"User pastes a value with a unit suffix ('10px'), passes a flag-like token that gets captured as the argument, or a wrapper script forwards an env var that happens to be empty/non-numeric. Also occurs when the radix assumption (base 10) silently rejects hex/scientific input like '0x1F' or '1e2'.","solutions":["Pass a valid integer on the command line, e.g. `add 12 56` instead of `add twelve`.","If non-integer input is legitimate, loosen the parser — replace parseInt with Number or parseFloat and widen the isNaN check, or use a regex to strip units before parsing.","Add a friendlier message: `throw new InvalidArgumentError('Expected an integer, got: ' + value);` so the end user knows what to fix.","If empty strings are expected, guard with `if (value === '') return defaultValue;` before parsing."],"exampleFix":"// before\nfunction myParseInt(value) {\n  const parsedValue = parseInt(value, 10);\n  if (isNaN(parsedValue)) {\n    throw new InvalidArgumentError('Not a number.');\n  }\n  return parsedValue;\n}\n\n// after\nfunction myParseInt(value) {\n  const parsedValue = parseInt(value, 10);\n  if (isNaN(parsedValue)) {\n    throw new InvalidArgumentError(`Expected an integer, got: '${value}'`);\n  }\n  return parsedValue;\n}","handlingStrategy":"try-catch","validationCode":"// Pre-validate before relying on the value\nfunction isValidInt(v) {\n  return typeof v === 'string' && /^-?\\d+$/.test(v);\n}\nif (!isValidInt(rawInput)) {\n  console.error(`Expected integer, got: ${rawInput}`);\n  process.exit(1);\n}","typeGuard":"function isIntString(v: unknown): v is string {\n  return typeof v === 'string' && /^-?\\d+$/.test(v);\n}","tryCatchPattern":"// Wrap the parser so a bad value is reported, not fatal\ntry {\n  program.parseAsync();\n} catch (e) {\n  if (e.code === 'commander.invalidArgument') {\n    console.error(e.message);\n    process.exit(1);\n  }\n  throw e;\n}","preventionTips":["Always pass exitOverride + a parseAsync try/catch so InvalidArgumentError surfaces as a clean message.","Document accepted formats in the argument description string so --help is self-explanatory.","Strip whitespace/units in the parser before parseInt to be lenient on common input."],"tags":["commander","argument-parsing","validation","cli"],"analyzedSha":"ba6d13ddb4243e5913367734f8c159089ffe7834","analyzedAt":"2026-08-03T20:26:04.326Z","schemaVersion":2}