Skip to content

Support CLI options with optional values #6365

Closed

Description

@schickling-assistant

Problem

The CLI module in effect-smol has the same limitation as @effect/cli: boolean flags never consume a value, and text/choice options always require one. There's no way to define a flag that works both bare (--watch) and with an explicit value (--watch=until-done).

This is the same feature request as #6182, filed here for effect-smol / v4.

Desired behavior

// --watch → defaults to 'first-failure'
// --watch=until-done → explicit value
// absent → Option.none
const watch = Options.choice('watch', ['first-failure', 'until-done']).pipe(
  Options.withOptionalValue('first-failure'),
  Options.optional,
)

Prior art

  • GNU coreutils: --color[=WHEN]
  • Python argparse: nargs='?' with const

Filed on behalf of @schickling

Activity

  1. added
    enhancement
    on Jul 14, 2026
  2. self-assigned this
    on Jul 14, 2026
  3. IMax153 commented on Jul 15, 2026

    @IMax153
    Member

    @schickling - which optional-value semantics make the most sense to you?

    1. GNU-style: --watch uses the fallback and only --watch=until-done supplies a value (i.e. = is required)
    2. argparse-style: also accept --watch until-done by consuming the next non-flag token

    I would recommend the GNU-style semantics. They match your examples and avoid ambiguously consuming positional arguments or subcommands, such as --watch build. Also, the CLI's lexer already distinguishes bare and inline values, so this would be simpler and more deterministic.

  4. jayhemnani9910 commented on Oct 13, 2026

    @jayhemnani9910
    Contributor

    @IMax153 I have the GNU-style version you suggested working on v4 effect/cli, if it's useful. Since the issue is assigned to you, are you OK with me opening a PR for it?

    The shape:

    const watch = Flag.Literals("watch", ["first-failure", "until-done"]).pipe(
      Flag.withOptionalValue("first-failure"),
      Flag.optional
    )
    // --watch             -> Some("first-failure")
    // --watch=until-done  -> Some("until-done")
    // -w / -w=until-done  -> same, through the alias
    // (absent)            -> None
    // --watch build       -> Some("first-failure"), "build" stays a positional / subcommand
    • The value only comes after =. A bare flag never consumes the next token.
    • The value given to withOptionalValue is the raw string, parsed and validated like a typed --watch=<value>. So it works under Flag.map, Flag.withDefault, choices and so on.
    • Help shows --watch, -w [=choice]. The wizard writes the value inline. Shell completions stop expecting a separate value word after these flags; zsh also completes --watch=<TAB>.

    Branch: https://github.com/jayhemnani9910/effect/tree/feat/flag-optional-value

    It touches Param/Flag, one branch in the parser's consumeFlagValue, and help, wizard and completions. It comes with tests and a changeset.

  5. IMax153 commented on Oct 13, 2026

    @IMax153
    Member

    @jayhemnani9910 feel free to open a PR, thanks!

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

Metadata

Metadata

Assignees

Labels

4.0clienhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions