File: option_parser.coffee

package info (click to toggle)
coffeescript 2.7.0%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,360 kB
  • sloc: makefile: 20; xml: 9; sh: 6; javascript: 5
file content (94 lines) | stat: -rw-r--r-- 2,429 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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