1return unless require?
2{buildCSOptionParser} = require '../lib/coffeescript/command'
3
4optionParser = buildCSOptionParser()
5
6sameOptions = (opts1, opts2, msg) ->
7  ownKeys = Object.keys(opts1).sort()
8  otherKeys = Object.keys(opts2).sort()
9  arrayEq ownKeys, otherKeys, msg
10  for k in ownKeys
11    arrayEq opts1[k], opts2[k], msg
12  yes
13
14test "combined options are not split after initial file name", ->
15  argv = ['some-file.coffee', '-bc']
16  parsed = optionParser.parse argv
17  expected = arguments: ['some-file.coffee', '-bc']
18  sameOptions parsed, expected
19
20  argv = ['some-file.litcoffee', '-bc']
21  parsed = optionParser.parse argv
22  expected = arguments: ['some-file.litcoffee', '-bc']
23  sameOptions parsed, expected
24
25  argv = ['-c', 'some-file.coffee', '-bc']
26  parsed = optionParser.parse argv
27  expected =
28    compile: yes
29    arguments: ['some-file.coffee', '-bc']
30  sameOptions parsed, expected
31
32  argv = ['-bc', 'some-file.coffee', '-bc']
33  parsed = optionParser.parse argv
34  expected =
35    bare: yes
36    compile: yes
37    arguments: ['some-file.coffee', '-bc']
38  sameOptions parsed, expected
39
40test "combined options are not split after a '--', which is discarded", ->
41  argv = ['--', '-bc']
42  parsed = optionParser.parse argv
43  expected =
44    doubleDashed: yes
45    arguments: ['-bc']
46  sameOptions parsed, expected
47
48  argv = ['-bc', '--', '-bc']
49  parsed = optionParser.parse argv
50  expected =
51    bare: yes
52    compile: yes
53    doubleDashed: yes
54    arguments: ['-bc']
55  sameOptions parsed, expected
56
57test "options are not split after any '--'", ->
58  argv = ['--', '--', '-bc']
59  parsed = optionParser.parse argv
60  expected =
61    doubleDashed: yes
62    arguments: ['--', '-bc']
63  sameOptions parsed, expected
64
65  argv = ['--', 'some-file.coffee', '--', 'arg']
66  parsed = optionParser.parse argv
67  expected =
68    doubleDashed: yes
69    arguments: ['some-file.coffee', '--', 'arg']
70  sameOptions parsed, expected
71
72  argv = ['--', 'arg', 'some-file.coffee', '--', '-bc']
73  parsed = optionParser.parse argv
74  expected =
75    doubleDashed: yes
76    arguments: ['arg', 'some-file.coffee', '--', '-bc']
77  sameOptions parsed, expected
78
79test "any non-option argument stops argument parsing", ->
80  argv = ['arg', '-bc']
81  parsed = optionParser.parse argv
82  expected = arguments: ['arg', '-bc']
83  sameOptions parsed, expected
84
85test "later '--' are not removed", ->
86  argv = ['some-file.coffee', '--', '-bc']
87  parsed = optionParser.parse argv
88  expected = arguments: ['some-file.coffee', '--', '-bc']
89  sameOptions parsed, expected
90
91test "throw on invalid options", ->
92  argv = ['-k']
93  throws -> optionParser.parse argv
94
95  argv = ['-ck']
96  throws (-> optionParser.parse argv), /multi-flag/
97
98  argv = ['-kc']
99  throws (-> optionParser.parse argv), /multi-flag/
100
101  argv = ['-oc']
102  throws (-> optionParser.parse argv), /needs an argument/
103
104  argv = ['-o']
105  throws (-> optionParser.parse argv), /value required/
106
107  argv = ['-co']
108  throws (-> optionParser.parse argv), /value required/
109
110  # Check if all flags in a multi-flag are recognized before checking if flags
111  # before the last need arguments.
112  argv = ['-ok']
113  throws (-> optionParser.parse argv), /unrecognized option/
114
115test "has expected help text", ->
116  ok optionParser.help() is '''
117
118Usage: coffee [options] path/to/script.coffee [args]
119
120If called without options, `coffee` will run your script.
121
122      --ast          generate an abstract syntax tree of nodes
123  -b, --bare         compile without a top-level function wrapper
124  -c, --compile      compile to JavaScript and save as .js files
125  -e, --eval         pass a string from the command line as input
126  -h, --help         display this help message
127  -i, --interactive  run an interactive CoffeeScript REPL
128  -j, --join         concatenate the source CoffeeScript before compiling
129  -l, --literate     treat stdio as literate style coffeescript
130  -m, --map          generate source map and save as .js.map files
131  -M, --inline-map   generate source map and include it directly in output
132  -n, --nodes        print out the parse tree that the parser produces
133      --nodejs       pass options directly to the "node" binary
134      --no-header    suppress the "Generated by" header
135  -o, --output       set the output path or path/filename for compiled JavaScript
136  -p, --print        print out the compiled JavaScript
137  -r, --require      require the given module before eval or REPL
138  -s, --stdio        listen for and compile scripts over stdio
139  -t, --transpile    pipe generated JavaScript through Babel
140      --tokens       print out the tokens that the lexer/rewriter produce
141  -v, --version      display the version number
142  -w, --watch        watch scripts for changes and rerun commands
143
144  '''
145