Build Status Platform Language: Swift CocoaPods Carthage GITTER: join chat GITTER: join chat

Swiftline 是一套帮助你创建命令行应用程序的工具。Swiftline 的灵感来源于 highline

Swiftline 包含以下内容

目录

用法 安装 示例 文档 测试

用法

Colorize 🎨

Colorize 帮助在将字符串打印到终端之前对其进行样式设置。您可以更改文本颜色、文本背景和文本样式。Colorize 通过扩展 String 结构来添加样式。

要更改文本颜色,请使用 string.fstring.foreground

print("Red String".f.Red)
print("Blue String".foreground.Blue)

要更改文本背景颜色,请使用 string.bstring.background

print("I have a white background".b.White)
print("My background color is green".background.Green)

要更改文本背景样式,请使用 string.sstring.style

print("I am a bold string".s.Bold)
print("I have an underline".style.Underline)

您可以组合前景色、背景色和样式

print("I am an underlined red on white string".s.Underline.f.Red.b.White)

Ask, Choose, Agree ❓

Ask、Choose 和 Agree 用于提示用户提供更多信息。

Ask

Ask 向用户显示提示,并等待用户输入。

let userName = ask("Enter user name?")

userName 将包含用户输入的名称

Ask 可用于询问 Int、Double 或 Float 类型的值,例如询问整数

let age = ask("How old are you?", type: Int.self)

如果用户输入的内容无法转换为整数,则会向其显示新的提示,该提示将一直显示,直到用户输入 Int

How old are you?
None
You must enter a valid Integer.
?  Error
You must enter a valid Integer.
?  5
5

通过在 AskSettings 上调用 addInvalidCase 来添加验证。

let name = ask("Who are you?") { settings in
    settings.addInvalidCase("Snuffles is not allowed") { value in
        value.containsString("Snuffles")
    }
}

如果用户输入了 Snuffles,ask 将一直显示传递给 addInvalidCase 的无效消息

Who are you?
Snuffles
Snuffles is not allowed
?  Snuffles
Snuffles is not allowed
?  Snowball

Your name is Snowball

AskSettings.confirm 将要求用户在输入后确认其选择

let name = ask("Who are you?") { settings in
    settings.confirm = true
}

以上将输出

Who are you?
Snuffles
Are you sure?  YES

Your name is Snuffles

Choose

Choose 用于提示用户在几个可能的选项之间选择一个项目。

例如,要显示编程语言的选择

let choice = choose("Whats your favorite programming language? ",
    choices: "Swift", "Objective C", "Ruby", "Python", "Java :S")

这将打印

1. Swift
2. Objective C
3. Ruby
4. Python
5. Java :S
Whats your favorite programming language?

用户可以选择数字 (1..5) 或项目本身。如果用户输入错误,则会一直显示提示,直到用户做出正确的选择

Whats your favorite programming language? JavaScript
You must choose one of [1, 2, 3, 4, 5, Swift, Objective C, Ruby, Python, Java :S].
?  BBB
You must choose one of [1, 2, 3, 4, 5, Swift, Objective C, Ruby, Python, Java :S].
?  Swift

You selected Swift, good choice!

您可以自定义每个选择元素的返回值。例如,如果您想从选择中获取 Int,您可以这样做

let choice = choose("Whats your favorite programming language? ", type: Int.self) { settings in
    settings.addChoice("Swift") { 42 }
    settings.addChoice("Objective C") { 20 }
}

左侧的数字可以更改为字母,以下是如何操作

let choice = choose("Whats your favorite programming language? ", type: String.self) { settings in
   //choice value will be set to GOOD
   settings.addChoice("Swift") { "GOOD" }

   //choice value will be set to BAD
   settings.addChoice("Java") { "BAD" }

   settings.index = .Letters
   settings.indexSuffix = " ----> "
   }

这将打印

a ----> Swift
b ----> Java
Whats your favorite programming language?

Agree

Agree 用于询问用户一个 Yes/No 问题。它返回一个布尔值,表示用户输入。

let choice = agree("Are you sure you want to `rm -rf /` ?")

如果用户输入任何无效输入,agree 将一直提示他输入 Yes/No 问题

Are you sure you want to `rm -rf /` ?  What!
Please enter "yes" or "no".
Are you sure you want to `rm -rf /` ?  Wait
Please enter "yes" or "no".
Are you sure you want to `rm -rf /` ?  No

You entered false

Run 🏃

Run 提供了一种快速简洁的方式来运行外部命令并读取其标准输出和标准错误。

要执行一个简单的命令,您可以这样做

let result = run("ls -all")
print(result.stdout)

result 类型为 RunResults,它包含

虽然 run("command") 可以按空格拆分参数。有时参数拆分并非易事。如果您有多个参数要传递给要执行的命令,则应使用 run(command: String, args: String...)。以上转换为

let result = run("ls", args: "-all")

要自定义 run 函数,您可以传入一个自定义块

let result = run("ls -all") { settings in
    settings.dryRun = true
    settings.echo = [.Stdout, .Stderr, .Command]
    settings.interactive = false
}

settings 是 RunSettings 的一个实例,它包含以下变量

runWithoutCapture("command") 是一种在交互模式下运行命令的快速方法。返回值是该命令的退出代码。

Env

Env 用于读取和写入传递给脚本的环境变量

// Set enviroment variable
Env.set("key1", "value1")

// Get environment variable
Env.get("SomeKey")

// Clear all variables
Env.clear()

// Get all keys and values
Env.keys()
Env.values()

Args

返回传递给脚本的参数。例如,当调用 script -f1 val1 -f2 val2 -- val3 val4

Args.all 返回所有原始参数的数组,在此示例中,它将是 ["-f1", "val1", "-f2", "val2", "--", "val3", "val4"

Args.parsed 返回一个结构,该结构包含已解析的参数映射和参数数组,对于此示例

Args.parsed.parameters 返回 ["val3", "val4"]

Args.parsed.flags 返回标志字典 ["f1": "val1", "f2", "val2"]

Args.parsed.command 返回可执行文件本身的名称 "script"

文档

文档可以在 这里 找到

安装

您可以使用 Swift package manager 安装 Swiftline

将以下内容添加到您的 Package.swift 依赖项中

.package(url: "https://github.com/Swiftline/Swiftline.git", from: "0.6.0")

或者,通过将 https://github.com/Swiftline/Swiftline.git, version "0.6.0" 或更高版本,添加到 Xcode 中任何项目的 Swift 包列表中。

鸣谢

Daniel Beere 创建了徽标 @DanielBeere 查看 danielbeere 在 dribble 上的作品 Omar Abdelhafith 当前项目维护者 @ifnottrue