The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install...
Transcript of The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install...
![Page 1: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/1.jpg)
The Command Line Toolbox !
A Crash Course on building your own CLI tools
![Page 2: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/2.jpg)
Michael Bates
Kentucky !"
Swing Dance
#$
Learn Swift Louisville Organizer
Day job: Python & JS !
CLI tools: Swift
"
![Page 3: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/3.jpg)
Goals
Learn Tips & Tricks
Discover Open Source Packages
Build your own tools!
![Page 4: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/4.jpg)
Command Lines Shells
Necessary !
Super-powerful !
Not great languages !
![Page 5: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/5.jpg)
! Running Scripts!
Building Interfaces!
Effective I/O
![Page 6: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/6.jpg)
! Running Scripts
![Page 7: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/7.jpg)
! Running Scripts
1. Single executable file
2. Swift Package Manager
![Page 8: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/8.jpg)
Single Executable File
//FoodPls.swift
print("
!"#$%&'()*+,-./01
".randomElement()!)
![Page 9: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/9.jpg)
Single Executable File
//FoodPls.swift
print("
!"#$%&'()*+,-./01
".randomElement()!)
> swift FoodPls.swift!
![Page 10: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/10.jpg)
Single Executable File
//FoodPls.swift
print("
!"#$%&'()*+,-./01
".randomElement()!)
> swift FoodPls.swift!
> foodpls
"
![Page 11: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/11.jpg)
Getting Fancy
1. chmod +x FoodPls.swift
![Page 12: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/12.jpg)
Getting Fancy
1. chmod +x FoodPls.swift
2. #!/usr/bin/xcrun swift
![Page 13: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/13.jpg)
Getting Fancy
1. chmod +x FoodPls.swift
2. #!/usr/bin/xcrun swift
3. cp FoodPls.swift /usr/local/bin/foodpls
![Page 14: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/14.jpg)
Getting Fancy
1. chmod +x FoodPls.swift
2. #!/usr/bin/xcrun swift
3. cp FoodPls.swift /usr/local/bin/foodpls
> foodpls!
![Page 15: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/15.jpg)
Swift Package Manger
Executable target
![Page 16: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/16.jpg)
Swift Package Manger
Executable target — from scratch
> xcrun swift package init --type=executable
![Page 17: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/17.jpg)
Swift Package Manger
Executable target — existing package
targets: [ .target(name: "foodpls", dependencies: []), // ...]
// Sources/foodpls/main.swift
![Page 18: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/18.jpg)
Swift Package Manager
"Installing" the binary
> swift build> cp .build/debug/foodpls /usr/local/bin/
![Page 20: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/20.jpg)
Marathon !
✔ Write, run, and install single-file scripts✔
Install dependencies
![Page 21: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/21.jpg)
Marathon !
> marathon run FoodPls.swift!
![Page 22: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/22.jpg)
Marathon !
> marathon edit FoodPls.swift!
Updating packages...
✏
Opening foodpls.xcodeproj/
![Page 23: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/23.jpg)
Marathon !
> marathon install FoodPls.swift !
Compiling script... Installing binary...
"
FoodPls.swift installed at /usr/local/bin/foodpls
![Page 24: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/24.jpg)
Marathon !
import Files // marathon:https://github.com/JohnSundell/Files.git
![Page 25: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/25.jpg)
Marathon !
import Files // marathon:https://github.com/JohnSundell/Files.git
> marathon add https://github.com/JohnSundell/Files.git
![Page 26: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/26.jpg)
Marathon !
> marathon install https://example.com/script.swift
GitHub Gists: !
![Page 28: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/28.jpg)
Mint !
Install executables from any Swift Package
— Carthage
— SwiftLint
— Sourcery
— Your own tools!
![Page 29: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/29.jpg)
Mint !
> mint install realm/SwiftLint!
Finding latest version of SwiftLint
!
Resolved latest version of SwiftLint to 0.28.0
!
Cloning https://github.com/realm/SwiftLint.git 0.28.0...
!
Building SwiftLint Package with SPM...
!
Installing SwiftLint...
!
Installed SwiftLint 0.28.0
!
Linked swiftlint 0.28.0 to /usr/local/bin.
> swiftlint version0.28.0
![Page 31: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/31.jpg)
Mint !
Mintfile
Carthage/Carthagerealm/SwiftLintkrzysztofzablocki/[email protected]...
Better than a readme !
![Page 32: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/32.jpg)
Gotchas — Mint !
and Marathon "
Building from source
Check your version
Swiftenv considered harmful:Package.swift not found
![Page 33: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/33.jpg)
! Building Interfaces
![Page 34: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/34.jpg)
! Building Interfaces
Complex Behavior + Good UI = !
![Page 35: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/35.jpg)
! Building Interfaces
Complex Behavior + Good UI = !
GUI !
AnimationsCLI
!
Sub-commands
![Page 36: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/36.jpg)
! Building Interfaces
> todos add "buy milk"
![Page 37: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/37.jpg)
! Building Interfaces
> todos add "buy milk"
1. DIY sub-commands
2. Packages
![Page 38: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/38.jpg)
DIY Sub-Commands
let args = CommandLine.arguments.dropfirst()
![Page 39: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/39.jpg)
DIY Sub-Commands
let args = CommandLine.arguments.dropfirst()
dropFirst: removes program name
![Page 40: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/40.jpg)
DIY Sub-Commands
let args = CommandLine.arguments.dropfirst()let cmd = args[0]!
![Page 41: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/41.jpg)
DIY Sub-Commands
let args = CommandLine.arguments.dropfirst()
enum Command: String { case add // rm, list, etc.}
let cmd = args.first.map(Command(rawValue:))
![Page 42: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/42.jpg)
DIY Sub-Commands
switch cmd {case nil: fatalError("Unrecognized command")case .add: addTodo(title: CommandLine.arguments[1])}
![Page 43: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/43.jpg)
DIY Sub-Commands
switch cmd {case nil: fatalError("Unrecognized command")case .add: addTodo(title: CommandLine.arguments[1])}
> todos add "buy milk"
![Page 44: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/44.jpg)
DIY Sub-Commands
Good for simple scripts !
Doesn't scale well !
Manual type conversion !
![Page 45: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/45.jpg)
DIY Sub-Commands
> todos helpAvailable commands: add Create a new task do Complete tasks by ID edit Change the title of a task ls List outstanding tasks rm Remove tasks undo Un-complete tasks by ID
See mklbtz/finch for a full implementation of this!
![Page 47: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/47.jpg)
Commander
import Commander
Group { $0.command("add") { (title: String) in addTask(title: title) }}.run()
![Page 48: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/48.jpg)
Commander
import Commander
Group { $0.command("add") { (title: String) in addTask(title: title) }}.run()
> todos add "buy milk"
![Page 49: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/49.jpg)
Commander
Type inference !
Good for medium complexity !
Automatic Numeric & Array conversion !
![Page 51: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/51.jpg)
Commandant
import Commandant
let commands = CommandRegistry<String>()
commands.register(AddCommand(manager: try TaskManager()))
commands.register(HelpCommand(registry: commands))
commands.main(defaultVerb: "help") { error in print(error)}
![Page 52: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/52.jpg)
Commandant
struct AddCommand: CommandProtocol { let verb = "add" let function = "Create a new task"
func run(_ options: Options) -> Result<Void, String> { // ... }}
![Page 53: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/53.jpg)
Commandant
extension AddCommand { struct Options: OptionsProtocol { let title: String
static func evaluate(_ m: CommandMode) -> Result<Options, CommandantError<String>> { return Options.init <*> m <|* Argument<String>(usage: "Title for task") } }}
![Page 54: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/54.jpg)
Commandant
extension AddCommand { struct Options: OptionsProtocol { let title: String
static func evaluate(_ m: CommandMode) -> Result<Options, CommandantError<String>> { return Options.init <*> m <|* Argument<String>(usage: "Title for task") } }}
> todos add "buy milk"
![Page 55: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/55.jpg)
Commandant
Good for complex tools with lots of options !
Battle-tested by Carthage !
Custom operators !
![Page 57: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/57.jpg)
Beak !
Static-analysis with SourceKit
Generated interface & help
Dependency management like Mint !
![Page 58: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/58.jpg)
Beak !
// beak.swift
/// Create a new taskpublic func add(title: String) {}
![Page 59: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/59.jpg)
Beak !
// beak.swift
/// Create a new taskpublic func add(title: String) {}
> beak listFunctions: add: Create a new task
> beak run add --title="buy milk"
![Page 60: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/60.jpg)
Beak !
// beak.swift
/// Create a new taskpublic func add(_ title: String) {}
> beak listFunctions: add: Create a new task
> beak run add "buy milk"
![Page 61: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/61.jpg)
Beak !
Great for task-runners !
Use with Mint for super-productivity !
![Page 62: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/62.jpg)
! Effective I/O
![Page 63: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/63.jpg)
! Effective I/O
1. stdin
2. stderr
3. Exit codes
4. Files
![Page 64: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/64.jpg)
stdin
func readline() -> String?
![Page 65: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/65.jpg)
stdin
print("What is your name?")print("> ", terminator: "")
let name = readLine() ?? "stranger"
print("Hello, \(name)")
![Page 66: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/66.jpg)
stdin
print("What is your name?")print("> ", terminator: "")
let name = readLine() ?? "stranger"
print("Hello, \(name)")
What is your name?> MichaelHello, Michael
![Page 67: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/67.jpg)
stdin
print("What is your name?")print("> ", terminator: "")
let name = readLine() ?? "stranger"
print("Hello, \(name)")
What is your name?> ^DHello, stranger
![Page 68: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/68.jpg)
stdin
while let input = readLine() { // ...}
![Page 69: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/69.jpg)
stdin
sequence(first: "", next: { _ in readLine(strippingNewline: false) }).joined()
![Page 70: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/70.jpg)
stderr
![Page 71: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/71.jpg)
stderr
func print<Target>(_: Any..., to: inout Target) where Target : TextOutputStream
![Page 72: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/72.jpg)
stderr
func print<Target>(_: Any..., to: inout Target) where Target : TextOutputStream
/// Returns the file handle associated with the standard error fileclass var standardError: FileHandle { get }
![Page 73: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/73.jpg)
stderr
extension FileHandle: TextOutputStream { public func write(_ string: String) { if let data = string.data(using: .utf8) { self.write(data) } }}
![Page 74: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/74.jpg)
stderr
func errorPrint(_ item: Any) { var stderr = FileHandle.standardError print(item, to: &stderr)}
![Page 75: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/75.jpg)
Error Codes
![Page 76: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/76.jpg)
Error Codes
fatalError("oops!")
Lots of stack dump info !
![Page 77: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/77.jpg)
Error Codes
import Darwinexit(-1)
![Page 79: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/79.jpg)
Files
Wrapper around Foundation APIs !
Very convenient to use !
![Page 80: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/80.jpg)
Files
let folder = try Folder(path: "/users/john/folder")
let file = try folder.createFile(named: "file.json")try file.write(string: "{\"hello\": \"world\"}")
try file.delete()try folder.delete()
![Page 81: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/81.jpg)
Files
for file in try Folder(path: "MyFolder").files { try file.rename(to: file.nameWithoutExtension.capitalized)}
![Page 82: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/82.jpg)
Files
let origin = try Folder(path: "/users/john/folderA")let target = try Folder(path: "/users/john/folderB")try origin.files.move(to: target)
![Page 83: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/83.jpg)
Files
✔ SPM✔
Carthage✔
CocoaPods
![Page 84: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/84.jpg)
Wrap-up
![Page 85: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/85.jpg)
Wrap-up
1.!
Running Scripts
— single-files & packages
— JohnSundell/Marathon
"
— yonaskolb/Mint
#
![Page 86: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/86.jpg)
Wrap-up
1. Building Interfaces
— DIY
— kylef/commander
— Carthage/Commandant
— yonaskolb/beak
![Page 87: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/87.jpg)
Wrap-up
1. Effective I/O
— stdin
— stderr
— error codes
— JohnSundell/Files
![Page 88: The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install realm/SwiftLint! Finding latest version of SwiftLint Resolved latest version of SwiftLint](https://reader034.fdocuments.us/reader034/viewer/2022042408/5f242b5ca3f7ae50d10afb90/html5/thumbnails/88.jpg)
The Command Line Toolbox !
A Crash Course on building your own CLI toolsMichael Bates — @mklbtz — mklbtz.com