Skip to content

Instantly share code, notes, and snippets.

@DavidPiper94
DavidPiper94 / Run.swift
Created October 24, 2022 04:51
Example code for article about ArgumentParser - Run Method
mutating func run() throws {
// 1
let filePathWithExtension = "\(filePath)\(fileExtension)"
// 2
try? note.write(toFile: filePathWithExtension, atomically: true, encoding: .utf8)
}
@DavidPiper94
DavidPiper94 / Input.swift
Last active October 21, 2022 04:56
Example code for article about ArgumentParser - Input
// 1
@Option(name: [.customLong("file")], help: "The file path to write the note to.")
var filePath: String
// 2
@Argument(help: "The note to write to file.")
var note: String
// 3
@Flag(name: .shortAndLong, help: "Overrides existing file.")
@DavidPiper94
DavidPiper94 / Subcommand.swift
Last active October 20, 2022 05:02
Example code for article about ArgumentParser - Subcommand
struct CLINotes: ParsableCommand {
// 1
static let configuration: CommandConfiguration = CommandConfiguration(
commandName: "clinotes",
abstract: "Manage your notes from the command line",
discussion: "Create your nodes from the command-line. Each file generated with this tool has the extension \(fileExtension).",
subcommands: [Write.self]
)
@DavidPiper94
DavidPiper94 / RuntimeValidation.Swift
Created October 20, 2022 04:19
Example code for article about ArgumentParser - Runtime validation
mutating func run() throws {
let filePathWithExtension = "\(filePath)\(fileExtension)"
// 1
do {
try note.write(toFile: filePathWithExtension, atomically: true, encoding: .utf8)
} catch {
// 2
throw RuntimeError("Couldn't write to `\(filePath)`!")
}
}
@DavidPiper94
DavidPiper94 / Validation.swift
Last active October 24, 2022 04:42
Example code for article about ArgumentParser - Validation
// 1
func validate() throws {
let filePathWithExtension = "\(filePath)\(fileExtension)"
// 2
if FileManager.default.fileExists(atPath: filePathWithExtension) && force == false {
// 3
throw ValidationError("Given file already exists. To override it, please add `--force`.")
}
}
@DavidPiper94
DavidPiper94 / RuntimeError.swift
Created October 20, 2022 04:15
Example code for article about ArgumentParser - RuntimeError
struct RuntimeError: Error, CustomStringConvertible {
var description: String
init(_ description: String) {
self.description = description
}
}
@DavidPiper94
DavidPiper94 / CLINotes.swift
Last active October 20, 2022 05:01
Example code for article about ArgumentParser - Defining the top level command
import ArgumentParser
import Foundation
// 1
struct CLINotes: ParsableCommand {
// 2
static let fileExtension = ".clinote"
// 3
@DavidPiper94
DavidPiper94 / Package.swift
Last active October 19, 2022 05:05
Example code for article about ArgumentParser - Defining the executable and dependencies
// swift-tools-version: 5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "CLINotes",
products: [
.executable(
name: "CLINotes",
@DavidPiper94
DavidPiper94 / CategoricalAxis.swift
Last active January 9, 2022 20:13
Example code for article about Audio Graphs - Using a categorical axis
// 1
enum Category: String {
case UIKit, SwiftUI
// 2
static var order = [Category.UIKit.rawValue, Category.SwiftUI.rawValue]
}
// 3
private var categoricalXAxis: AXCategoricalDataAxisDescriptor {
@DavidPiper94
DavidPiper94 / NonContinuousDataSeries.swift
Created December 29, 2021 20:03
Example code for article about Audio Graphs - Showing non-continuous data series
private var series: [AXDataSeriesDescriptor] {
let yValuesSeries1 = [4.0, 5.0, 6.0, 3.0, 2.0, 1.0, 1.0, 3.0, 6.0, 9.0]
let dataPointsSeries1 = yValuesSeries1.enumerated().map { index, yValue in
AXDataPoint(x: Double(index), y: yValue)
}
return [
AXDataSeriesDescriptor(
name: "Data Series 1",