jackwener/OpenCLI · error · ArgumentError

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

Error message

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

What it means

ArgumentError from `stackoverflow tag` when the `sort` flag is not one of the accepted question-sort keys for the /questions endpoint (activity, votes, creation, hot, week, month per the command's SORT_OPTIONS). The value is lowercased before the membership check, so only unknown keys fail. This validation happens before any network request.

Source

Thrown at clis/stackoverflow/tag.js:33

cli({
    site: 'stackoverflow',
    name: 'tag',
    access: 'read',
    description: 'List Stack Overflow questions tagged with a given tag (most active first).',
    domain: 'stackoverflow.com',
    strategy: Strategy.PUBLIC,
    browser: false,
    args: [
        { name: 'tag', positional: true, required: true, type: 'string', help: 'Tag slug (e.g. python, rust, typescript).' },
        { name: 'sort', type: 'string', default: 'activity', help: `Sort key: ${SORT_OPTIONS.join(', ')}` },
        { name: 'limit', type: 'int', default: 20, help: 'Max questions to return (max 100).' },
    ],
    columns: ['rank', 'id', 'title', 'score', 'answers', 'views', 'isAnswered', 'tags', 'author', 'createdAt', 'lastActivityAt', 'url'],
    func: async (args) => {
        const tag = requireString(args.tag, 'tag').toLowerCase();
        const sort = String(args.sort ?? 'activity').toLowerCase();
        if (!SORT_OPTIONS.includes(sort)) {
            throw new ArgumentError(`sort must be one of ${SORT_OPTIONS.join(', ')}`);
        }
        const limit = normalizeLimit(args.limit, 20, 100, 'limit');
        const data = await seFetch(`/questions`, {
            searchParams: {
                tagged: tag,
                order: 'desc',
                sort,
                pagesize: limit,
            },
        });
        const items = ensureItems(data, `stackoverflow tag "${tag}"`);
        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,

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Use an accepted sort key (see `stackoverflow tag --help`; e.g. `--sort votes`).
  2. Omit --sort entirely to use the default 'activity'.
  3. Check the SORT_OPTIONS list at the top of clis/stackoverflow/tag.js if help text is unclear.

Example fix

// before
stackoverflow tag javascript --sort newest
// after
stackoverflow tag javascript --sort creation
Defensive patterns

Strategy: validation

Validate before calling

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

Try / catch

try {
  await byTag(tag, { sort });
} catch (e) {
  if (e.name === 'ArgumentError' && e.message.startsWith('sort must be one of')) {
    return byTag(tag, { sort: 'activity' }); // fall back to default
  }
  throw e;
}

Prevention

When it happens

Trigger: `stackoverflow tag <tag> --sort relevance`, `--sort newest`, `--sort answered`, or any key not in the command's SORT_OPTIONS array.

Common situations: Using web-UI sort names ('Newest', 'Active') in flag form; carrying over sort values from other CLI commands with different option sets; typos like 'vote' or 'activty'.

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