Skip to content

cli: parseArgs() treats an empty flag value passed without "=" as a positional argument #7351

Open

Description

@kawmra

Describe the bug

When an empty string is given as the value of a flag as a separate argument (--foo ""), parseArgs() sets the flag to "" but also pushes the same "" into _.

Steps to Reproduce

import { parseArgs } from "jsr:@std/cli@1.0.32";

console.log(parseArgs(["--foo", ""], { string: ["foo"] }));
// Actual:   { _: [ "" ], foo: "" }
// Expected: { _: [], foo: "" }

Expected behavior

{ _: [], foo: "" } is returned. The empty string becomes the value of foo and does not appear in _.

Actual behavior

{ _: [ "" ], foo: "" } is returned. foo is "" as expected, but the same "" is also added to _.

Environment

  • OS: macOS 26.4.1
  • deno version: deno 2.9.7
  • std version: @std/cli 1.0.32

Activity

  1. Yusuf-Hussien commented on Oct 8, 2026

    @Yusuf-Hussien

    I'd like to take this. Root cause is in cli/parse_args.ts in the long-flag branch of the �rgs loop:

    s const next = args[i + 1]; if (next) { // <-- truthiness check if (!booleanSet.has(key) && !allBools && !next.startsWith("-") && ...) { value = next; i++; setArgument(key, value, arg, true); continue; } ... } value = stringSet.has(key) ? "" : true; // fallback: never consumes args[i+1] setArgument(key, value, arg, true);

    For --foo "",
    ext is "", so if (next) is false and the value arg is never consumed. The fallback then still sets oo to "" (because oo is in stringSet), but i is not incremented, so the stray "" is visited on the next iteration and lands in _ as a positional. The short-flag branch has the same shape at if (nextArg) (line 841).

    Fix: use an explicit
    ext !== undefined check (and likewise
    extArg !== undefined) so an empty string is accepted as an option value; booleans are unaffected because the branch already excludes �ooleanSet/�llBools. I'll add a regression test in cli/parse_args_test.ts covering parseArgs(["--foo", ""], { string: ["foo"] }) === { _: [], foo: "" } plus a short-flag case, and run the std/cli suite. I'll open a PR once a maintainer confirms the direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions