import type { Flags , FlagTypeOrSchema , TypeFlag , } from './types' ; import { toCamelCase , createFlagsObject , mapAliases , parseFlag , getDefaultFromTypeWithValue , validateFlags , getFlagType , } from './utils' ; const isAliasPattern = /^-[\da-z]+/i ; const isFlagPattern = /^--[\w-]{2,}/ ; param schemas param argv returns example export function typeFlag < Schemas extends Flags > ( schemas , argv = process . argv . slice ( 2 ) ) { const aliasesMap = mapAliases ( schemas ) ; const parsed = { flags : createFlagsObject ( schemas ) , unknownFlags : { } , _ : [ ] , } ; let expectingValue ; const setKnown = ( flagName , flagSchema , flagValue ) => { const flagType = getFlagType ( flagName as string , flagSchema ) ; flagValue = getDefaultFromTypeWithValue ( flagType , flagValue ) ; if ( flagValue !== undefined && ! Number . isNaN ( flagValue ) ) { if ( Array . isArray ( parsed . flags [ flagName ] ) ) { parsed . flags [ flagName ] . push ( flagType ( flagValue ) ) ; } else { parsed . flags [ flagName ] = flagType ( flagValue ) ; } } else { expectingValue = ( value ) => { if ( Array . isArray ( parsed . flags [ flagName ] ) ) { parsed . flags [ flagName ] . push ( flagType ( getDefaultFromTypeWithValue ( flagType , value || '' ) ) ) ; } else { parsed . flags [ flagName ] = flagType ( getDefaultFromTypeWithValue ( flagType , value || '' ) ) ; } expectingValue = undefined ; } ; } } ; const setUnknown = ( flagName , flagValue ) => { if ( ! ( flagName in parsed . unknownFlags ) ) { parsed . unknownFlags [ flagName ] = [ ] ; } if ( flagValue !== undefined ) { parsed . unknownFlags [ flagName ] . push ( flagValue ) ; } else { expectingValue = ( value = true ) => { parsed . unknownFlags [ flagName ] . push ( value ) ; expectingValue = undefined ; } ; } } ; for ( let i = 0 ; i < argv . length ; i += 1 ) { const argvElement = argv [ i ] ; if ( argvElement === '--' ) { parsed . _ . push ( ... argv . slice ( i + 1 ) ) ; break ; } const isAlias = isAliasPattern . test ( argvElement ) ; const isFlag = isFlagPattern . test ( argvElement ) ; if ( isFlag || isAlias ) { if ( expectingValue ) { expectingValue ( ) ; } const parsedFlag = parseFlag ( argvElement ) ; const { flagValue } = parsedFlag ; let { flagName } = parsedFlag ; if ( isAlias ) { for ( let j = 0 ; j < flagName . length ; j += 1 ) { const alias = flagName [ j ] ; const hasAlias = aliasesMap . get ( alias ) ; const isLast = j === flagName . length - 1 ; if ( hasAlias ) { setKnown ( hasAlias . name , hasAlias . schema , isLast ? flagValue : true ) ; } else { setUnknown ( alias , isLast ? flagValue : true ) ; } } continue ; } let flagSchema = schemas [ flagName ] ; if ( ! flagSchema ) { const camelized = toCamelCase ( flagName ) ; flagSchema = schemas [ camelized ] ; if ( flagSchema ) { flagName = camelized ; } } if ( ! flagSchema ) { setUnknown ( flagName , flagValue ) ; continue ; } setKnown ( flagName , flagSchema , flagValue ) ; } else if ( expectingValue ) { expectingValue ( argvElement ) ; } else { parsed . _ . push ( argvElement ) ; } } if ( expectingValue ) { expectingValue ( ) ; } validateFlags ( schemas , parsed . flags ) ; type Parsed = typeof parsed ; return parsed as { [ Key in keyof Parsed ] : Parsed [ Key ] ; } ; }