jackwener/OpenCLI · error · ArgumentError
zhihu search query must not be empty
Error message
zhihu search query must not be empty
What it means
requireQuery in clis/zhihu/search.js throws ArgumentError when the search query string is empty after trimming. The CLI refuses to issue a Zhihu search with a blank query, since that request is meaningless and would fail server-side. Thrown before any network call.
Source
Thrown at clis/zhihu/search.js:57
return '';
}
const MAX_LIMIT = 1000;
const PAGE_SIZE = 20;
const TYPES = ['all', 'answer', 'article', 'question'];
function parseLimit(value) {
const limit = Number(value ?? 10);
if (!Number.isInteger(limit) || limit <= 0 || limit > MAX_LIMIT) {
throw new ArgumentError(`zhihu search --limit must be a positive integer no greater than ${MAX_LIMIT}`, 'Use a normal-sized limit to avoid slow requests or Zhihu risk controls');
}
return limit;
}
function requireQuery(value) {
const query = String(value || '').trim();
if (!query) {
throw new ArgumentError('zhihu search query must not be empty', 'Example: opencli zhihu search codex');
}
return query;
}
function requireType(value) {
const type = String(value || 'all');
if (!TYPES.includes(type)) {
throw new ArgumentError(`zhihu search --type must be one of: ${TYPES.join(', ')}`, 'Example: opencli zhihu search codex --type answer');
}
return type;
}
function unwrapEvaluateResult(payload) {
if (payload && typeof payload === 'object' && 'data' in payload && 'session' in payload) return payload.data;
return payload;
}
function requireSearchPayload(data, url) {View on GitHub (pinned to 49907e53dc)
Solutions
- Provide a non-empty search term: opencli zhihu search codex
- Quote the query in scripts so empty variables do not vanish as arguments
- Guard the variable in scripts: [ -n "$Q" ] || exit 1 before invoking the CLI
Example fix
// before opencli zhihu search $Q # Q is unset -> empty query // after [ -n "$Q" ] && opencli zhihu search "$Q"
Defensive patterns
Strategy: validation
Validate before calling
const q = String(rawQuery ?? '').trim();
if (!q) throw new Error('Search query must be a non-empty string, e.g. "codex"'); Type guard
function hasQuery(v) {
return typeof v === 'string' && v.trim().length > 0;
} Try / catch
try {
await runSearch({ query: q });
} catch (err) {
if (err instanceof ArgumentError && err.message.includes('query must not be empty')) {
console.error('Usage: opencli zhihu search <query>');
process.exitCode = 2;
return;
}
throw err;
} Prevention
- Quote shell variables: opencli zhihu search "$Q"
- Check for unset variables with : "${Q:?Q must be set}" in scripts
- Trim and validate user input before passing it to the CLI
When it happens
Trigger: Running `opencli zhihu search` with no query argument, or `--query ""` / `--query " "` where the value trims to empty.
Common situations: Scripting the CLI with an unquoted shell variable that is unset/empty (e.g. $Q missing); passing a value containing only whitespace; forgetting the positional argument.
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
- zhihu search --limit must be a positive integer no greater t
- tid must be a numeric thread id
- --from and --to must differ (got ${fromCity})
- --from and --to must differ (got ${fromCode})
- --from and --to must differ (got ${fromName})
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/136b3c6d08b478da.
Report an issue: GitHub.