mirror of
https://git.tonybark.com/tonytins/swiftlyfox.git
synced 2026-02-10 16:24:48 -05:00
Added ArgumentParser to main project
- At Swift book's Functions and Closure section in Playgrounds (next up Objects and Classes)
This commit is contained in:
parent
8f83d25989
commit
82077a9bb2
4 changed files with 49 additions and 21 deletions
15
Package.resolved
Normal file
15
Package.resolved
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -5,11 +5,19 @@ import PackageDescription
|
||||||
|
|
||||||
let package = Package(
|
let package = Package(
|
||||||
name: "swiftyfox",
|
name: "swiftyfox",
|
||||||
|
dependencies: [
|
||||||
|
// other dependencies
|
||||||
|
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.3.0"),
|
||||||
|
],
|
||||||
targets: [
|
targets: [
|
||||||
// Targets are the basic building blocks of a package, defining a module or a test suite.
|
// 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.
|
// Targets can depend on other targets in this package and products from dependencies.
|
||||||
.executableTarget(
|
.executableTarget(
|
||||||
name: "swiftyfox"
|
name: "swiftyfox",
|
||||||
),
|
dependencies: [
|
||||||
|
// other dependencies
|
||||||
|
.product(name: "ArgumentParser", package: "swift-argument-parser"),
|
||||||
|
]),
|
||||||
|
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
// The Swift Programming Language
|
import ArgumentParser
|
||||||
// https://docs.swift.org/swift-book
|
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct swiftyfox {
|
struct swiftyfox: ParsableCommand {
|
||||||
|
@Flag var verbose = false
|
||||||
|
|
||||||
static func main() {
|
static func main() {
|
||||||
print("Hello, world!")
|
print("Hello, world!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,28 @@
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
let interestingNumbers = [
|
func greet(_ person: String, on day: String) -> String {
|
||||||
"Prime": [2, 3, 5, 7, 11, 13],
|
return "Hello \(person), today is \(day)."
|
||||||
"Fibonacci": [1, 1, 2, 3, 5, 8],
|
|
||||||
"Square": [1, 4, 9, 16, 25],
|
|
||||||
];
|
|
||||||
|
|
||||||
var largest = 0
|
|
||||||
|
|
||||||
for (_, numbers) in interestingNumbers {
|
|
||||||
for number in numbers {
|
|
||||||
if number > largest {
|
|
||||||
largest = number // 25
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var total = 0
|
// "Hello Tony Bark, today is Tuesday."
|
||||||
for i in 0..<4 {
|
greet("Tony Bark", on: "Tuesday")
|
||||||
total += i // 6
|
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let stats = calcStats(scores: [5, 3, 100, 3, 9])
|
||||||
|
let sum = stats.sum // 120
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue