-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathCLI.swift
More file actions
326 lines (263 loc) · 9.29 KB
/
CLI.swift
File metadata and controls
326 lines (263 loc) · 9.29 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import ArgumentParser
import Foundation
private let reminders = Reminders()
private struct ShowLists: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Print the name of lists to pass to other commands")
@Option(
name: .shortAndLong,
help: "format, either of 'plain' or 'json'")
var format: OutputFormat = .plain
func run() {
reminders.showLists(outputFormat: format)
}
}
private struct ShowAll: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Print all reminders")
@Flag(help: "Show completed items only")
var onlyCompleted = false
@Flag(help: "Include completed items in output")
var includeCompleted = false
@Option(
name: .shortAndLong,
help: "Show only reminders due on this date")
var dueDate: DateComponents?
@Option(
name: .shortAndLong,
help: "format, either of 'plain' or 'json'")
var format: OutputFormat = .plain
func validate() throws {
if self.onlyCompleted && self.includeCompleted {
throw ValidationError(
"Cannot specify both --show-completed and --only-completed")
}
}
func run() {
var displayOptions = DisplayOptions.incomplete
if self.onlyCompleted {
displayOptions = .complete
} else if self.includeCompleted {
displayOptions = .all
}
reminders.showAllReminders(
dueOn: self.dueDate, displayOptions: displayOptions, outputFormat: format)
}
}
private struct Show: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Print the items on the given list")
@Argument(
help: "The list to print items from, see 'show-lists' for names",
completion: .custom(listNameCompletion))
var listName: String
@Flag(help: "Show completed items only")
var onlyCompleted = false
@Flag(help: "Include completed items in output")
var includeCompleted = false
@Option(
name: .shortAndLong,
help: "Show the reminders in a specific order, one of: \(Sort.commaSeparatedCases)")
var sort: Sort = .none
@Option(
name: [.customShort("o"), .long],
help: "How the sort order should be applied, one of: \(CustomSortOrder.commaSeparatedCases)")
var sortOrder: CustomSortOrder = .ascending
@Option(
name: .shortAndLong,
help: "Show only reminders due on this date")
var dueDate: DateComponents?
@Option(
name: .shortAndLong,
help: "format, either of 'plain' or 'json'")
var format: OutputFormat = .plain
func validate() throws {
if self.onlyCompleted && self.includeCompleted {
throw ValidationError(
"Cannot specify both --show-completed and --only-completed")
}
}
func run() {
var displayOptions = DisplayOptions.incomplete
if self.onlyCompleted {
displayOptions = .complete
} else if self.includeCompleted {
displayOptions = .all
}
reminders.showListItems(
withName: self.listName, dueOn: self.dueDate, displayOptions: displayOptions,
outputFormat: format, sort: sort, sortOrder: sortOrder)
}
}
private struct Add: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Add a reminder to a list")
@Argument(
help: "The list to add to, see 'show-lists' for names",
completion: .custom(listNameCompletion))
var listName: String
@Argument(
parsing: .remaining,
help: "The reminder contents")
var reminder: [String]
@Option(
name: .shortAndLong,
help: "The date the reminder is due")
var dueDate: DateComponents?
@Option(
name: .shortAndLong,
help: "The priority of the reminder")
var priority: Priority = .none
@Option(
name: .shortAndLong,
help: "format, either of 'plain' or 'json'")
var format: OutputFormat = .plain
@Option(
name: .shortAndLong,
help: "The notes to add to the reminder")
var notes: String?
func run() {
reminders.addReminder(
string: self.reminder.joined(separator: " "),
notes: self.notes,
toListNamed: self.listName,
dueDateComponents: self.dueDate,
priority: priority,
outputFormat: format)
}
}
private struct Complete: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Complete a reminder")
@Argument(
help: "The list to complete a reminder on, see 'show-lists' for names",
completion: .custom(listNameCompletion))
var listName: String
@Argument(
help: "The index or id of the reminder to delete, see 'show' for indexes")
var index: String
func run() {
reminders.setComplete(true, itemAtIndex: self.index, onListNamed: self.listName)
}
}
private struct Uncomplete: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Uncomplete a reminder")
@Argument(
help: "The list to uncomplete a reminder on, see 'show-lists' for names",
completion: .custom(listNameCompletion))
var listName: String
@Argument(
help: "The index or id of the reminder to delete, see 'show' for indexes")
var index: String
func run() {
reminders.setComplete(false, itemAtIndex: self.index, onListNamed: self.listName)
}
}
private struct Delete: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Delete a reminder")
@Argument(
help: "The list to delete a reminder on, see 'show-lists' for names",
completion: .custom(listNameCompletion))
var listName: String
@Argument(
help: "The index or id of the reminder to delete, see 'show' for indexes")
var index: String
func run() {
reminders.delete(itemAtIndex: self.index, onListNamed: self.listName)
}
}
func listNameCompletion(_ arguments: [String]) -> [String] {
// NOTE: A list name with ':' was separated in zsh completion, there might be more of these or
// this might break other shells
return reminders.getListNames().map { $0.replacingOccurrences(of: ":", with: "\\:") }
}
private struct Edit: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Edit the text of a reminder")
@Argument(
help: "The list to edit a reminder on, see 'show-lists' for names",
completion: .custom(listNameCompletion))
var listName: String
@Argument(
help: "The index or id of the reminder to delete, see 'show' for indexes")
var index: String
@Option(
name: .shortAndLong,
help: "The new date the reminder is due")
var dueDate: DateComponents?
@Flag(
name: .shortAndLong,
help: "Clear the due date.")
var clearDueDate: Bool = false
@Option(
name: .shortAndLong,
help: "The new priority of the reminder")
var priority: Priority?
@Flag(
name: .shortAndLong,
help: "Clear the priority of the reminder.")
var clearPriority: Bool = false
@Option(
name: .shortAndLong,
help: "The notes to set on the reminder, overwriting previous notes")
var notes: String?
@Argument(
parsing: .remaining,
help: "The new reminder contents")
var reminder: [String] = []
func validate() throws {
if self.dueDate != nil && self.clearDueDate {
throw ValidationError("Don't try to set & clear the due date at the same time.")
}
if self.reminder.isEmpty && self.notes == nil && self.dueDate == nil && !self.clearDueDate {
throw ValidationError("Must specify new reminder content, new notes, or a new due date.")
}
}
func run() {
let newText = self.reminder.joined(separator: " ")
reminders.edit(
itemAtIndex: self.index,
onListNamed: self.listName,
newText: newText.isEmpty ? nil : newText,
newNotes: self.notes,
dueDateComponents: self.dueDate,
clearDueDate: self.clearDueDate,
priority: self.priority,
clearPriority: self.clearPriority
)
}
}
private struct NewList: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Create a new list")
@Argument(
help: "The name of the new list")
var listName: String
@Option(
name: .shortAndLong,
help: "The name of the source of the list, if all your lists use the same source it will default to that")
var source: String?
func run() {
reminders.newList(with: self.listName, source: self.source)
}
}
public struct CLI: ParsableCommand {
public static let configuration = CommandConfiguration(
commandName: "reminders",
abstract: "Interact with macOS Reminders from the command line",
subcommands: [
Add.self,
Complete.self,
Uncomplete.self,
Delete.self,
Edit.self,
Show.self,
ShowLists.self,
NewList.self,
ShowAll.self,
]
)
public init() {}
}