export interface Flags { server ? : string ; subject : string ; payload : any ; options : { [ key ] : string ; } ; } export function parseFlags ( args , usage , flags ) { let p = new ArgParser ( args , usage , flags ) ; return p . parseFlags ( ) ; } class ArgParser { args : string [ ] ; usage : Function ; flags ? : string [ ] ; constructor ( args , usage , flags ) { this . args = args ; this . usage = usage ; this . flags = flags ; } getOpt ( flag ) { let si = this . args . indexOf ( flag ) ; if ( si !== - 1 ) { let v = this . args [ si + 1 ] ; this . args . splice ( si , 2 ) ; return v ; } return undefined ; } parseFlags ( ) { let opts = { } as Flags ; opts . server = this . getOpt ( '-s' ) ; if ( this . flags ) { opts . options = { } as { [ key ] : string ; } ; this . flags . forEach ( ( f ) => { let v = this . getOpt ( '-' + f ) ; if ( v ) { opts . options [ f ] = v ; } } ) ; } if ( this . args . length < 1 ) { this . usage ( ) ; } opts . subject = this . args [ 0 ] || '' ; opts . payload = this . args [ 1 ] || '' ; return opts ; } }