File: OptionParsing.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (194 lines) | stat: -rw-r--r-- 7,230 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//===--------------- OptionParsing.swift - Swift Option Parser ------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import protocol TSCBasic.DiagnosticData

public enum OptionParseError : Error, Equatable, DiagnosticData {
  case unknownOption(index: Int, argument: String)
  case missingArgument(index: Int, argument: String)
  case unsupportedOption(index: Int, argument: String, option: Option, currentDriverKind: DriverKind)

  public var description: String {
    switch self {
    case let .unknownOption(index: _, argument: arg):
      return "unknown argument: '\(arg)'"
    case let .missingArgument(index: _, argument: arg):
      return "missing argument value for '\(arg)'"
    case let .unsupportedOption(index: _, argument: arg, option: option, currentDriverKind: driverKind):
      // TODO: This logic to choose the recommended kind is copied from the C++
      // driver and could be improved.
      let recommendedDriverKind: DriverKind = option.attributes.contains(.noBatch) ? .interactive : .batch
      return "option '\(arg)' is not supported by '\(driverKind.usage)'; did you mean to use '\(recommendedDriverKind.usage)'?"
    }
  }
}

extension OptionTable {
  /// Parse the given command-line arguments into a set of options.
  ///
  /// Throws an error if the command line contains any errors.
  public func parse(_ arguments: [String],
                    for driverKind: DriverKind, delayThrows: Bool = false) throws -> ParsedOptions {
    var trie = PrefixTrie<Option>()
    // Add all options, ignoring the .noDriver ones
    for opt in options where !opt.attributes.contains(.noDriver) {
      trie[opt.spelling] = opt
    }

    var parsedOptions = ParsedOptions()
    var seenDashE = false
    var index = arguments.startIndex
    while index < arguments.endIndex {
      // Capture the next argument.
      let argument = arguments[index]
      index += 1

      // Ignore empty arguments.
      if argument.isEmpty {
        continue
      }

      // If this is not a flag, record it as an input.
      if argument == "-" || argument.first! != "-" {
        // If we've seen -e, this argument and all remaining ones are arguments to the -e script.
        if driverKind == .interactive && seenDashE {
          parsedOptions.addOption(.DASHDASH, argument: .multiple(Array(arguments[(index-1)...])))
          break
        }

        parsedOptions.addInput(argument)

        // In interactive mode, synthesize a "--" argument for all args after the first input.
        if driverKind == .interactive && index < arguments.endIndex {
          parsedOptions.addOption(.DASHDASH, argument: .multiple(Array(arguments[index...])))
          break
        }

        continue
      }

      // Match to an option, identified by key. Note that this is a prefix
      // match -- if the option is a `.flag`, we'll explicitly check to see if
      // there's an unmatched suffix at the end, and pop an error. Otherwise,
      // we'll treat the unmatched suffix as the argument to the option.
      guard let option = trie[argument] else {
        if delayThrows {
          parsedOptions.addUnknownFlag(index: index - 1, argument: argument)
          continue
        } else {
          throw OptionParseError.unknownOption(index: index - 1, argument: argument)
        }
      }

      let verifyOptionIsAcceptedByDriverKind = {
        // Make sure this option is supported by the current driver kind.
        guard option.isAccepted(by: driverKind) else {
          throw OptionParseError.unsupportedOption(
            index: index - 1, argument: argument, option: option,
            currentDriverKind: driverKind)
        }
      }

      if option == .e {
        seenDashE = true
      }

      // Translate the argument
      switch option.kind {
      case .input:
        parsedOptions.addOption(option, argument: .single(argument))

      case .commaJoined:
        // Comma-separated list of arguments follows the option spelling.
        try verifyOptionIsAcceptedByDriverKind()
        let rest = argument.dropFirst(option.spelling.count)
        parsedOptions.addOption(
          option,
          argument: .multiple(rest.split(separator: ",").map { String($0) }))

      case .flag:
        // Make sure there was no extra text.
        if argument != option.spelling {
          throw OptionParseError.unknownOption(
            index: index - 1, argument: argument)
        }
        try verifyOptionIsAcceptedByDriverKind()
        parsedOptions.addOption(option, argument: .none)

      case .joined:
        // Argument text follows the option spelling.
        try verifyOptionIsAcceptedByDriverKind()
        parsedOptions.addOption(
          option,
          argument: .single(String(argument.dropFirst(option.spelling.count))))

      case .joinedOrSeparate:
        // Argument text follows the option spelling.
        try verifyOptionIsAcceptedByDriverKind()
        let arg = argument.dropFirst(option.spelling.count)
        if !arg.isEmpty {
          parsedOptions.addOption(option, argument: .single(String(arg)))
          break
        }

        if index == arguments.endIndex {
          throw OptionParseError.missingArgument(
            index: index - 1, argument: argument)
        }

        parsedOptions.addOption(option, argument: .single(arguments[index]))
        index += 1

      case .remaining:
        if argument != option.spelling {
          throw OptionParseError.unknownOption(
            index: index - 1, argument: argument)
        }
        parsedOptions.addOption(.DASHDASH, argument: .multiple(Array()))
        arguments[index...].map { String($0) }.forEach { parsedOptions.addInput($0) }
        index = arguments.endIndex

      case .separate:
        if argument != option.spelling {
          throw OptionParseError.unknownOption(
            index: index - 1, argument: argument)
        }
        try verifyOptionIsAcceptedByDriverKind()

        if index == arguments.endIndex {
          throw OptionParseError.missingArgument(
            index: index - 1, argument: argument)
        }

        parsedOptions.addOption(option, argument: .single(arguments[index]))
        index += 1

      case .multiArg:
        if argument != option.spelling {
          throw OptionParseError.unknownOption(
            index: index - 1, argument: argument)
        }
        let endIdx = index + Int(option.numArgs)
        if endIdx > arguments.endIndex {
          throw OptionParseError.missingArgument(
            index: index - 1, argument: argument)
        }
        parsedOptions.addOption(option, argument: .multiple(Array()))
        arguments[index..<endIdx].map { String($0) }.forEach { parsedOptions.addInput($0) }
        index = endIdx

      }
    }
    parsedOptions.buildIndex()
    return parsedOptions
  }
}