mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-19 03:44:23 -05:00
removing the vendored optparse in favor of a pure-coffeescript optparse library
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
posix: require 'posix'
|
||||
path: require 'path'
|
||||
coffee: require 'coffee-script'
|
||||
optparse: require('./../../vendor/optparse-js/src/optparse')
|
||||
optparse: require('optparse')
|
||||
|
||||
BANNER: '''
|
||||
coffee compiles CoffeeScript source files into JavaScript.
|
||||
@@ -41,7 +41,7 @@ exports.run: ->
|
||||
|
||||
# The "--help" usage message.
|
||||
usage: ->
|
||||
puts '\n' + option_parser.toString() + '\n'
|
||||
puts '\n' + option_parser.help() + '\n'
|
||||
process.exit 0
|
||||
|
||||
# The "--version" message.
|
||||
@@ -107,11 +107,10 @@ parse_options: ->
|
||||
opts: options: {}
|
||||
oparser: option_parser: new optparse.OptionParser SWITCHES
|
||||
oparser.banner: BANNER
|
||||
oparser.add: oparser['on']
|
||||
|
||||
oparser.add 'interactive', -> opts.interactive: true
|
||||
oparser.add 'run', -> opts.run: true
|
||||
oparser.add 'output', (n, dir) -> opts.output: dir
|
||||
oparser.add 'output', (dir) -> opts.output: dir
|
||||
oparser.add 'watch', -> opts.watch: true
|
||||
oparser.add 'print', -> opts.print: true
|
||||
oparser.add 'lint', -> opts.lint: true
|
||||
|
||||
78
src/optparse.coffee
Normal file
78
src/optparse.coffee
Normal file
@@ -0,0 +1,78 @@
|
||||
# Create an OptionParser with a list of valid options.
|
||||
op: exports.OptionParser: (rules) ->
|
||||
@banner: 'Usage: [Options]'
|
||||
@options_title: 'Available options:'
|
||||
@rules: build_rules(rules)
|
||||
@actions: {}
|
||||
this
|
||||
|
||||
# Add a callback to fire when a particular option is encountered.
|
||||
op::add: (value, callback) ->
|
||||
@actions[value]: callback
|
||||
|
||||
# Parse the argument array, calling defined callbacks, returning the remaining non-option arguments.
|
||||
op::parse: (args) ->
|
||||
results: []
|
||||
args: args.concat []
|
||||
while (arg: args.shift())
|
||||
is_option: false
|
||||
for rule in @rules
|
||||
if rule.letter is arg or rule.flag is arg
|
||||
callback: @actions[rule.name]
|
||||
value: rule.argument and args.shift()
|
||||
callback(value) if callback
|
||||
is_option: true
|
||||
break
|
||||
results.push arg unless is_option
|
||||
results
|
||||
|
||||
# Return the help text for this OptionParser, for --help and such.
|
||||
op::help: ->
|
||||
longest: 0
|
||||
has_shorts: false
|
||||
lines: [@banner, '', @options_title]
|
||||
for rule in @rules
|
||||
has_shorts: true if rule.letter
|
||||
longest: rule.flag.length if rule.flag.length > longest
|
||||
for rule in @rules
|
||||
if has_shorts
|
||||
text: if rule.letter then spaces(2) + rule.letter + ', ' else spaces(6)
|
||||
text += spaces(longest, rule.flag) + spaces(3)
|
||||
text += rule.description
|
||||
lines.push text
|
||||
lines.join('\n')
|
||||
|
||||
# Private:
|
||||
|
||||
# Regex matchers for option flags.
|
||||
LONG_FLAG: /^(--\w+)/
|
||||
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
|
||||
tuple.unshift(null) if tuple.length < 3
|
||||
build_rule(tuple...)
|
||||
|
||||
# Build a rule from a short-letter-flag, long-form-flag, and help text.
|
||||
build_rule: (letter, flag, description) ->
|
||||
match: flag.match(OPTIONAL)
|
||||
{
|
||||
name: flag.match(LONG_FLAG)[1].substr(2)
|
||||
letter: letter
|
||||
flag: flag
|
||||
description: description
|
||||
argument: !!(match and match[1])
|
||||
}
|
||||
|
||||
# Space-pad a string with the specified number of characters.
|
||||
spaces: (num, text) ->
|
||||
builder: []
|
||||
if text
|
||||
return text if text.length >= num
|
||||
num -= text.length
|
||||
builder.push text
|
||||
while num -= 1 then builder.push ' '
|
||||
builder.join ''
|
||||
@@ -533,7 +533,7 @@
|
||||
|
||||
# JavaScript templating a-la ERB, pilfered from John Resig's
|
||||
# "Secrets of the JavaScript Ninja", page 83.
|
||||
# Single-quotea fix from Rick Strahl's version.
|
||||
# Single-quote fix from Rick Strahl's version.
|
||||
_.template: (str, data) ->
|
||||
c: _.templateSettings
|
||||
fn: new Function 'obj',
|
||||
|
||||
Reference in New Issue
Block a user