import { Command , CommandFlags } from '../types' ; import { splitArguments , parseArguments } from './parseArguments' ; import { deepMerge } from '../utils' ; export function filterOptions ( options , flags , argv ) { return function ( command ) { const commandArgumentKeys = filterFlags ( command . flags , flags , argv ) ; return filterObject ( commandArgumentKeys , options ) ; } ; } function filterFlags ( commandFlags , parsedFlags , argv ) { const mergedFlags = deepMerge ( commandFlags , parsedFlags ) ; const [ , options ] = splitArguments ( parseArguments ( argv , mergedFlags ) ) ; return Object . keys ( options ) . filter ( key => { const value = options [ key ] ; if ( key . startsWith ( '-' ) ) return false ; const typeOf = typeof value as 'string' | 'boolean' ; const flags = typeOf === 'string' ? mergedFlags . string : mergedFlags . boolean ; if ( typeof flags === 'string' ) { return true ; } if ( flags && flags . indexOf ( key ) > - 1 || flags && flags . indexOf ( '--' + key ) > - 1 ) { if ( typeOf === 'string' ) return Boolean ( value ) ; return true ; } return false ; } ) ; } function filterObject ( keys , object ) { return Object . keys ( object ) . reduce ( function ( acc , key ) { if ( keys . indexOf ( key ) > - 1 ) { acc [ key ] = object [ key ] ; } return acc ; } , { } ) ; }