Skip to content

Commit

Permalink
feat(flags): add parseFlagValue() method
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Mar 22, 2020
1 parent 6900462 commit 1983bd1
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions packages/flags/lib/flags.ts
Expand Up @@ -232,16 +232,7 @@ export function parseFlags( args: string[], opts: IParseOptions = {} ): IFlagsRe
throw new Error( 'Wrongly used parseValue.' );
}

const parse: ITypeHandler<any> =
( typeof option.type === 'function' && option.type )
|| ( opts.types && arg.type && opts.types[ arg.type ] )
|| Types[ arg.type || OptionType.STRING ];

if ( !parse ) {
throw new Error( `Unknown parser ${ arg.type }` );
}

let result = parse( option, arg, nextValue );
let result = parseFlagValue( option, arg, nextValue, opts );

if ( typeof result !== 'undefined' ) {
increase = true;
Expand All @@ -263,6 +254,23 @@ export function parseFlags( args: string[], opts: IParseOptions = {} ): IFlagsRe
return { flags, unknown, literal };
}

export function parseFlagValue( option: IFlagOptions, arg: IFlagArgument, nextValue: string | false, opts: IParseOptions ): any {

const parse: ITypeHandler<any> =
// Type registered with option
( typeof option.type === 'function' && option.type )
// Type registered with parse() options
|| ( opts.types && arg.type && opts.types[ arg.type ] )
// Default type
|| Types[ arg.type || OptionType.STRING ];

if ( !parse ) {
throw new Error( `Unknown parser ${ arg.type }` );
}

return parse( option, arg, nextValue );
}

/**
* Find option by name.
*
Expand Down

0 comments on commit 1983bd1

Please sign in to comment.