Skip to content

Commit b08ceaa

Browse files
committed
Throw an error if an option is unsupported for a given driver kind
1 parent a8d3820 commit b08ceaa

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public struct Driver {
205205

206206
self.driverKind = try Self.determineDriverKind(args: &args)
207207
self.optionTable = OptionTable()
208-
self.parsedOptions = try optionTable.parse(Array(args), forInteractiveMode: self.driverKind == .interactive)
208+
self.parsedOptions = try optionTable.parse(Array(args), for: self.driverKind)
209209

210210
let explicitTarget = (self.parsedOptions.getLastArgument(.target)?.asSingle)
211211
.map {

Sources/SwiftDriver/Options/OptionParsing.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
public enum OptionParseError : Error, Equatable {
1313
case unknownOption(index: Int, argument: String)
1414
case missingArgument(index: Int, argument: String)
15+
case unsupportedOption(index: Int, option: Option)
1516
}
1617

1718
extension OptionTable {
1819
/// Parse the given command-line arguments into a set of options.
1920
///
2021
/// Throws an error if the command line contains any errors.
2122
public func parse(_ arguments: [String],
22-
forInteractiveMode isInteractiveMode: Bool = false) throws -> ParsedOptions {
23+
for driverKind: DriverKind) throws -> ParsedOptions {
2324
var trie = PrefixTrie<String.UTF8View, Option>()
2425
for opt in options {
2526
trie[opt.spelling.utf8] = opt
@@ -42,7 +43,7 @@ extension OptionTable {
4243
parsedOptions.addInput(argument)
4344

4445
// In interactive mode, synthesize a "--" argument for all args after the first input.
45-
if isInteractiveMode && index < arguments.endIndex {
46+
if driverKind == .interactive && index < arguments.endIndex {
4647
parsedOptions.addOption(.DASHDASH, argument: .multiple(Array(arguments[index...])))
4748
break
4849
}
@@ -59,6 +60,10 @@ extension OptionTable {
5960
index: index - 1, argument: argument)
6061
}
6162

63+
guard option.isAccepted(by: driverKind) else {
64+
throw OptionParseError.unsupportedOption(index: index - 1, option: option)
65+
}
66+
6267
// Translate the argument
6368
switch option.kind {
6469
case .input:

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,38 @@ final class SwiftDriverTests: XCTestCase {
2121
let results = try options.parse([
2222
"input1", "-color-diagnostics", "-Ifoo", "-I", "bar spaces",
2323
"-I=wibble", "input2", "-module-name", "main",
24-
"-sanitize=a,b,c", "--", "-foo", "-bar"])
24+
"-sanitize=a,b,c", "--", "-foo", "-bar"], for: .batch)
2525
XCTAssertEqual(results.description,
2626
"input1 -color-diagnostics -I foo -I 'bar spaces' -I=wibble input2 -module-name main -sanitize=a,b,c -- -foo -bar")
2727
}
2828

2929
func testParseErrors() {
3030
let options = OptionTable()
3131

32-
XCTAssertThrowsError(try options.parse(["-unrecognized"])) { error in
32+
XCTAssertThrowsError(try options.parse(["-unrecognized"], for: .batch)) { error in
3333
XCTAssertEqual(error as? OptionParseError, .unknownOption(index: 0, argument: "-unrecognized"))
3434
}
3535

36-
XCTAssertThrowsError(try options.parse(["-I"])) { error in
36+
XCTAssertThrowsError(try options.parse(["-I"], for: .batch)) { error in
3737
XCTAssertEqual(error as? OptionParseError, .missingArgument(index: 0, argument: "-I"))
3838
}
3939

40-
XCTAssertThrowsError(try options.parse(["-color-diagnostics", "-I"])) { error in
40+
XCTAssertThrowsError(try options.parse(["-color-diagnostics", "-I"], for: .batch)) { error in
4141
XCTAssertEqual(error as? OptionParseError, .missingArgument(index: 1, argument: "-I"))
4242
}
4343

44-
XCTAssertThrowsError(try options.parse(["-module-name"])) { error in
44+
XCTAssertThrowsError(try options.parse(["-module-name"], for: .batch)) { error in
4545
XCTAssertEqual(error as? OptionParseError, .missingArgument(index: 0, argument: "-module-name"))
4646
}
47+
48+
XCTAssertThrowsError(try options.parse(["-o"], for: .interactive)) { error in
49+
XCTAssertEqual(error as? OptionParseError, .unsupportedOption(index: 0, option: .o))
50+
}
51+
52+
XCTAssertThrowsError(try options.parse(["-repl"], for: .batch)) { error in
53+
XCTAssertEqual(error as? OptionParseError, .unsupportedOption(index: 0, option: .repl))
54+
}
55+
4756
}
4857

4958
func testInvocationRunModes() throws {
@@ -293,7 +302,7 @@ final class SwiftDriverTests: XCTestCase {
293302
XCTAssertEqual(driver.moduleName, "main")
294303
}
295304

296-
try assertNoDriverDiagnostics(args: "swiftc", "-repl") { driver in
305+
try assertNoDriverDiagnostics(args: "swift", "-repl") { driver in
297306
XCTAssertNil(driver.moduleOutput)
298307
XCTAssertEqual(driver.moduleName, "REPL")
299308
}

0 commit comments

Comments
 (0)