|
8 | 8 | import ArgumentParser |
9 | 9 | import Foundation |
10 | 10 |
|
| 11 | +// ################################################## |
| 12 | +// This needs to be changed prior to every release! |
| 13 | +// ################################################## |
| 14 | +let CLI_VERSION = "0.0.3" |
| 15 | + |
11 | 16 | struct CodeEditCLI: ParsableCommand { |
12 | 17 | static let configuration = CommandConfiguration( |
13 | 18 | commandName: "codeedit-cli", |
14 | | - abstract: "A command-line tool to open files/folders in CodeEdit.app." |
15 | | - ) |
16 | | - |
17 | | - @Argument( |
18 | | - help: "The path of a file/folder to open.", |
19 | | - completion: .file() |
| 19 | + abstract: """ |
| 20 | + A set of command line tools that ship with CodeEdit |
| 21 | + which allow users to open and interact with editor via the command line. |
| 22 | +
|
| 23 | + Version: \(CLI_VERSION) |
| 24 | + """, |
| 25 | + subcommands: [Open.self, Version.self], |
| 26 | + defaultSubcommand: Open.self |
20 | 27 | ) |
21 | | - private var path: String |
22 | | - |
23 | | - @Option(name: .shortAndLong, help: "The line number to open a file at. Optional.") |
24 | | - private var line: Int? |
25 | | - |
26 | | - @Option(name: .shortAndLong, help: "The column to open a file at. Optional.") |
27 | | - private var column: Int? |
28 | 28 |
|
29 | 29 | init() {} |
30 | 30 |
|
31 | | - func run() throws { |
32 | | - let task = Process() |
33 | | - let openURL = try absolutePath(for: task) |
34 | | - |
35 | | - // use the `open` cli as the executable |
36 | | - task.launchPath = "/usr/bin/open" |
37 | | - |
38 | | - // open CodeEdit using the url scheme |
39 | | - if let line, !openURL.hasDirectoryPath { |
40 | | - task.arguments = ["-u", "codeedit://\(openURL.path):\(line):\(column ?? 1)"] |
41 | | - } else { |
42 | | - task.arguments = ["-u", "codeedit://\(openURL.path)"] |
43 | | - } |
44 | | - |
45 | | - try task.run() |
46 | | - } |
47 | | - |
48 | | - private func absolutePath(for task: Process) throws -> URL { |
49 | | - guard let workingDirectory = task.currentDirectoryURL, |
50 | | - let url = URL(string: path, relativeTo: workingDirectory) else { |
51 | | - throw CLIError.invalidWorkingDirectory |
52 | | - } |
53 | | - return url |
54 | | - } |
55 | | - |
56 | 31 | enum CLIError: Error { |
57 | 32 | case invalidWorkingDirectory |
58 | 33 | } |
|
0 commit comments