@@ -67,46 +67,57 @@ function getUserHome() {
6767}
6868exports . getUserHome = getUserHome ;
6969
70- // Returns {'alice': true, 'bob': false} when passed a dictionary, e.g. :
70+ // Returns {'alice': true, 'bob': false} when passed a string and dictionary as follows :
7171// parseOptions('-a', {'a':'alice', 'b':'bob'});
72- function parseOptions ( str , map ) {
72+ // Returns {'reference': 'string-value', 'bob': false} when passed two dictionaries of the form:
73+ // parseOptions({'-r': 'string-value'}, {'r':'reference', 'b':'bob'});
74+ function parseOptions ( opt , map ) {
7375 if ( ! map )
7476 error ( 'parseOptions() internal error: no map given' ) ;
7577
7678 // All options are false by default
7779 var options = { } ;
7880 for ( var letter in map ) {
79- if ( ! map [ letter ] . match ( '^!' ) )
81+ if ( map [ letter ] [ 0 ] !== '!' )
8082 options [ map [ letter ] ] = false ;
8183 }
8284
83- if ( ! str )
85+ if ( ! opt )
8486 return options ; // defaults
8587
86- if ( typeof str !== 'string' )
87- error ( 'parseOptions() internal error: wrong str' ) ;
88-
89- // e.g. match[1] = 'Rf' for str = '-Rf'
90- var match = str . match ( / ^ \- ( .+ ) / ) ;
91- if ( ! match )
92- return options ;
93-
94- // e.g. chars = ['R', 'f']
95- var chars = match [ 1 ] . split ( '' ) ;
96-
97- var opt ;
98- chars . forEach ( function ( c ) {
99- if ( c in map ) {
100- opt = map [ c ] ;
101- if ( opt . match ( '^!' ) )
102- options [ opt . slice ( 1 , opt . length - 1 ) ] = false ;
103- else
104- options [ opt ] = true ;
105- } else {
106- error ( 'option not recognized: ' + c ) ;
107- }
108- } ) ;
88+ var optionName ;
89+ if ( typeof opt === 'string' ) {
90+ if ( opt [ 0 ] !== '-' )
91+ return options ;
10992
93+ // e.g. chars = ['R', 'f']
94+ var chars = opt . slice ( 1 ) . split ( '' ) ;
95+
96+ chars . forEach ( function ( c ) {
97+ if ( c in map ) {
98+ optionName = map [ c ] ;
99+ if ( optionName [ 0 ] === '!' )
100+ options [ optionName . slice ( 1 , optionName . length - 1 ) ] = false ;
101+ else
102+ options [ optionName ] = true ;
103+ } else {
104+ error ( 'option not recognized: ' + c ) ;
105+ }
106+ } ) ;
107+ } else if ( typeof opt === 'object' ) {
108+ for ( var key in opt ) {
109+ // key is a string of the form '-r', '-d', etc.
110+ var c = key [ 1 ] ;
111+ if ( c in map ) {
112+ optionName = map [ c ] ;
113+ options [ optionName ] = opt [ key ] ; // assign the given value
114+ } else {
115+ error ( 'option not recognized: ' + c ) ;
116+ }
117+ }
118+ } else {
119+ error ( 'options must be strings or key-value pairs' ) ;
120+ }
110121 return options ;
111122}
112123exports . parseOptions = parseOptions ;
@@ -213,8 +224,11 @@ function wrap(cmd, fn, options) {
213224 if ( options && options . notUnix ) {
214225 retValue = fn . apply ( this , args ) ;
215226 } else {
216- if ( args . length === 0 || typeof args [ 0 ] !== 'string' || args [ 0 ] . length <= 1 || args [ 0 ] [ 0 ] !== '-' )
227+ if ( typeof args [ 0 ] === 'object' && args [ 0 ] . constructor . name === 'Object' ) {
228+ args = args ; // object count as options
229+ } else if ( args . length === 0 || typeof args [ 0 ] !== 'string' || args [ 0 ] . length <= 1 || args [ 0 ] [ 0 ] !== '-' ) {
217230 args . unshift ( '' ) ; // only add dummy option if '-option' not already present
231+ }
218232 // Expand the '~' if appropriate
219233 var homeDir = getUserHome ( ) ;
220234 args = args . map ( function ( arg ) {
0 commit comments