@@ -18,48 +18,72 @@ var Module = require('module');
1818function parseOptions ( argv ) {
1919 var program = require ( 'commander' ) ;
2020
21- // process obsolete options first, we don't want them in the help
22- var args = argv . slice ( 0 , 2 ) ;
23- argv . slice ( 2 ) . forEach ( function ( arg ) {
24- if ( arg === '--cache' ) {
25- // ignore silently - cache is now on by default.
26- } else if ( / ^ ( - l ( m | i | p ) | - - ( l i n e s - ( m a r k | i g n o r e | p r e s e r v e ) | s t a n d a l o n e | f a s t | o l d - s t y l e - f u t u r e | p r o m i s e | c b | a g g r e s s i v e ) ) $ / . test ( arg ) ) {
27- util . warn ( 'obsolete option ignored: ' + arg ) ;
28- return ;
29- } else if ( arg === '--map' ) {
30- util . warn ( 'obsolete option: --map, use -s or --source-maps instead' ) ;
31- args . push ( '--source-maps' ) ;
32- return ;
33- } else if ( arg === '--source-map' ) {
34- util . warn ( 'obsolete option: --source-map, use --source-map-target instead' ) ;
35- args . push ( '--source-map-target' ) ;
36- } else if ( / ^ - - ( f i b e r s | g e n e r a t o r s ) $ / . test ( arg ) ) {
37- util . warn ( 'obsolete option: ' + arg + ', use --runtime ' + arg . substring ( 2 ) + ' instead' ) ;
38- args . push ( '--runtime' ) ;
39- args . push ( arg . substring ( 2 ) ) ;
40- } else if ( / ^ ( - o | - - o u t p u t - d i r ) $ / . test ( arg ) ) {
41- util . warn ( 'obsolete option: ' + arg + ', use -d or --out-dir instead' ) ;
42- args . push ( '-d' ) ;
43- } else if ( arg === '-v' ) {
44- util . warn ( 'obsolete option: -v, verbose is on by default, use -q to turn it off' ) ;
45- } else {
46- args . push ( arg ) ;
47- }
48- } ) ;
49-
50- var options = program . version ( require ( '../package' ) . version , '-v, --version' )
21+ var prog = program . version ( require ( '../package' ) . version , '-v, --version' )
5122 . usage ( '[options] [script] [arguments]' )
5223 . option ( '-c, --compile' , "compile [script] files and save as *.js files" )
5324 . option ( '-d, --out-dir <dir>' , "save compiled .js files in the given directory" )
5425 . option ( '--no-cache' , "turns caching off" , true )
5526 . option ( '--cache-dir <dir>' , "sets the cache directory" )
5627 . option ( '-f, --force' , "force transformation (even if found in cache)" )
5728 . option ( '--runtime <name>' , "target runtime" )
58- . option ( '-s, --source-maps [ true|false|inline|both] ' , "(see babel)" , / ^ ( t r u e | f a l s e | i n l i n e | b o t h ) $ / )
29+ . option ( '-s, --source-maps < true|false|inline|both> ' , "(see babel)" , / ^ ( t r u e | f a l s e | i n l i n e | b o t h ) $ / )
5930 . option ( '--source-map-target <file>' , "output file for source map" )
6031 . option ( '-q, --quiet' , "don't log" )
61- . option ( '--preload <modules>' , "hook to preload a specified list of modules (comma-separated)" )
62- . parse ( argv ) ; // commander skips 2 first args, not just first one.
32+ . option ( '--preload <modules>' , "hook to preload a specified list of modules (comma-separated)" ) ;
33+
34+ // Arguments that follow the script or filename should not be intercepted by commander but passed
35+ // verbatim to the script (https://github.com/Sage/streamlinejs/issues/297)
36+ // There may be a clever way to do this with commander but the following hack should do for now
37+ // I'm handling compat in the same loop to cut correctly if script accepts obsolete streamline options (-o for ex).
38+ // commander skips 2 first args, not just first one.
39+ var args = argv . slice ( 0 , 2 ) ;
40+ var cut = 2 ;
41+ while ( cut < argv . length ) {
42+ var arg = argv [ cut ] ;
43+ if ( arg [ 0 ] !== '-' ) break ;
44+ cut ++ ;
45+ var opt = prog . options . filter ( function ( o ) {
46+ return o . short === arg || o . long === arg ;
47+ } ) [ 0 ] ;
48+ if ( opt ) {
49+ args . push ( arg ) ;
50+ if ( opt . flags . indexOf ( '<' ) >= 0 && cut < argv . length ) {
51+ args . push ( argv [ cut ++ ] ) ;
52+ }
53+ } else {
54+ // handle compat options
55+ if ( arg === '--cache' ) {
56+ // ignore silently - cache is now on by default.
57+ } else if ( / ^ ( - l ( m | i | p ) | - - ( l i n e s - ( m a r k | i g n o r e | p r e s e r v e ) | s t a n d a l o n e | f a s t | o l d - s t y l e - f u t u r e | p r o m i s e | c b | a g g r e s s i v e ) ) $ / . test ( arg ) ) {
58+ util . warn ( 'obsolete option ignored: ' + arg ) ;
59+ return ;
60+ } else if ( arg === '--map' ) {
61+ util . warn ( 'obsolete option: --map, use -s or --source-maps instead' ) ;
62+ args . push ( '--source-maps' ) ;
63+ args . push ( 'true' ) ;
64+ return ;
65+ } else if ( arg === '--source-map' ) {
66+ util . warn ( 'obsolete option: --source-map, use --source-map-target instead' ) ;
67+ args . push ( '--source-map-target' ) ;
68+ } else if ( / ^ - - ( f i b e r s | g e n e r a t o r s ) $ / . test ( arg ) ) {
69+ util . warn ( 'obsolete option: ' + arg + ', use --runtime ' + arg . substring ( 2 ) + ' instead' ) ;
70+ args . push ( '--runtime' ) ;
71+ args . push ( arg . substring ( 2 ) ) ;
72+ } else if ( / ^ ( - o | - - o u t p u t - d i r ) $ / . test ( arg ) ) {
73+ util . warn ( 'obsolete option: ' + arg + ', use -d or --out-dir instead' ) ;
74+ args . push ( '-d' ) ;
75+ if ( cut < argv . length ) args . push ( argv [ cut ++ ] ) ;
76+ } else if ( arg === '-v' ) {
77+ util . warn ( 'obsolete option: -v, verbose is on by default, use -q to turn it off' ) ;
78+ } else {
79+ // push invalid option - commander will deal with it
80+ args . push ( arg ) ;
81+ }
82+ }
83+ }
84+
85+ var options = prog . parse ( args ) ;
86+ options . args = options . args . concat ( argv . slice ( cut ) ) ;
6387
6488 options = Object . keys ( options ) . filter ( function ( opt ) {
6589 return ! / ^ ( [ A - Z ] | _ ) / . test ( opt ) ;
0 commit comments