import * as OptionParser from "." ; function handleArgument ( result , opts , rawArg ) { const arg = rawArg . substr ( 2 ) ; const argSplit = arg . split ( /=/ ) ; const getName = ( ) : string => { if ( arg . includes ( "=" ) ) { return argSplit [ 0 ] ; } else { return arg ; } } ; const getValue = ( ) : string | null => { if ( arg . includes ( "=" ) ) { return argSplit [ 1 ] ; } else { return "" ; } } ; const getOption = ( ) : [ string , OptionParser . IOption ] | null => { const directLookup = opts . arguments [ name ] ; if ( directLookup ) { return [ name , directLookup ] ; } for ( const key of Object . keys ( opts . arguments ) ) { const value = opts . arguments [ key ] ; if ( value . aliases && value . aliases . includes ( name ) ) { return [ key , value ] ; } } return null ; } ; const setResult = ( value ) => { result . arguments [ option ! [ 0 ] ] = value ; } ; const name = getName ( ) ; const option = getOption ( ) ; if ( option === null ) { result . unrecognized . push ( rawArg ) ; return ; } const type = option [ 1 ] . type ; if ( type === "string" ) { setResult ( getValue ( ) ) ; } else if ( type === "number" ) { setResult ( Number ( getValue ( ) ) ) ; } else if ( type === "boolean" ) { setResult ( true ) ; } } export function parse ( argv , opts ) { const result = { unrecognized : [ ] , plain : [ ] , arguments : { } , } ; for ( const key of Object . keys ( opts . arguments ) ) { const val = opts . arguments [ key ] ; result . arguments [ key ] = val . default ; } for ( const str of argv ) { if ( str . startsWith ( "--" ) ) { handleArgument ( result , opts , str ) ; } else { result . plain . push ( str ) ; } } return result ; }