Perceived Problem
User wants an interface like this:
Case 1:
foo
Case 2:
foo -i
Case 3:
foo -i yarn
foo -I npm
foo -i pnpm
Case 1, when nothing given, means, don't do -i
thing.
Case 2, when given as boolean, means, do -i
thing with some default behaviour
case 3, when given, means, do -i
thing with this
Ideas / Proposed Solution(s)
How could this be represented in the Zod types?
z.boolean().or(z.enum(['a','b','c'])).default(false)
Should do it.
The internal type would be:
true | false | 'a' | 'b' | 'c'
The false
case is because 1) its the default and 2) the user could do this explicitly:
foo --no-i
Currently Mold Command does not know how to parse based on an input union. Nor does it know how to generate docs for an input union.
For parsing there are few rules:
-
If the union includes string literals AND a boolean those string literals CANNOT be false
or true
since that would make it impossible to properly parse/cast the environment input.
-
Looking at the above example for a concrete example: When parsing the flags, we assume that this -i --something-else
means the user is intending to pass as the boolean union member. When they pass -i yarn --something-else
we assume the enum.
-
This kind of zod expression would be unsupportable by Molt: z.boolean().or(z.enum(['a','b','c']).default('b')).default(false)
. A default on a member within the union is not parsable.
~The above leads me to believe that we shouldn't support zod unions but instead roll our own, like we did with .mutuallyExclusive
. We could have .union
or .or
or even simpler could just be accepting an array as the second parameter to .parameter
:~
Ignore examples below and see comment below.
Command.parameter('i install', [z.boolean(), z.enum(['a','b','c'])])
Ah, but the problem with this is we lose the ability to express the concept of a default for the parameter. Ok, try again with a builder function then:
Command
.parameter('i install', __ =>
__.value(z.enum(['a','b','c']).description('...'))
.value(z.boolean().description('...'))
.default(true)
)
enhancement