Skip to content

Add acceptsValue parameter to flag()#643

Open
gildor wants to merge 1 commit into
ajalt:masterfrom
gildor:flag-accepts-value
Open

Add acceptsValue parameter to flag()#643
gildor wants to merge 1 commit into
ajalt:masterfrom
gildor:flag-accepts-value

Conversation

@gildor

@gildor gildor commented Jun 10, 2026

Copy link
Copy Markdown

Lets a boolean flag also accept an explicit attached value, parsed by the existing lenient boolean() converter — handy when the value comes from a script or environment variable that already holds a boolean string:

val cache by option("--cache").flag("--no-cache", default = true, acceptsValue = true)
// --cache, --no-cache, AND --cache=true|false
  • Off by default, so existing flags are unchanged.
  • Widens the flag's arity to 0..1. The value must be attached with = (acceptsUnattachedValue = false), so --cache value stays unambiguous with a positional argument.
  • An empty value is an error (boolean() already rejects it) — useful when the value comes from an envvar that may be misconfigured.
  • Giving a value to a secondary/negation name (--no-cache=true) is an error pointing at the positive form, via a new Localization.valueOnFlagNegation.

Companion to #642 (negatable). The two are orthogonal and compose — with both, option("--cache").flag(negatable = true, acceptsValue = true) accepts --cache, --no-cache, and --cache=true|false. They touch the same flag/nullableFlag functions, so whichever lands second needs a small rebase (happy to do it).

Includes the regenerated ABI dump, changelog, docs, and tests (FlagValueTest).

acceptsValue lets a boolean flag also accept an attached --flag=true|false
value, parsed leniently like boolean() (an empty value errors). The value
must be attached with =, so --flag value stays a positional. A value on a
secondary (negation) name is an error pointing at the positive form, via
the new Localization.valueOnFlagNegation.
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.

1 participant