Skip to content

fix(js): throw error when unsupported language is passed to CLI via -l / --lang - #1491

Merged
tushuhei merged 2 commits into
google:mainfrom
codeXsidd:feat/issue-1490-cli-invalid-lang-error
Nov 4, 2026
Merged

tushuhei merged 2 commits into
google:mainfrom
codeXsidd:feat/issue-1490-cli-invalid-lang-error

Conversation

@codeXsidd

Copy link
Copy Markdown
Contributor

Summary

When an unsupported language code is passed to the JavaScript CLI using -l or --lang, throw an explicit error with the list of available languages instead of silently falling back to the Japanese parser.

Problem

In javascript/src/cli.ts, parser resolution was implemented as:

  const parser = model
    ? loadCustomParser(model)
    : lang && defaultParsers.has(lang)
      ? defaultParsers.get(lang)!
      : loadDefaultJapaneseParser();

If a caller supplied an unknown language (such as budoux -l fr "Bonjour"), the condition lang && defaultParsers.has(lang) evaluated to false, causing the CLI to silently default to loadDefaultJapaneseParser(). This resulted in unexpected Japanese-model segmentation instead of informing the user that the language was not recognized. In contrast, the Python CLI validates language options and produces a clear error.

Solution

  • Added a check in javascript/src/cli.ts: if lang is provided (and model is not overriding), verify defaultParsers.has(lang). If not found, throw an Error stating that the language is not supported and listing available supported languages.
  • Added unit tests for both -l and --lang with unsupported language inputs in javascript/src/tests/cli_test.ts.

Testing

  • Unit tests: npx vitest run passed across all 6 test suites (166 tests passed).
  • CLI version check: node ./scripts/check-cli-version.js passed.
  • Linting / formatting: npx biome check src/cli.ts src/tests/cli_test.ts passed with 0 errors.

Related Issue

Closes #1490

@tushuhei tushuhei left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your PR!

  1. Rather than adding an empty if (model) {} branch and falling back to loadDefaultJapaneseParser() (which also lets -l "" silently fall back to Japanese and instantiates the ja parser twice), please set 'ja' as the default value on .option('-l, --lang <str>', ..., 'ja') and resolve parser in a single lookup:
  .option(
    '-l, --lang <str>',
    `language model to use. -m and --model will be prioritized if any.\navailable languages: ${[
      ...defaultParsers.keys(),
    ].join(', ')}`,
    'ja'
  )
  const {lang, model, delim, sep, html} = options as {
    html: boolean;
    delim: string;
    sep: string;
    model?: string;
    lang: string;
  };
  const {args} = program;

  const parser =
    model !== undefined ? loadCustomParser(model) : defaultParsers.get(lang);
  if (!parser) {
    throw new Error(
      `'${lang}' is not supported. Available languages: ${[
        ...defaultParsers.keys(),
      ].join(', ')}`
    );
  }

(You can also remove the unused loadDefaultJapaneseParser import.)

  1. In javascript/src/tests/cli_test.ts, please avoid carrying over the stab typo (e.g., inline expect(() => cli(argv)).toThrowError(...)) and assert the full expected error message including [...loadDefaultParsers().keys()].join(', ').

…mpty model branch, assert full error message in tests
@codeXsidd

Copy link
Copy Markdown
Contributor Author

Thank you for the review @tushuhei! I've addressed both points:

  1. Set 'ja' as the default value directly on .option('-l, --lang ', ..., 'ja') and resolved the parser in a single lookup: const parser = model !== undefined ? loadCustomParser(model) : defaultParsers.get(lang). Removed the now-unused loadDefaultJapaneseParser import.
  2. Removed the stab typo variable and updated both error-throwing tests to use �xpect(() => cli(argv)).toThrowError(...) with the full expected error message including [...loadDefaultParsers().keys()].join(', ').

All 166 tests pass and biome check reports 0 errors.

@codeXsidd
codeXsidd requested a review from tushuhei November 3, 2026 14:59
@tushuhei
tushuhei merged commit 197046c into google:main Nov 4, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(js): throw error when unsupported language is passed to CLI via -l / --lang

2 participants