@@ -55,6 +55,11 @@ export interface Option extends YargsOptions {
5555 * The supported types of the option.
5656 */
5757 types ?: ReadonlyArray < 'string' | 'number' | 'boolean' | 'array' | 'object' > ;
58+
59+ /**
60+ * Allowed boolean values when `boolean` is one of the supported types.
61+ */
62+ booleanValues ?: ReadonlySet < boolean > ;
5863}
5964
6065/**
@@ -87,39 +92,70 @@ function checkStringMap(keyValuePairOptions: Set<string>, args: Arguments): bool
8792 return true ;
8893}
8994
95+ const ALL_BOOLEAN_VALUES : ReadonlySet < boolean > = new Set ( [ true , false ] ) ;
96+
9097/**
91- * A Yargs coerce function that converts empty or boolean-like values to a boolean
92- * when an option supports both `boolean` and another type (such as `array` or `string`).
93- * @param value The parsed argument value.
94- * @returns A boolean if the value represents a boolean flag, otherwise the original value.
98+ * Coerces a scalar string value to a boolean when permitted by the schema's allowed boolean values.
9599 */
96- function coerceToBoolean ( value : unknown ) : unknown {
100+ function coerceStringToBoolean ( value : unknown , allowTrue : boolean , allowFalse : boolean ) : unknown {
97101 if ( typeof value === 'string' ) {
98- if ( value === '' || value === 'true' ) {
102+ if ( ( value === '' || value === 'true' ) && allowTrue ) {
99103 return true ;
100104 }
101105
102- if ( value === 'false' ) {
106+ if ( value === 'false' && allowFalse ) {
103107 return false ;
104108 }
105- } else if ( Array . isArray ( value ) ) {
106- if ( value . length === 0 ) {
107- return true ;
109+ }
110+
111+ return value ;
112+ }
113+
114+ /**
115+ * Creates a Yargs coerce function that converts empty or boolean-like values to a boolean
116+ * when an option supports both `boolean` and another type (`array` or `string`).
117+ * @param type The primary Yargs option type (`array` or `string`).
118+ * @param allowedBooleans The boolean literal values permitted by the option's JSON schema.
119+ * @returns A Yargs coerce function.
120+ */
121+ function createCoerceToBoolean (
122+ type : 'array' | 'string' ,
123+ allowedBooleans : ReadonlySet < boolean > = ALL_BOOLEAN_VALUES ,
124+ ) : ( value : unknown ) => unknown {
125+ const allowTrue = allowedBooleans . has ( true ) ;
126+ const allowFalse = allowedBooleans . has ( false ) ;
127+
128+ if ( type === 'string' ) {
129+ return ( value : unknown ) =>
130+ Array . isArray ( value )
131+ ? value . map ( ( item ) => coerceStringToBoolean ( item , allowTrue , allowFalse ) )
132+ : coerceStringToBoolean ( value , allowTrue , allowFalse ) ;
133+ }
134+
135+ return ( value : unknown ) => {
136+ if ( ! Array . isArray ( value ) ) {
137+ return coerceStringToBoolean ( value , allowTrue , allowFalse ) ;
108138 }
109139
110- if ( value . length === 1 ) {
111- const firstValue = value [ 0 ] ;
112- if ( typeof firstValue === 'boolean' ) {
113- return firstValue ;
114- }
140+ if ( value . length === 0 ) {
141+ return allowTrue ? true : value ;
142+ }
115143
116- if ( firstValue === 'true' || firstValue === 'false' ) {
117- return firstValue === 'true' ;
144+ let result : unknown [ ] | boolean | undefined ;
145+ for ( const item of value ) {
146+ if ( item === true || ( item === 'true' && allowTrue ) ) {
147+ result = true ;
148+ } else if ( item === false || ( item === 'false' && allowFalse ) ) {
149+ result = false ;
150+ } else if ( Array . isArray ( result ) ) {
151+ result . push ( item ) ;
152+ } else {
153+ result = [ item ] ;
118154 }
119155 }
120- }
121156
122- return value ;
157+ return result ;
158+ } ;
123159}
124160
125161/**
@@ -245,6 +281,58 @@ function isSupportedArraySchema(schema: json.JsonObject): boolean {
245281 return false ;
246282}
247283
284+ /**
285+ * Gets the boolean literal values permitted by a JSON schema node.
286+ * @param schema The JSON schema node to inspect.
287+ * @returns A set of allowed boolean values (`true`, `false`, or both).
288+ */
289+ function getAllowedBooleanValues ( schema : json . JsonObject ) : ReadonlySet < boolean > {
290+ if ( ! json . schema . getTypesOfSchema ( schema ) . has ( 'boolean' ) ) {
291+ return new Set ( ) ;
292+ }
293+
294+ let allowed = new Set < boolean > ( ALL_BOOLEAN_VALUES ) ;
295+
296+ if ( schema . const !== undefined ) {
297+ if ( typeof schema . const === 'boolean' ) {
298+ allowed = new Set ( [ schema . const ] ) ;
299+ } else {
300+ return new Set ( ) ;
301+ }
302+ }
303+
304+ if ( json . isJsonArray ( schema . enum ) ) {
305+ const enumSet = new Set ( schema . enum ) ;
306+ allowed = new Set ( [ ...allowed ] . filter ( ( v ) => enumSet . has ( v ) ) ) ;
307+ }
308+
309+ if ( json . isJsonArray ( schema . allOf ) ) {
310+ for ( const subSchema of schema . allOf ) {
311+ if ( isJsonObject ( subSchema ) ) {
312+ const subAllowed = getAllowedBooleanValues ( subSchema ) ;
313+ allowed = new Set ( [ ...allowed ] . filter ( ( v ) => subAllowed . has ( v ) ) ) ;
314+ }
315+ }
316+ }
317+
318+ for ( const keyword of [ 'oneOf' , 'anyOf' ] as const ) {
319+ const subSchemas = schema [ keyword ] ;
320+ if ( json . isJsonArray ( subSchemas ) ) {
321+ const combined = new Set < boolean > ( ) ;
322+ for ( const subSchema of subSchemas ) {
323+ if ( isJsonObject ( subSchema ) ) {
324+ for ( const value of getAllowedBooleanValues ( subSchema ) ) {
325+ combined . add ( value ) ;
326+ }
327+ }
328+ }
329+ allowed = new Set ( [ ...allowed ] . filter ( ( v ) => combined . has ( v ) ) ) ;
330+ }
331+ }
332+
333+ return allowed ;
334+ }
335+
248336/**
249337 * Gets the supported types for a JSON schema node.
250338 * @param current The JSON schema node to get the supported types for.
@@ -411,7 +499,7 @@ export async function parseJsonSchemaToOptions(
411499 return ;
412500 }
413501
414- const [ type ] = types ;
502+ const type = types . find ( ( t ) => t !== 'boolean' ) ?? types [ 0 ] ;
415503 const $default = current . $default ;
416504 const $defaultIndex =
417505 isJsonObject ( $default ) && $default [ '$source' ] === 'argv' ? $default [ 'index' ] : undefined ;
@@ -441,6 +529,7 @@ export async function parseJsonSchemaToOptions(
441529 deprecated : xDeprecated === true || typeof xDeprecated === 'string' ? xDeprecated : undefined ,
442530 positional,
443531 types,
532+ booleanValues : types . includes ( 'boolean' ) ? getAllowedBooleanValues ( current ) : undefined ,
444533 ...( type === 'object'
445534 ? {
446535 type : 'array' ,
@@ -494,6 +583,7 @@ export function addSchemaOptionsToCommand<T>(
494583 userAnalytics,
495584 type,
496585 types,
586+ booleanValues,
497587 itemValueType,
498588 hidden,
499589 name,
@@ -515,8 +605,12 @@ export function addSchemaOptionsToCommand<T>(
515605 let coerce : YargsOptions [ 'coerce' ] ;
516606 if ( itemValueType ) {
517607 coerce = coerceToStringMap ;
518- } else if ( type !== 'boolean' && types ?. includes ( 'boolean' ) ) {
519- coerce = coerceToBoolean ;
608+ } else if (
609+ ( type === 'array' || type === 'string' ) &&
610+ types ?. includes ( 'boolean' ) &&
611+ booleanValues ?. size !== 0
612+ ) {
613+ coerce = createCoerceToBoolean ( type , booleanValues ) ;
520614 }
521615
522616 const sharedOptions : YargsOptions & PositionalOptions = {
@@ -527,7 +621,7 @@ export function addSchemaOptionsToCommand<T>(
527621 choices,
528622 coerce,
529623 // This should only be done when `--help` is used otherwise default will override options set in angular.json.
530- ...( includeDefaultValues ? { default : defaultVal } : { } ) ,
624+ ...( includeDefaultValues && defaultVal !== undefined ? { default : defaultVal } : { } ) ,
531625 } ;
532626
533627 if ( positional === undefined ) {
0 commit comments