Skip to content

Commit aad1ac0

Browse files
Make ParsableCommand.run() a mutating method (#163)
Co-authored-by: Erik Little <[email protected]>
1 parent c87d0d0 commit aad1ac0

File tree

18 files changed

+44
-44
lines changed

18 files changed

+44
-44
lines changed

Documentation/01 Getting Started.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct Count: ParsableCommand {
5252
@Argument()
5353
var outputFile: String
5454

55-
func run() throws {
55+
mutating func run() throws {
5656
print("""
5757
Counting words in '\(inputFile)' \
5858
and writing the result into '\(outputFile)'.
@@ -91,7 +91,7 @@ struct Count: ParsableCommand {
9191
@Option()
9292
var outputFile: String
9393

94-
func run() throws {
94+
mutating func run() throws {
9595
print("""
9696
Counting words in '\(inputFile)' \
9797
and writing the result into '\(outputFile)'.
@@ -135,7 +135,7 @@ struct Count: ParsableCommand {
135135
@Flag()
136136
var verbose: Bool
137137

138-
func run() throws {
138+
mutating func run() throws {
139139
if verbose {
140140
print("""
141141
Counting words in '\(inputFile)' \
@@ -177,7 +177,7 @@ struct Count: ParsableCommand {
177177
@Flag(name: .shortAndLong)
178178
var verbose: Bool
179179

180-
func run() throws { ... }
180+
mutating func run() throws { ... }
181181
}
182182
```
183183

@@ -211,7 +211,7 @@ struct Count: ParsableCommand {
211211
@Flag(name: .shortAndLong, help: "Print status updates while counting.")
212212
var verbose: Bool
213213

214-
func run() throws { ... }
214+
mutating func run() throws { ... }
215215
}
216216
```
217217

@@ -246,7 +246,7 @@ struct Count: ParsableCommand {
246246
@Flag(name: .shortAndLong, help: "Print status updates while counting.")
247247
var verbose: Bool
248248

249-
func run() throws {
249+
mutating func run() throws {
250250
if verbose {
251251
print("""
252252
Counting words in '\(inputFile)' \

Documentation/02 Arguments, Options, and Flags.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ enum ReleaseMode: String, ExpressibleByArgument {
143143
struct Example: ParsableCommand {
144144
@Option() var mode: ReleaseMode
145145

146-
func run() throws {
146+
mutating func run() throws {
147147
print(mode)
148148
}
149149
}
@@ -194,7 +194,7 @@ struct Example: ParsableCommand {
194194
@Flag(default: nil, inversion: .prefixedEnableDisable)
195195
var requiredElement: Bool
196196

197-
func run() throws {
197+
mutating func run() throws {
198198
print(index, requiredElement)
199199
}
200200
}
@@ -230,7 +230,7 @@ struct Example: ParsableCommand {
230230

231231
@Flag() var colors: [Color]
232232

233-
func run() throws {
233+
mutating func run() throws {
234234
print(cacheMethod)
235235
print(colors)
236236
}
@@ -254,7 +254,7 @@ struct Example: ParsableCommand {
254254
@Flag(name: .shortAndLong)
255255
var verbose: Int
256256

257-
func run() throws {
257+
mutating func run() throws {
258258
print("Verbosity level: \(verbose)")
259259
}
260260
}
@@ -281,7 +281,7 @@ struct Example: ParsableCommand {
281281
@Option() var name: String
282282
@Argument() var file: String?
283283

284-
func run() throws {
284+
mutating func run() throws {
285285
print("Verbose: \(verbose), name: \(name), file: \(file ?? "none")")
286286
}
287287
}
@@ -327,7 +327,7 @@ struct Example: ParsableCommand {
327327
@Option() var file: [String]
328328
@Flag() var verbose: Bool
329329

330-
func run() throws {
330+
mutating func run() throws {
331331
print("Verbose: \(verbose), files: \(file)")
332332
}
333333
}
@@ -378,7 +378,7 @@ struct Example: ParsableCommand {
378378
@Flag() var verbose: Bool
379379
@Argument() var files: [String]
380380

381-
func run() throws {
381+
mutating func run() throws {
382382
print("Verbose: \(verbose), files: \(files)")
383383
}
384384
}

Documentation/03 Commands and Subcommands.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ extension Math {
7474
@OptionGroup()
7575
var options: Math.Options
7676

77-
func run() {
77+
mutating func run() {
7878
let result = options.values.reduce(0, +)
7979
print(format(result: result, usingHex: options.hexadecimalOutput))
8080
}
@@ -87,7 +87,7 @@ extension Math {
8787
@OptionGroup()
8888
var options: Math.Options
8989

90-
func run() {
90+
mutating func run() {
9191
let result = options.values.reduce(1, *)
9292
print(format(result: result, usingHex: options.hexadecimalOutput))
9393
}
@@ -130,7 +130,7 @@ extension Math.Statistics {
130130
func calculateMedian() -> Double { ... }
131131
func calculateMode() -> [Double] { ... }
132132

133-
func run() {
133+
mutating func run() {
134134
switch kind {
135135
case .mean:
136136
print(calculateMean())
@@ -153,7 +153,7 @@ extension Math.Statistics {
153153
@Argument(help: "A group of floating-point values to operate on.")
154154
var values: [Double]
155155

156-
func run() {
156+
mutating func run() {
157157
if values.isEmpty {
158158
print(0.0)
159159
} else {

Documentation/04 Customizing Help.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct Repeat: ParsableCommand {
8787
@Argument(help: "The phrase to repeat.")
8888
var phrase: String
8989

90-
func run() throws {
90+
mutating func run() throws {
9191
while true { print(phrase) }
9292
}
9393
}
@@ -131,7 +131,7 @@ struct Example: ParsableCommand {
131131
@Option(name: .shortAndLong, help: "The number of history entries to show.")
132132
var historyDepth: Int
133133

134-
func run() throws {
134+
mutating func run() throws {
135135
printHistory(depth: historyDepth)
136136
}
137137
}

Documentation/05 Validation and Errors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct Select: ParsableCommand {
3232
}
3333
}
3434

35-
func run() {
35+
mutating func run() {
3636
print(elements.shuffled().prefix(count).joined(separator: "\n"))
3737
}
3838
}
@@ -66,7 +66,7 @@ The `ValidationError` type is a special `ArgumentParser` error — a validation
6666
struct LineCount: ParsableCommand {
6767
@Argument() var file: String
6868

69-
func run() throws {
69+
mutating func run() throws {
7070
let contents = try String(contentsOfFile: file, encoding: .utf8)
7171
let lines = contents.split(separator: "\n")
7272
print(lines.count)
@@ -94,7 +94,7 @@ struct RuntimeError: Error, CustomStringConvertible {
9494
struct Example: ParsableCommand {
9595
@Argument() var inputFile: String
9696

97-
func run() throws {
97+
mutating func run() throws {
9898
if !ExampleCore.processFile(inputFile) {
9999
// ExampleCore.processFile(_:) prints its own errors
100100
// and returns `false` on failure.

Documentation/06 Manual Parsing and Testing.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ Let's see how this works by using the `Math` command and subcommands defined in
5656

5757
```swift
5858
do {
59-
let command = try Math.parseAsRoot()
59+
var command = try Math.parseAsRoot()
6060

6161
switch command {
62-
case let command as Math.Add:
62+
case var command as Math.Add:
6363
print("You chose to add \(command.options.values.count) values.")
6464
command.run()
6565
default:

Examples/math/main.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ extension Math {
5757
@OptionGroup()
5858
var options: Options
5959

60-
func run() {
60+
mutating func run() {
6161
let result = options.values.reduce(0, +)
6262
print(format(result, usingHex: options.hexadecimalOutput))
6363
}
@@ -70,7 +70,7 @@ extension Math {
7070
@OptionGroup()
7171
var options: Options
7272

73-
func run() {
73+
mutating func run() {
7474
let result = options.values.reduce(1, *)
7575
print(format(result, usingHex: options.hexadecimalOutput))
7676
}
@@ -145,7 +145,7 @@ extension Math.Statistics {
145145
.map { k, _ in k }
146146
}
147147

148-
func run() {
148+
mutating func run() {
149149
switch kind {
150150
case .mean:
151151
print(calculateMean())
@@ -168,7 +168,7 @@ extension Math.Statistics {
168168
@Argument(help: "A group of floating-point values to operate on.")
169169
var values: [Double]
170170

171-
func run() {
171+
mutating func run() {
172172
if values.isEmpty {
173173
print(0.0)
174174
} else {

Examples/repeat/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Repeat: ParsableCommand {
2121
@Argument(help: "The phrase to repeat.")
2222
var phrase: String
2323

24-
func run() throws {
24+
mutating func run() throws {
2525
let repeatCount = count ?? .max
2626

2727
for i in 1...repeatCount {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Repeat: ParsableCommand {
2121
@Argument(help: "The phrase to repeat.")
2222
var phrase: String
2323

24-
func run() throws {
24+
mutating func run() throws {
2525
let repeatCount = count ?? .max
2626

2727
for i in 1...repeatCount {

Sources/ArgumentParser/Parsable Types/EnumerableFlag.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
/// struct Example: ParsableCommand {
2323
/// @Flag() var sizes: [Size]
2424
///
25-
/// func run() {
25+
/// mutating func run() {
2626
/// print(sizes)
2727
/// }
2828
/// }

0 commit comments

Comments
 (0)