jackwener/OpenCLI · error · ArgumentError

Subreddit name is required.

Error message

Subreddit name is required.

What it means

`parseSubredditName` in the subreddit-info command throws this ArgumentError when the raw input is empty after trimming (String(raw || '').trim() is falsy). The library requires an explicit subreddit name and offers a hint ('Pass a subreddit name like `python` (or `r/python`)'). It is an input-validation error, not a network error.

Source

Thrown at clis/reddit/subreddit-info.js:11

import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { cli, Strategy } from '@jackwener/opencli/registry';

// Reddit subreddit names: 3–21 chars, letters/digits/underscore, must start
// with a letter. Accept an optional `r/` prefix and normalise it off.
const SUBREDDIT_NAME_RE = /^[A-Za-z][A-Za-z0-9_]{2,20}$/;

export function parseSubredditName(raw) {
    let name = String(raw || '').trim();
    if (!name) {
        throw new ArgumentError(
            'Subreddit name is required.',
            'Pass a subreddit name like `python` (or `r/python`).',
        );
    }
    if (name.startsWith('/r/')) name = name.slice(3);
    else if (name.startsWith('r/')) name = name.slice(2);
    if (!SUBREDDIT_NAME_RE.test(name)) {
        throw new ArgumentError(
            'Invalid subreddit name.',
            'Subreddit names are 3–21 characters, start with a letter, and contain only letters, digits, and underscores.',
        );
    }
    return name;
}

cli({
    site: 'reddit',
    name: 'subreddit-info',

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Pass a subreddit name as the positional argument, e.g. `sub python` or `sub r/python`
  2. Check the shell/script variable feeding the command is actually set and non-empty
  3. Trim user input before passing it; reject empty values in your own wrapper first
  4. Catch ArgumentError and print the usage hint to the user

Example fix

// before
await runCli(['reddit', 'sub', subFromEnv]); // SUB unset -> throws
// after
if (!subFromEnv?.trim()) throw new Error('Set SUB env var, e.g. SUB=python');
await runCli(['reddit', 'sub', subFromEnv.trim()]);
Defensive patterns

Strategy: validation

Validate before calling

// Validate the argument before calling the command:
function requireSubreddit(raw) {
  const name = String(raw ?? '').trim();
  if (!name) throw new Error('Subreddit name is required — e.g. `python` or `r/python`');
  return name;
}

Type guard

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

Try / catch

try {
  await runCli(['reddit', 'sub', raw]);
} catch (e) {
  if (e instanceof ArgumentError && /required/i.test(e.message)) {
    console.error('Usage: sub <name>  (e.g. sub python or sub r/python)');
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the `sub` / subreddit-info command with no positional argument, an empty string, whitespace-only input, or undefined/null where a subreddit name was expected.

Common situations: Shell variable that failed to expand (e.g. "$SUB" unset); a script piping an empty value; forgetting the positional argument on the CLI.

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