jackwener/OpenCLI · error · ArgumentError

archive search query must not be empty

Error message

archive search query must not be empty

What it means

This ArgumentError is thrown before any network call by `archive search` when the positional `query` argument is empty after trimming. A blank search would hit archive.org with an empty `q` parameter, so the CLI rejects it up front. It is a client-side input validation error.

Source

Thrown at clis/archive/search.js:48

        const sortRaw = String(args.sort ?? 'downloads').toLowerCase();
        const sort = SORT_ALIAS[sortRaw] ?? sortRaw;
        if (!SORT_OPTIONS.includes(sort)) {
            throw new ArgumentError(`archive search sort must be one of ${SORT_OPTIONS.join(', ')}`);
        }
        if (args.mediatype && !MEDIATYPES.includes(String(args.mediatype))) {
            throw new ArgumentError(`archive search mediatype must be one of ${MEDIATYPES.join(', ')}`);
        }
        const limit = Number(args.limit ?? 20);
        if (!Number.isInteger(limit) || limit <= 0) {
            throw new ArgumentError('archive search limit must be a positive integer');
        }
        if (limit > 100) {
            throw new ArgumentError('archive search limit must be <= 100');
        }

        const query = String(args.query ?? '').trim();
        if (!query) {
            throw new ArgumentError('archive search query must not be empty');
        }

        const fullQuery = args.mediatype
            ? `(${query}) AND mediatype:${args.mediatype}`
            : query;

        const url = new URL('https://archive.org/advancedsearch.php');
        url.searchParams.set('q', fullQuery);
        url.searchParams.set('output', 'json');
        url.searchParams.set('rows', String(limit));
        url.searchParams.set('sort[]', `${sort} desc`);
        for (const fl of ['identifier', 'title', 'creator', 'date', 'mediatype', 'downloads']) {
            url.searchParams.append('fl[]', fl);
        }

        let resp;
        try {
            resp = await fetch(url, {

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Provide a non-empty search term as the positional argument
  2. In scripts, guard: [ -n "${TERM// /}" ] before invoking, or default the term
  3. If you want to browse rather than search, use a broad term like the OR operator in archive.org query syntax, e.g. 'mediatype:texts' as the query

Example fix

// before
opencli archive search "$QUERY"   # QUERY empty
// after
: "${QUERY:?QUERY must be a non-empty search term}"
opencli archive search "$QUERY"
Defensive patterns

Strategy: validation

Validate before calling

const query = String(rawQuery ?? '').trim();
if (!query) {
    throw new Error('search query must not be empty');
}

Type guard

const hasQuery = (q) => typeof q === 'string' && q.trim().length > 0;

Try / catch

try {
    rows = await run(['archive', 'search', query]);
} catch (err) {
    if (err instanceof ArgumentError && err.message.includes('query must not be empty')) {
        console.error('No search term provided; nothing to do.');
        process.exitCode = 2;
    } else { throw err; }
}

Prevention

When it happens

Trigger: Calling `opencli archive search ""`, `opencli archive search " "`, or omitting the positional query entirely so args.query is undefined (coerced to '').

Common situations: Shell variable holding the search term is unset or whitespace-only; quoting mistakes pass an empty string; script pipes lose the term before it reaches the command.

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