mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
4e57ca6833
* Add #! support for executable scripts on Linux. Pass arguments to executable script unchanged if using "#!/usr/bin/env coffee". (Previously, "./test.coffee -abck" would be turned into "-a -b -c -k", for example.) Fixes #1752. * refactor option parsing clean up parsing code and in the process fix oustanding bug where coffeescript modified arguments meant for an executable script * address comments * intermediate save * add note saying where OptionParser is used in coffee command * add some more work * fix flatten functions * refactor tests * make argument processing less confusing * add basic test * remove unused file * compilation now hangs * remove unnecessary changes * add tests!!! * add/fix some tests * clarify a test * fix helpers * fix opt parsing * fix infinite loop * make rule building easier to read * add tests for flag overlap * revamp argument parsing again and add more thorough testing * add tests, comment, clean unused method * address review comments * add test for direct invocation of shebang scripts * move shebang parsing test to separate file and check for browser * remove TODO * example backwards compatible warnings * add correct tests for warning 1 * add tests for warnings * commit output js libs and update docs * respond to review comments also add tests for help text * respond to review comments * fix example output * Rewrite argument parsing documentation to be more concise; add it to sidebar and body; add new output * Don’t mention deprecated syntax; clean up variable names
94 lines
2.4 KiB
CoffeeScript
94 lines
2.4 KiB
CoffeeScript
# Option Parser
|
|
# -------------
|
|
|
|
# Ensure that the OptionParser handles arguments correctly.
|
|
return unless require?
|
|
{OptionParser} = require './../lib/coffeescript/optparse'
|
|
|
|
flags = [
|
|
['-r', '--required [DIR]', 'desc required']
|
|
['-o', '--optional', 'desc optional']
|
|
['-l', '--list [FILES*]', 'desc list']
|
|
]
|
|
|
|
banner = '''
|
|
banner text
|
|
'''
|
|
|
|
opt = new OptionParser flags, banner
|
|
|
|
test "basic arguments", ->
|
|
args = ['one', 'two', 'three', '-r', 'dir']
|
|
result = opt.parse args
|
|
arrayEq args, result.arguments
|
|
eq undefined, result.required
|
|
|
|
test "boolean and parameterised options", ->
|
|
result = opt.parse ['--optional', '-r', 'folder', 'one', 'two']
|
|
ok result.optional
|
|
eq 'folder', result.required
|
|
arrayEq ['one', 'two'], result.arguments
|
|
|
|
test "list options", ->
|
|
result = opt.parse ['-l', 'one.txt', '-l', 'two.txt', 'three']
|
|
arrayEq ['one.txt', 'two.txt'], result.list
|
|
arrayEq ['three'], result.arguments
|
|
|
|
test "-- and interesting combinations", ->
|
|
result = opt.parse ['-o','-r','a','-r','b','-o','--','-a','b','--c','d']
|
|
arrayEq ['-a', 'b', '--c', 'd'], result.arguments
|
|
ok result.optional
|
|
eq 'b', result.required
|
|
|
|
args = ['--','-o','a','-r','c','-o','--','-a','arg0','-b','arg1']
|
|
result = opt.parse args
|
|
eq undefined, result.optional
|
|
eq undefined, result.required
|
|
arrayEq args[1..], result.arguments
|
|
|
|
test "throw if multiple flags try to use the same short or long name", ->
|
|
throws -> new OptionParser [
|
|
['-r', '--required [DIR]', 'required']
|
|
['-r', '--long', 'switch']
|
|
]
|
|
|
|
throws -> new OptionParser [
|
|
['-a', '--append [STR]', 'append']
|
|
['-b', '--append', 'append with -b short opt']
|
|
]
|
|
|
|
throws -> new OptionParser [
|
|
['--just-long', 'desc']
|
|
['--just-long', 'another desc']
|
|
]
|
|
|
|
throws -> new OptionParser [
|
|
['-j', '--just-long', 'desc']
|
|
['--just-long', 'another desc']
|
|
]
|
|
|
|
throws -> new OptionParser [
|
|
['--just-long', 'desc']
|
|
['-j', '--just-long', 'another desc']
|
|
]
|
|
|
|
test "outputs expected help text", ->
|
|
expectedBanner = '''
|
|
|
|
banner text
|
|
|
|
-r, --required desc required
|
|
-o, --optional desc optional
|
|
-l, --list desc list
|
|
|
|
'''
|
|
ok opt.help() is expectedBanner
|
|
|
|
expected = [
|
|
''
|
|
' -r, --required desc required'
|
|
' -o, --optional desc optional'
|
|
' -l, --list desc list'
|
|
''
|
|
].join('\n')
|
|
ok new OptionParser(flags).help() is expected
|