@@ -3,19 +3,21 @@ import { TargetConfiguration, WorkspaceConfiguration } from './workspace';
33import * as inquirer from 'inquirer' ;
44import { logger } from './logger' ;
55
6+ type PropertyDescription = {
7+ type ?: string ;
8+ properties ?: any ;
9+ oneOf ?: any ;
10+ items ?: any ;
11+ alias ?: string ;
12+ description ?: string ;
13+ default ?: string | number | boolean | string [ ] ;
14+ $ref ?: string ;
15+ $default ?: { $source : 'argv' ; index : number } ;
16+ 'x-prompt' ?: string | { message : string ; type : string ; items : any [ ] } ;
17+ } ;
18+
619type Properties = {
7- [ p : string ] : {
8- type ?: string ;
9- properties ?: any ;
10- oneOf ?: any ;
11- items ?: any ;
12- alias ?: string ;
13- description ?: string ;
14- default ?: string | number | boolean | string [ ] ;
15- $ref ?: string ;
16- $default ?: { $source : 'argv' ; index : number } ;
17- 'x-prompt' ?: string | { message : string ; type : string ; items : any [ ] } ;
18- } ;
20+ [ p : string ] : PropertyDescription ;
1921} ;
2022export type Schema = {
2123 properties : Properties ;
@@ -76,21 +78,30 @@ export function convertToCamelCase(parsed: ParsedArgs): Options {
7678 */
7779export function coerceTypesInOptions ( opts : Options , schema : Schema ) : Options {
7880 Object . keys ( opts ) . forEach ( ( k ) => {
79- opts [ k ] = coerceType (
80- schema . properties [ k ] ? schema . properties [ k ] . type : 'unknown' ,
81- opts [ k ]
82- ) ;
81+ opts [ k ] = coerceType ( schema . properties [ k ] , opts [ k ] ) ;
8382 } ) ;
8483 return opts ;
8584}
8685
87- function coerceType ( type : string , value : any ) {
88- if ( type == 'boolean' ) {
86+ function coerceType ( prop : PropertyDescription | undefined , value : any ) {
87+ if ( ! prop ) return value ;
88+ if ( prop . oneOf ) {
89+ for ( let i = 0 ; i < prop . oneOf . length ; ++ i ) {
90+ const coerced = coerceType ( prop . oneOf [ i ] , value ) ;
91+ if ( coerced !== value ) {
92+ return coerced ;
93+ }
94+ }
95+ return value ;
96+ } else if ( prop . type == 'boolean' ) {
8997 return value === true || value == 'true' ;
90- } else if ( type == 'number' ) {
98+ } else if ( prop . type == 'number' ) {
9199 return Number ( value ) ;
92- } else if ( type == 'array' ) {
93- return value . toString ( ) . split ( ',' ) ;
100+ } else if ( prop . type == 'array' ) {
101+ return value
102+ . toString ( )
103+ . split ( ',' )
104+ . map ( ( v ) => coerceType ( prop . items , v ) ) ;
94105 } else {
95106 return value ;
96107 }
@@ -297,7 +308,7 @@ export function convertPositionParamsIntoNamedParams(
297308 v . $default !== undefined &&
298309 argv [ v . $default . index ]
299310 ) {
300- opts [ k ] = coerceType ( v . type , argv [ v . $default . index ] ) ;
311+ opts [ k ] = coerceType ( v , argv [ v . $default . index ] ) ;
301312 }
302313 } ) ;
303314 delete opts [ '_' ] ;
0 commit comments