jackwener/OpenCLI · error · CliError

INVALID_INPUT

INVALID_INPUT

Error message

INVALID_INPUT

What it means

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.

Source

Thrown at clis/zhihu/user-arg.js:15

import { CliError } from '@jackwener/opencli/errors';

const SLUG_RE = /^[A-Za-z0-9_-]+$/;
const USER_PREFIX_RE = /^user:([A-Za-z0-9_-]+)$/;
const PEOPLE_PATH_RE = /^\/people\/([A-Za-z0-9_-]+)\/?$/;

/**
 * Parse a Zhihu user identifier for read commands.
 * Accepts a bare url_token (`wen-jie-16-47`), the `user:<slug>` form, or a
 * full people URL (`https://www.zhihu.com/people/<slug>`). Returns the slug.
 */
export function parseZhihuUser(input) {
  const value = String(input ?? '').trim();
  if (!value) {
    throw new CliError('INVALID_INPUT', 'A Zhihu user is required', 'Example: opencli zhihu user wen-jie-16-47');
  }
  const prefixMatch = value.match(USER_PREFIX_RE);
  if (prefixMatch) return prefixMatch[1];
  if (SLUG_RE.test(value)) return value;
  try {
    const url = new URL(value);
    if (url.protocol === 'https:' && url.hostname === 'www.zhihu.com') {
      const m = url.pathname.match(PEOPLE_PATH_RE);
      if (m) return m[1];
    }
  } catch {
    // fall through to the typed error below
  }
  throw new CliError(
    'INVALID_INPUT',
    `Invalid Zhihu user: ${value}`,
    'Use a url_token (wen-jie-16-47) or a people URL (https://www.zhihu.com/people/wen-jie-16-47)',
  );

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Provide a user: bare slug (wen-jie-16-47), user:<slug>, or https://www.zhihu.com/people/<slug>
  2. Check the shell variable you interpolated is actually set and non-empty
  3. Quote arguments and verify with the command's --help for the expected positional argument

Example fix

// before
opencli zhihu user answers "$ZHIHU_USER"   # ZHIHU_USER unset
// after
ZHIHU_USER=wen-jie-16-47
opencli zhihu user answers "$ZHIHU_USER"
Defensive patterns

Strategy: validation

Validate before calling

function requireZhihuUser(input) {
  const v = String(input ?? '').trim();
  if (!v) throw new Error('a zhihu user slug or people URL is required');
  return v;
}
const user = requireZhihuUser(process.argv[3]);

Type guard

function isNonEmptyString(v) {
  return typeof v === 'string' && v.trim().length > 0;
}

Try / catch

try {
  const slug = parseZhihuUser(userArg);
} catch (err) {
  if (err.code === 'INVALID_INPUT') {
    console.error('Provide a user: e.g. opencli zhihu user wen-jie-16-47');
  } else throw err;
}

Prevention

When it happens

Trigger: 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 ?? '').

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/d8ef959d71c6b1c5. Report an issue: GitHub.