jackwener/OpenCLI · error · ArgumentError

stackoverflow related sort must be one of ${SORT_OPTIONS.joi

Error message

stackoverflow related sort must be one of ${SORT_OPTIONS.join(', ')}

What it means

ArgumentError from `stackoverflow related` when the `sort` flag is not one of the accepted keys (rank, activity, votes, creation). The value is lowercased first, so case is not the problem — only an unknown sort key triggers this. The check runs before the API call to fail fast on invalid input.

Source

Thrown at clis/stackoverflow/related.js:40

    access: 'read',
    description: 'List Stack Overflow questions related to a given question id.',
    domain: 'stackoverflow.com',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'id', positional: true, required: true, type: 'string', help: 'Stack Overflow question id (numeric, e.g. 79935770).' },
        { name: 'sort', type: 'string', default: 'rank', help: `Sort key: ${SORT_OPTIONS.join(', ')} (rank = SO relevance default).` },
        { name: 'limit', type: 'int', default: 20, help: 'Max related questions (1-100).' },
    ],
    columns: ['rank', 'id', 'title', 'score', 'answers', 'views', 'isAnswered', 'tags', 'author', 'createdAt', 'lastActivityAt', 'url'],
    func: async (args) => {
        const id = String(args.id ?? '').trim();
        if (!/^\d+$/.test(id)) {
            throw new ArgumentError(`stackoverflow related id must be a numeric question id, got ${JSON.stringify(args.id)}`);
        }
        const sort = String(args.sort ?? 'rank').toLowerCase();
        if (!SORT_OPTIONS.includes(sort)) {
            throw new ArgumentError(`stackoverflow related sort must be one of ${SORT_OPTIONS.join(', ')}`);
        }
        const limit = normalizeLimit(args.limit, 20, 100, 'limit');
        const data = await seFetch(`/questions/${encodeURIComponent(id)}/related`, {
            searchParams: {
                order: 'desc',
                sort,
                pagesize: limit,
            },
        });
        const items = ensureItems(data, `stackoverflow related ${id}`);
        return items.slice(0, limit).map((q, i) => ({
            rank: i + 1,
            id: q.question_id,
            title: decodeHtmlEntities(q.title || ''),
            score: q.score ?? 0,
            answers: q.answer_count ?? 0,
            views: q.view_count ?? 0,
            isAnswered: Boolean(q.is_answered),

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use one of: rank, activity, votes, creation (e.g. `stackoverflow related 79935770 --sort votes`).
  2. Check `stackoverflow related --help` for the current sort key list.
  3. If you want relevance ordering, use the default (omit --sort, which defaults to 'rank').

Example fix

// before
stackoverflow related 79935770 --sort relevance
// after
stackoverflow related 79935770 --sort rank
Defensive patterns

Strategy: validation

Validate before calling

const SORTS = ['rank', 'activity', 'votes', 'creation'];
if (!SORTS.includes(String(sort).toLowerCase())) throw new TypeError(`sort must be one of ${SORTS.join(', ')}`);

Try / catch

try {
  await related(id, { sort });
} catch (e) {
  if (e.name === 'ArgumentError' && e.message.includes('sort must be one of')) {
    console.error(`${sort} is invalid; use rank|activity|votes|creation`);
    process.exitCode = 2;
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: `stackoverflow related <id> --sort relevance`, `--sort score`, `--sort newest`, or any other key outside SORT_OPTIONS = ['rank','activity','votes','creation'].

Common situations: Assuming related shares sort keys with the `tag` command or the generic /questions endpoint (which uses 'activity', 'votes', 'creation' but not 'rank'); guessing sort names from the SE web UI ('relevance', 'newest'); typos like 'vote' or 'creat'.

Understand the failure class

Background: Invalid option value errors: "must be one of", "is not a valid", and "only allows" failures explained — this error's family across 23 libraries.

Related errors


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