{"record":{"id":"d8ef959d71c6b1c5","repo":"jackwener/OpenCLI","slug":"invalid-input-d8ef95","errorCode":"INVALID_INPUT","errorMessage":"INVALID_INPUT","messagePattern":"INVALID_INPUT","errorType":"error_code","errorClass":"CliError","httpStatus":null,"severity":"error","filePath":"clis/zhihu/user-arg.js","lineNumber":15,"sourceCode":"import { CliError } from '@jackwener/opencli/errors';\n\nconst SLUG_RE = /^[A-Za-z0-9_-]+$/;\nconst USER_PREFIX_RE = /^user:([A-Za-z0-9_-]+)$/;\nconst PEOPLE_PATH_RE = /^\\/people\\/([A-Za-z0-9_-]+)\\/?$/;\n\n/**\n * Parse a Zhihu user identifier for read commands.\n * Accepts a bare url_token (`wen-jie-16-47`), the `user:<slug>` form, or a\n * full people URL (`https://www.zhihu.com/people/<slug>`). Returns the slug.\n */\nexport function parseZhihuUser(input) {\n  const value = String(input ?? '').trim();\n  if (!value) {\n    throw new CliError('INVALID_INPUT', 'A Zhihu user is required', 'Example: opencli zhihu user wen-jie-16-47');\n  }\n  const prefixMatch = value.match(USER_PREFIX_RE);\n  if (prefixMatch) return prefixMatch[1];\n  if (SLUG_RE.test(value)) return value;\n  try {\n    const url = new URL(value);\n    if (url.protocol === 'https:' && url.hostname === 'www.zhihu.com') {\n      const m = url.pathname.match(PEOPLE_PATH_RE);\n      if (m) return m[1];\n    }\n  } catch {\n    // fall through to the typed error below\n  }\n  throw new CliError(\n    'INVALID_INPUT',\n    `Invalid Zhihu user: ${value}`,\n    'Use a url_token (wen-jie-16-47) or a people URL (https://www.zhihu.com/people/wen-jie-16-47)',\n  );","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/zhihu/user-arg.js#L1-L33","documentation":"parseZhihuUser resolves a user argument (bare url_token, 'user:<slug>', or a people URL) to a slug. If the input trims to an empty string, it throws CliError with code INVALID_INPUT because no Zhihu user can be identified. Empty input cannot match any accepted format, so it fails immediately with an example usage.","triggerScenarios":"Calling a command requiring a user argument with an empty string, only whitespace, an unset shell variable (e.g. $USER empty), or null/undefined coerced via String(input ?? '').","commonSituations":"Unquoted empty shell variables, scripting pipelines where a variable was never assigned, forgetting the positional argument entirely (framework may pass empty string), copy-paste losing the username.","solutions":["Provide a user: bare slug (wen-jie-16-47), user:<slug>, or https://www.zhihu.com/people/<slug>","Check the shell variable you interpolated is actually set and non-empty","Quote arguments and verify with the command's --help for the expected positional argument"],"exampleFix":"// before\nopencli zhihu user answers \"$ZHIHU_USER\"   # ZHIHU_USER unset\n// after\nZHIHU_USER=wen-jie-16-47\nopencli zhihu user answers \"$ZHIHU_USER\"","handlingStrategy":"validation","validationCode":"function requireZhihuUser(input) {\n  const v = String(input ?? '').trim();\n  if (!v) throw new Error('a zhihu user slug or people URL is required');\n  return v;\n}\nconst user = requireZhihuUser(process.argv[3]);","typeGuard":"function isNonEmptyString(v) {\n  return typeof v === 'string' && v.trim().length > 0;\n}","tryCatchPattern":"try {\n  const slug = parseZhihuUser(userArg);\n} catch (err) {\n  if (err.code === 'INVALID_INPUT') {\n    console.error('Provide a user: e.g. opencli zhihu user wen-jie-16-47');\n  } else throw err;\n}","preventionTips":["Check positional arguments for emptiness before invoking commands","Quote and default shell variables: \"${ZHIHU_USER:?unset}\"","Pass the bare url_token or a full people URL, never empty strings","In scripts, validate required args exist and exit early with usage text"],"tags":["zhihu","input-validation","empty-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}