The Command Line Toolboxmklbtz.com/files/command-line-toolbox/slides.pdfMint !> mint install...

88
The Command Line Toolbox A Crash Course on building your own CLI tools

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

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

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

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

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

! 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

! 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

! 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

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

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

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

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

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

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

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

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

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

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

Swift Package Manager

"Installing" the binary

> swift build> cp .build/debug/foodpls /usr/local/bin/

Page 19: 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

Marathon !

by John Sundellgithub.com/JohnSundell/Marathon

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

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

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

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

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

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

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

Marathon !

> marathon install https://example.com/script.swift

GitHub Gists: !

Page 27: 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

Mint !

by Yonas Kolbgithub.com/yonaskolb/mint

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

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

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 30: 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

Mint !

> mint run realm/[email protected] swiftlint

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

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

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

! 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

! 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

! 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

! 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

! 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

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

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

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

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

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

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

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

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 46: 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

Commanderby Kyle Fullergithub.com/kylef/Commander

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

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

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

Commander

Type inference !

Good for medium complexity !

Automatic Numeric & Array conversion !

Page 50: 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

Commandantby Carthagegithub.com/Carthage/Commandant

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

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

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

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

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

Commandant

Good for complex tools with lots of options !

Battle-tested by Carthage !

Custom operators !

Page 56: 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

Beak !

by Yonas Kolbgithub.com/yonaskolb/beak

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

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

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

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

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

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

! 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

! 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

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

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

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

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

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

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

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

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

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

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

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

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

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

Error Codes

import Darwinexit(-1)

Page 78: 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

Filesby John Sundellgithub.com/JohnSundell/Files

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

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

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

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

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

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

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

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

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

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

The Command Line Toolbox !

A Crash Course on building your own CLI toolsMichael Bates — @mklbtz — mklbtz.com