@@ -16,25 +16,22 @@ extension CodeEditCLI {
1616 )
1717
1818 @Argument (
19- help: " The path of a file/folder to open. " ,
19+ help: """
20+ The path of a file/folder to open.
21+ When opening files, line and column numbers can be appended: `index.html:42:10`
22+ """ ,
2023 completion: . file( )
2124 )
2225 private var path : String ?
2326
24- @Option ( name: . shortAndLong, help: " The line number to open a file at. Optional. " )
25- private var line : Int ?
26-
27- @Option ( name: . shortAndLong, help: " The column to open a file at. Optional. " )
28- private var column : Int ?
29-
3027 func run( ) throws {
3128 let task = Process ( )
3229
3330 // use the `open` cli as the executable
3431 task. launchPath = " /usr/bin/open "
3532
3633 if let path {
37-
34+ let ( path , line , column ) = try extractLineColumn ( path )
3835 let openURL = try absolutePath ( path, for: task)
3936
4037 // open CodeEdit using the url scheme
@@ -57,5 +54,35 @@ extension CodeEditCLI {
5754 }
5855 return url
5956 }
57+
58+ private func extractLineColumn( _ path: String ) throws -> ( path: String , line: Int ? , column: Int ? ) {
59+
60+ // split the string at `:` to get line and column numbers
61+ let components = path. split ( separator: " : " )
62+
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
70+ switch components. count {
71+ case 1 : // no line or column number provided
72+ return ( path, nil , nil )
73+
74+ case 2 : // only line number provided
75+ guard let row = Int ( components [ 1 ] ) else { throw CLIError . invalidFileURL }
76+ return ( path, row, nil )
77+
78+ case 3 : // line and column number provided
79+ guard let row = Int ( components [ 1 ] ) ,
80+ let column = Int ( components [ 2 ] ) else { throw CLIError . invalidFileURL }
81+ return ( path, row, column)
82+
83+ default : // any other case throw an error since this is invalid
84+ throw CLIError . invalidFileURL
85+ }
86+ }
6087 }
6188}
0 commit comments