parseArgs: --help and --version silently accept extra positional arguments

Open Beginner friendly
#88 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
typescript
Domain
cli

Research direction

Start by locating the parseArgs entry point and reproduce the documented --help and --version cases with extra positional arguments. Update the argument validation so these flags do not silently discard extras, while preserving the existing config-path behavior; done means the reproductions throw the expected error and normal help/version parsing still works.

Written by the indexing model from the issue text.

Description

bug S4

Observed behavior

parseArgs short-circuits when either --help or --version is present and returns immediately, before the positional.length > 1 check that was added to reject extra positional arguments (see #33). As a result:

parseArgs(['--help'])                       -> { help: true, ... }   (OK)
parseArgs(['--help', 'foo', 'bar'])         -> { help: true, ... }   (silently ignores foo, bar)
parseArgs(['--version', 'extra1', 'extra2']) -> { version: true, ... } (silently ignores extras)
parseArgs(['config.json', 'extra'])         -> THROW: Unexpected extra arguments: extra  (correct)

The relevant block in parseArgs:

if (booleans.help || booleans.version) {
  return {
    help: booleans.help,
    version: booleans.version,
    verbose: booleans.verbose,
    dryRun: booleans.dryRun,
    maxPrompts,
  };
}

if (positional.length > 1) {
  throw new Error(
    `Unexpected extra arguments: ${positional.slice(1).join(' ')}`,
  );
}

Expected behavior

For consistency with #33's fix, --help and --version should also reject extra positional arguments (or at least reject anything beyond a single positional). Silent acceptance hides typos like loop-the-loop --help confg.json and is inconsistent with the rest of the parser's strictness.

Minimal reproduction

parseArgs(['--help', 'foo', 'bar']);
// Returns { help: true, ... } with no warning that 'foo' and 'bar' were dropped.

Suggested fix

Move the positional.length > 1 check (or a positional.length > 0 check for the help/version branch) above the help/version short-circuit, or duplicate it inside the branch. For example:

if (booleans.help || booleans.version) {
  if (positional.length > 0) {
    throw new Error(
      `Unexpected extra arguments: ${positional.join(' ')}`,
    );
  }
  return { help: booleans.help, version: booleans.version, ... };
}

This mirrors how the parser already treats trailing args after a config path.

Dominant language
TypeScript
Stars
2
Forks
1
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from joewalker/loop-the-loop

All issues in joewalker/loop-the-loop

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.