optparse.coffee | |
---|---|
A simple OptionParser class to parse option flags from the command-line. Use it like so: | exports.OptionParser: class OptionParser |
Initialize with a list of valid options, in the form:
Along with an an optional banner for the usage help. | constructor: (rules, banner) ->
@banner: banner
@rules: buildRules(rules) |
Parse the list of arguments, populating an | parse: (args) ->
options: {arguments: []}
args: normalizeArguments args
while (arg: args.shift())
isOption: !!(arg.match(LONG_FLAG) or arg.match(SHORT_FLAG))
matchedRule: no
for rule in @rules
if rule.shortFlag is arg or rule.longFlag is arg
options[rule.name]: if rule.hasArgument then args.shift() else true
matchedRule: yes
break
throw new Error "unrecognized option: $arg" if isOption and not matchedRule
options.arguments.push arg unless isOption
options |
Return the help text for this OptionParser, listing and describing all
of the valid options, for | help: ->
lines: ['Available options:']
lines.unshift "$@banner\n" if @banner
for rule in @rules
spaces: 15 - rule.longFlag.length
spaces: if spaces > 0 then (' ' for i in [0..spaces]).join('') else ''
letPart: if rule.shortFlag then rule.shortFlag + ', ' else ' '
lines.push " $letPart$rule.longFlag$spaces$rule.description"
"\n${ lines.join('\n') }\n" |
Helpers | |
Regex matchers for option flags. | LONG_FLAG: /^(--\w[\w\-]+)/
SHORT_FLAG: /^(-\w)/
MULTI_FLAG: /^-(\w{2,})/
OPTIONAL: /\[(.+)\]/ |
Build and return the list of option rules. If the optional short-flag is
unspecified, leave it out by padding with | buildRules: (rules) ->
for tuple in rules
tuple.unshift null if tuple.length < 3
buildRule tuple... |
Build a rule from a | buildRule: (shortFlag, longFlag, description) ->
match: longFlag.match(OPTIONAL)
longFlag: longFlag.match(LONG_FLAG)[1]
{
name: longFlag.substr 2
shortFlag: shortFlag
longFlag: longFlag
description: description
hasArgument: !!(match and match[1])
} |
Normalize arguments by expanding merged flags into multiple flags. This allows
you to have | normalizeArguments: (args) ->
args: args.slice 0
result: []
for arg in args
if match: arg.match MULTI_FLAG
result.push '-' + l for l in match[1].split ''
else
result.push arg
result
|