Skip to content

Commit 85b7d81

Browse files
committed
improve error handling
1 parent acc94cd commit 85b7d81

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Sources/CodeEditCLI/Open.swift

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,31 @@ extension CodeEditCLI {
5656
}
5757

5858
private func extractLineColumn(_ path: String) throws -> (path: String, line: Int?, column: Int?) {
59+
60+
// split the string at `:` to get line and column numbers
5961
let components = path.split(separator: ":")
60-
let path = String(components[0])
6162

63+
// set path to only the first component
64+
guard let first = components.first else {
65+
throw CLIError.invalidFileURL
66+
}
67+
let path = String(first)
68+
69+
// switch on the number of components
6270
switch components.count {
63-
case 1:
71+
case 1: // no line or column number provided
6472
return (path, nil, nil)
65-
case 2:
73+
74+
case 2: // only line number provided
6675
guard let row = Int(components[1]) else { throw CLIError.invalidFileURL }
6776
return (path, row, nil)
68-
case (3...):
77+
78+
case 3: // line and column number provided
6979
guard let row = Int(components[1]),
7080
let column = Int(components[2]) else { throw CLIError.invalidFileURL }
7181
return (path, row, column)
72-
default:
82+
83+
default: // any other case throw an error since this is invalid
7384
throw CLIError.invalidFileURL
7485
}
7586
}

0 commit comments

Comments
 (0)