{repeat} = require './helpers'
{repeat} = require './helpers'
一個簡單的OptionParser類別,用於從命令列解析選項標記。使用方式如下
parser = new OptionParser switches, helpBanner
options = parser.parse process.argv
第一個非選項被視為檔案 (和檔案選項) 清單的開頭,而所有後續引數都不會解析。
exports.OptionParser = class OptionParser
constructor: (rules, @banner) ->
@rules = buildRules rules
解析引數清單,使用所有指定選項填入options
物件,並傳回它。第一個非選項引數之後的選項會被視為引數。options.arguments
會是一個包含剩餘引數的陣列。這是一個比許多選項解析器更簡單的 API,它們允許您為每個標記附加回呼動作。相反地,您負責詮釋選項物件。
parse: (args) ->
options = arguments: []
skippingArgument = no
originalArgs = args
args = normalizeArguments args
for arg, i in args
if skippingArgument
skippingArgument = no
continue
if arg is '--'
pos = originalArgs.indexOf '--'
options.arguments = options.arguments.concat originalArgs[(pos + 1)..]
break
isOption = !!(arg.match(LONG_FLAG) or arg.match(SHORT_FLAG))
CS 選項解析器有點奇怪;第一個非選項引數之後的選項會被視為非選項引數本身
seenNonOptionArg = options.arguments.length > 0
unless seenNonOptionArg
matchedRule = no
for rule in @rules
if rule.shortFlag is arg or rule.longFlag is arg
value = true
if rule.hasArgument
skippingArgument = yes
value = args[i + 1]
options[rule.name] = if rule.isList then (options[rule.name] or []).concat value else value
matchedRule = yes
break
throw new Error "unrecognized option: #{arg}" if isOption and not matchedRule
if seenNonOptionArg or not isOption
options.arguments.push arg
options
傳回此OptionParser的說明文字,列出並說明所有有效選項,用於--help
等。
help: ->
lines = []
lines.unshift "#{@banner}\n" if @banner
for rule in @rules
spaces = 15 - rule.longFlag.length
spaces = if spaces > 0 then repeat ' ', spaces else ''
letPart = if rule.shortFlag then rule.shortFlag + ', ' else ' '
lines.push ' ' + letPart + rule.longFlag + spaces + rule.description
"\n#{ lines.join('\n') }\n"
選項標記的正規表示式比對器。
LONG_FLAG = /^(--\w[\w\-]*)/
SHORT_FLAG = /^(-\w)$/
MULTI_FLAG = /^-(\w{2,})/
OPTIONAL = /\[(\w+(\*?))\]/
建立並傳回選項規則清單。如果未指定選用的短標記,請使用null
填補並略過。
buildRules = (rules) ->
for tuple in rules
tuple.unshift null if tuple.length < 3
buildRule tuple...
從-o
短標記、--output [DIR]
長標記和選項說明建立規則。
buildRule = (shortFlag, longFlag, description, options = {}) ->
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])
isList: !!(match and match[2])
}
將合併的旗標擴充為多個旗標,以標準化參數。這讓您可以將 -wl
視為與 --watch --lint
相同。
normalizeArguments = (args) ->
args = args[..]
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