{"record":{"id":"e58bfa7a2740912f","repo":"langgenius/dify","slug":"usageinvalidflag-e58bfa","errorCode":"UsageInvalidFlag","errorMessage":"invalid selection: ${line.trim()}","messagePattern":"invalid selection: (.+?)","errorType":"error_code","errorClass":"BaseError","httpStatus":null,"severity":"error","filePath":"cli/src/sys/io/select.ts","lineNumber":142,"sourceCode":"\nfunction cancelled(): BaseError {\n  return new BaseError({ code: ErrorCode.UsageMissingArg, message: 'selection cancelled' })\n}\n\nasync function pickNumbered<T>(opts: SelectOptions<T>): Promise<T> {\n  opts.io.err.write(`${opts.header}\\n`)\n  opts.items.forEach((item, idx) => {\n    opts.io.err.write(`  ${idx + 1}) ${opts.render(item)}\\n`)\n  })\n  opts.io.err.write('Enter number: ')\n\n  const rl = readline.createInterface({ input: opts.io.in, output: opts.io.err, terminal: false })\n  try {\n    const line: string = await new Promise((resolve) => rl.once('line', resolve))\n    const n = Number(line.trim())\n    const chosen = Number.isInteger(n) ? opts.items[n - 1] : undefined\n    if (chosen === undefined)\n      throw new BaseError({\n        code: ErrorCode.UsageInvalidFlag,\n        message: `invalid selection: ${line.trim()}`,\n      })\n    return chosen\n  } finally {\n    rl.close()\n  }\n}\n","sourceCodeStart":124,"sourceCodeEnd":151,"githubUrl":"https://github.com/langgenius/dify/blob/ef8544b173fd6cd7a8e71df2cab576e52bebbfbc/cli/src/sys/io/select.ts#L124-L151","documentation":"BaseError(UsageInvalidFlag, exit 2) from pickNumbered (select.ts:142), the non-TTY fallback picker. After printing a numbered menu it reads one line from stdin, trims, calls Number() on it, and indexes items[n-1]. If Number() returns NaN (non-numeric) or the index is out of range, chosen is undefined and the error throws echoing the offending input. Note Number('') === 0 and Number('1.5') is truthy but items[0.5] is undefined — so empty input and decimals also fail.","triggerScenarios":"Non-TTY mode (isErrTTY false) where the user types anything other than a positive integer in range: empty line, 'abc', '0', '99' (beyond list), '1.5', '1,' (trailing comma), or piping a wrong value (`echo name | difyctl ...`).","commonSituations":"Scripting the picker by piping a value but using the wrong column; EOF on stdin (closed pipe) resolves the line promise with undefined → `undefined.trim()` would throw before this — but a blank line yields '' which Number() turns to 0 → items[-1] undefined → this error; copy-paste included extra text; user pressed Enter without choosing.","solutions":["Enter a single integer in the displayed range (1..N).","When scripting, pipe exactly the numeric index: `echo 2 | difyctl ...`, or better, avoid the picker by passing the id directly.","If the input source may be empty, guard it upstream and pass an explicit id instead of relying on the numbered prompt.","For maintainers: reject empty/whitespace and non-integer input earlier with a clearer message; consider re-prompting in interactive non-TTY contexts."],"exampleFix":"# before — non-numeric or out-of-range piped input\necho abc | difyctl use workspace\necho 99 | difyctl use workspace   # only 2 workspaces listed\n\n# after — valid index in range, or skip the picker\necho 2 | difyctl use workspace\ndifyctl use workspace ws_abc123   # preferred: explicit id","handlingStrategy":"validation","validationCode":"// validate piped/typed input for the numbered picker\nfunction parsePickerIndex(line: string, count: number): number {\n  const n = Number(line.trim())\n  if (!Number.isInteger(n) || n < 1 || n > count) {\n    throw new Error(`expected an integer 1..${count}, got ${JSON.stringify(line)}`)\n  }\n  return n\n}","typeGuard":"function isValidPickerIndex(line: string, count: number): boolean {\n  const n = Number(line.trim())\n  return Number.isInteger(n) && n >= 1 && n <= count\n}","tryCatchPattern":null,"preventionTips":["When scripting the non-TTY picker, pipe exactly the integer index in range.","Prefer passing an explicit id to skip the picker entirely.","Guard empty/EOF input upstream — blank lines Number() to 0 and fail."],"tags":["cli","select","picker","non-tty","usage"],"backgroundTag":null,"analyzedSha":"ef8544b173fd6cd7a8e71df2cab576e52bebbfbc","analyzedAt":"2026-08-12T05:15:17.394Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}