From 82077a9bb2c2f5e4435435f9ac55da265f9e7b67 Mon Sep 17 00:00:00 2001 From: Tony Bark <35226681+tonytins@users.noreply.github.com> Date: Wed, 12 Nov 2025 18:52:42 -0500 Subject: [PATCH] Added ArgumentParser to main project - At Swift book's Functions and Closure section in Playgrounds (next up Objects and Classes) --- Package.resolved | 15 +++++++++++++ Package.swift | 12 ++++++++-- Sources/swiftyfox/swiftyfox.swift | 9 ++++---- SwiftyFox.playground/Contents.swift | 34 +++++++++++++++++------------ 4 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 Package.resolved diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..987a005 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,15 @@ +{ + "originHash" : "f3097f72511ac559eeb33298fe5d1aac2e73e26e2133181df5c213b8394d0ab0", + "pins" : [ + { + "identity" : "swift-argument-parser", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-argument-parser", + "state" : { + "revision" : "cdd0ef3755280949551dc26dee5de9ddeda89f54", + "version" : "1.6.2" + } + } + ], + "version" : 3 +} diff --git a/Package.swift b/Package.swift index d5881af..8d53745 100644 --- a/Package.swift +++ b/Package.swift @@ -5,11 +5,19 @@ import PackageDescription let package = Package( name: "swiftyfox", + dependencies: [ + // other dependencies + .package(url: "https://github.com/apple/swift-argument-parser", from: "1.3.0"), + ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products from dependencies. .executableTarget( - name: "swiftyfox" - ), + name: "swiftyfox", + dependencies: [ + // other dependencies + .product(name: "ArgumentParser", package: "swift-argument-parser"), + ]), + ] ) diff --git a/Sources/swiftyfox/swiftyfox.swift b/Sources/swiftyfox/swiftyfox.swift index c19e1e0..a851b6d 100644 --- a/Sources/swiftyfox/swiftyfox.swift +++ b/Sources/swiftyfox/swiftyfox.swift @@ -1,11 +1,10 @@ -// The Swift Programming Language -// https://docs.swift.org/swift-book +import ArgumentParser @main -struct swiftyfox { +struct swiftyfox: ParsableCommand { + @Flag var verbose = false + static func main() { print("Hello, world!") } } - - diff --git a/SwiftyFox.playground/Contents.swift b/SwiftyFox.playground/Contents.swift index 55ccc80..c8f4019 100644 --- a/SwiftyFox.playground/Contents.swift +++ b/SwiftyFox.playground/Contents.swift @@ -1,22 +1,28 @@ import Cocoa -let interestingNumbers = [ - "Prime": [2, 3, 5, 7, 11, 13], - "Fibonacci": [1, 1, 2, 3, 5, 8], - "Square": [1, 4, 9, 16, 25], -]; +func greet(_ person: String, on day: String) -> String { + return "Hello \(person), today is \(day)." +} -var largest = 0 +// "Hello Tony Bark, today is Tuesday." +greet("Tony Bark", on: "Tuesday") -for (_, numbers) in interestingNumbers { - for number in numbers { - if number > largest { - largest = number // 25 +func calcStats(scores: [Int]) -> (min: Int, max: Int, sum: Int) { + var min = scores[0] + var max = scores[0] + var sum = 0 + + for score in scores { + if score > max { + max = score + } else if score < min { + min = score } + sum += score } + + return (min, max, sum) } -var total = 0 -for i in 0..<4 { - total += i // 6 -} +let stats = calcStats(scores: [5, 3, 100, 3, 9]) +let sum = stats.sum // 120