(
t: z.ZodType,
opts: ZodToYargsOptionOptions = {},
)
| 170 | * Returns null for types that can't be represented as CLI flags. |
| 171 | */ |
| 172 | export function zodToYargsOption( |
| 173 | t: z.ZodType, |
| 174 | opts: ZodToYargsOptionOptions = {}, |
| 175 | ): YargsOptionConfig | null { |
| 176 | const unwrapped = unwrap(t); |
| 177 | const description = getDescription(t); |
| 178 | const demandOption = !isOptional(t) && !opts.hasHydratedDefault; |
| 179 | const typeName = getZodTypeName(unwrapped); |
| 180 | |
| 181 | if (typeName === 'string') { |
| 182 | return { type: 'string', describe: description, demandOption }; |
| 183 | } |
| 184 | |
| 185 | if (typeName === 'number' || typeName === 'int' || typeName === 'bigint') { |
| 186 | return { type: 'number', describe: description, demandOption }; |
| 187 | } |
| 188 | |
| 189 | if (typeName === 'boolean') { |
| 190 | return { type: 'boolean', describe: description, demandOption: false }; |
| 191 | } |
| 192 | |
| 193 | if (typeName === 'enum' || typeName === 'nativeEnum') { |
| 194 | const values = getEnumValues(unwrapped); |
| 195 | if (values) { |
| 196 | return { |
| 197 | type: 'string', |
| 198 | choices: values, |
| 199 | describe: description, |
| 200 | demandOption, |
| 201 | }; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (typeName === 'array') { |
| 206 | const element = getArrayElement(unwrapped); |
| 207 | if (element) { |
| 208 | const elemTypeName = getZodTypeName(unwrap(element)); |
| 209 | if (elemTypeName === 'string') { |
| 210 | return { type: 'array', describe: description, demandOption: false }; |
| 211 | } |
| 212 | if (elemTypeName === 'number') { |
| 213 | return { |
| 214 | type: 'array', |
| 215 | describe: description, |
| 216 | demandOption: false, |
| 217 | coerce: coerceNumberArray, |
| 218 | }; |
| 219 | } |
| 220 | } |
| 221 | // Complex array types - use --json fallback |
| 222 | return null; |
| 223 | } |
| 224 | |
| 225 | if (typeName === 'literal') { |
| 226 | const value = getLiteralValue(unwrapped); |
| 227 | if (typeof value === 'string') { |
| 228 | return { type: 'string', default: value, describe: description, demandOption: false }; |
| 229 | } |
no test coverage detected