Skip to content

Boolean flags

An option becomes a boolean flag when its coerce is coerce.boolean. It takes no value on the command line, and it accepts the --no- prefix.

defineOptions({
verbose: {
aliases: ["v"],
schema: z.boolean().optional(),
coerce: coerce.boolean,
},
});

Negation

A flag is true when it appears. The --no- prefix inverts whatever the final value would have been.

Terminal window
--verbose true
--no-verbose false

You can also assign a value. The assignment happens first, and then the --no- prefix flips the result.

Terminal window
--verbose=true true
--verbose=false false
--no-verbose=true false # assigned true, then inverted
--no-verbose=false true # assigned false, then inverted

Short flags

Short flags negate the same way, but they cannot take an assigned value:

Terminal window
-v true
--no-v false
-v=true error
-v=false error
--no-v=true false
--no-v=false true

Short boolean flags can be grouped: -rf is the same as -r -f.

Limits

--no- only works on options coerced with coerce.boolean. Using it on anything else fails with ParseErrorCode.InvalidNegationForNonBooleanOption.

Do not name an option in its negated form (noVerbose). The definition check rejects it because it would collide with the negation of verbose.

Note

A boolean flag that never appears is missing, not false. Use z.boolean().default(false) if you want a value either way.