2010-02-25 06:15:58 -05:00
|
|
|
# Create an OptionParser with a list of valid options, in the form:
|
|
|
|
# [short-flag (optional), long-flag, description]
|
|
|
|
# And an optional banner for the usage help.
|
2010-02-24 17:57:58 -05:00
|
|
|
op: exports.OptionParser: (rules, banner) ->
|
2010-02-25 06:15:58 -05:00
|
|
|
@banner: banner or 'Usage: [Options]'
|
|
|
|
@rules: build_rules(rules)
|
2010-02-14 15:16:33 -05:00
|
|
|
this
|
|
|
|
|
2010-02-25 06:15:58 -05:00
|
|
|
# Parse the argument array, populating an options object with all of the
|
|
|
|
# specified options, and returning it. options.arguments will be an array
|
|
|
|
# containing the remaning non-option arguments.
|
2010-02-14 15:16:33 -05:00
|
|
|
op::parse: (args) ->
|
2010-02-24 17:57:58 -05:00
|
|
|
options: {arguments: []}
|
2010-02-25 06:15:58 -05:00
|
|
|
args: args.slice 0
|
|
|
|
while arg: args.shift()
|
2010-02-14 15:16:33 -05:00
|
|
|
is_option: false
|
|
|
|
for rule in @rules
|
|
|
|
if rule.letter is arg or rule.flag is arg
|
2010-02-25 06:15:58 -05:00
|
|
|
options[rule.name]: if rule.has_argument then args.shift() else true
|
2010-02-14 15:16:33 -05:00
|
|
|
is_option: true
|
|
|
|
break
|
2010-02-24 17:57:58 -05:00
|
|
|
options.arguments.push arg unless is_option
|
|
|
|
options
|
2010-02-14 15:16:33 -05:00
|
|
|
|
|
|
|
# Return the help text for this OptionParser, for --help and such.
|
|
|
|
op::help: ->
|
2010-02-25 06:15:58 -05:00
|
|
|
lines: [@banner, '', 'Available options:']
|
2010-02-14 15:16:33 -05:00
|
|
|
for rule in @rules
|
2010-02-25 06:15:58 -05:00
|
|
|
spaces: 15 - rule.flag.length
|
|
|
|
spaces: if spaces > 0 then (' ' for i in [0..spaces]).join('') else ''
|
|
|
|
let_part: if rule.letter then rule.letter + ', ' else ' '
|
|
|
|
lines.push ' ' + let_part + rule.flag + spaces + rule.description
|
2010-02-14 15:16:33 -05:00
|
|
|
lines.join('\n')
|
|
|
|
|
|
|
|
# Regex matchers for option flags.
|
2010-02-21 13:48:38 -05:00
|
|
|
LONG_FLAG: /^(--[\w\-]+)/
|
2010-02-14 15:16:33 -05:00
|
|
|
SHORT_FLAG: /^(-\w+)/
|
|
|
|
OPTIONAL: /\[(.+)\]/
|
|
|
|
|
|
|
|
# Build rules from a list of valid switch tuples in the form:
|
|
|
|
# [letter-flag, long-flag, help], or [long-flag, help].
|
|
|
|
build_rules: (rules) ->
|
|
|
|
for tuple in rules
|
2010-02-25 06:15:58 -05:00
|
|
|
tuple.unshift null if tuple.length < 3
|
|
|
|
build_rule tuple...
|
2010-02-14 15:16:33 -05:00
|
|
|
|
|
|
|
# Build a rule from a short-letter-flag, long-form-flag, and help text.
|
|
|
|
build_rule: (letter, flag, description) ->
|
|
|
|
match: flag.match(OPTIONAL)
|
2010-02-14 20:50:45 -05:00
|
|
|
flag: flag.match(LONG_FLAG)[1]
|
2010-02-14 15:16:33 -05:00
|
|
|
{
|
2010-02-25 06:15:58 -05:00
|
|
|
name: flag.substr 2
|
2010-02-14 15:16:33 -05:00
|
|
|
letter: letter
|
|
|
|
flag: flag
|
|
|
|
description: description
|
2010-02-25 06:15:58 -05:00
|
|
|
has_argument: !!(match and match[1])
|
2010-02-14 15:16:33 -05:00
|
|
|
}
|