jackwener/OpenCLI · error · ArgumentError

nowcoder search requires a non-empty query

Error message

nowcoder search requires a non-empty query

What it means

The nowcoder search command validates its arguments before calling the API. If args.query is missing, not a string, or only whitespace, it throws ArgumentError('nowcoder search requires a non-empty query'). This is a fail-fast input validation, not a network error.

Source

Thrown at clis/nowcoder/search.js:26

cli({
    site: 'nowcoder',
    name: 'search',
    access: 'read',
    description: 'Search content and moment posts',
    domain: 'www.nowcoder.com',
    strategy: Strategy.COOKIE,
    browser: true,
    navigateBefore: false,
    args: [
        { name: 'query', positional: true, required: true, help: 'Search keyword' },
        { name: 'type', type: 'str', default: 'post', help: 'Post search scope (post/all)' },
        { name: 'limit', type: 'int', default: 10, help: 'Number of posts (1-50)' },
    ],
    columns: ['rank', 'post_type', 'id', 'uuid', 'entity_id', 'url', 'title', 'author', 'author_id', 'author_url', 'school', 'content', 'likes', 'comments', 'views', 'time'],
    func: async (page, args) => {
        const query = typeof args.query === 'string' ? args.query.trim() : '';
        if (!query) throw new ArgumentError('nowcoder search requires a non-empty query');
        const type = args.type ?? 'post';
        if (type !== 'all' && type !== 'post') {
            throw new ArgumentError('nowcoder search --type must be all or post');
        }
        const limit = requirePositiveInt(args.limit ?? 10, 'limit', 50);
        const data = await fetchNowcoderData(
            page,
            'https://gw-c.nowcoder.com/api/sparta/pc/search',
            { method: 'POST', body: { query, type, page: 1, pageSize: limit }, timeoutMs: 15_000 },
            'Nowcoder search request',
        );
        return projectNowcoderFeed(data.records, limit, 'search', type === 'all');
    },
});

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a non-empty --query value, e.g. nowcoder search --query "golang"
  2. Trim and check the query in the calling script before invoking the CLI
  3. If scripted, abort early when the query variable is empty

Example fix

// before
await run(['nowcoder', 'search', '--query', q]);
// after
if (!q || !q.trim()) throw new Error('search query required');
await run(['nowcoder', 'search', '--query', q.trim()]);
Defensive patterns

Strategy: validation

Validate before calling

const q = typeof query === 'string' ? query.trim() : '';
if (!q) throw new Error('nowcoder search --query must be a non-empty string');

Type guard

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

Try / catch

try { await run(['nowcoder', 'search', '--query', q]); }
catch (e) {
  if (e instanceof ArgumentError && /non-empty query/.test(e.message)) {
    console.error('Usage: nowcoder search --query <text> [--type post|all] [--limit N]');
    process.exitCode = 2;
  } else throw e;
}

Prevention

When it happens

Trigger: Invoking `nowcoder search` without --query, with --query "" or --query " ", or passing a non-string value for query.

Common situations: Shell quoting mistakes dropping the argument; scripting the CLI and forgetting the query field; an upstream pipeline passing an empty variable.

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/dbf5ca12d47585f5. Report an issue: GitHub.