• 跳转到 … +
    browser.coffee cake.coffee coffee-script.coffee command.coffee grammar.coffee helpers.coffee index.coffee lexer.coffee nodes.coffee optparse.coffee register.coffee repl.coffee rewriter.coffee scope.litcoffee sourcemap.litcoffee
  • optparse.coffee

  • ¶
    {repeat} = require './helpers'
  • ¶

    一个简单的 OptionParser 类,用于从命令行解析选项标志。使用方法如下

    parser  = new OptionParser switches, helpBanner
    options = parser.parse process.argv
    

    第一个非选项被认为是文件(和文件选项)列表的开始,所有后续参数都保持未解析状态。

    exports.OptionParser = class OptionParser
  • ¶

    使用有效选项列表进行初始化,格式为

    [short-flag, long-flag, description]
    

    以及可选的用于使用帮助的横幅。

      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+(\*?))\]/
  • ¶

    构建并返回选项规则列表。如果可选的 short-flag 未指定,则用 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