DashDashSwift

DashDashSwift Logo

一个为 Swift CLI 项目设计的、不带预设的命令行解析器。 DashDashSwift 提供直接、强大的键值方式来访问命令行参数。

安装

DashDashSwift 可以通过 Swift Package Manager 安装。 这里是 Apple 提供的快速指南,关于如何将包集成到你的应用中。

(如果你喜欢,你也可以直接将 Sources/DashDashSwift 目录下的内容拖入你的项目中。)

入门指南

你可以使用 CommandLineParser 的一个实例来解析参数数组。 通常,你可以从 CommandLine 的一个实例中获取这些参数,但是 args(from:) 函数可以从一个普通字符串中生成这些参数。

从此,解析器具有提取字符串、整数、双精度浮点数、布尔值等的方法。

// example --name Chris --age 8
var parser = CommandLineParser()
parser.arguments = CommandLine.arguments
let name = parser.string(forKey: "name") // -> Optional("Chris")
let age = parser.int(forKey: "age")      // -> Optional(8)

解析器期望多字符标志以 -- 为前缀,并允许将单字符布尔标志与单个 - 组合在一起。 例如:

let command = `-rf --path ./input.json -o ./output.json`
var parser = CommandLineParser()
parser.arguments = CommandLineParser.args(from: command)

let inputPath = parser.string(forKey: "path") // -> Optional("./input.json")
let outputPath = parser.string(forKey: "o")   // -> Optional("./output.json")

let isRecursive = parser.bool(forKey: "r")    // -> true
let isForced = parser.bool(forKey: "f")       // -> true

示例

最简示例

import DashDashSwift

// create a parser
var parser = CommandLineParser()

let name = parser.string(forKey: "name", shortKey: "n", args: CommandLine.arguments) ?? "Anonymous"

let age = parser.int(forKey: "age", args: CommandLine.arguments) ?? 21

let height = parser.double(forKey: "height", shortKey:"h" args: CommandLine.arguments) ?? 180.0

更炫酷的用法

import DashDashSwift

// create a parser, passing in a title and description.
// this will be printed when a user requests help
var parser = CommandLineParser(title: "Chotchkie", description: "Chotchkie is a command line program to control the amount of flair on your uniform.")

// optionally register the command line arguments to parse.
// you may also pass this value in to any of the parser's functions
parser.arguments = CommandLine.arguments

// optionally register your commands with the parser.
// this allows you to ask for `key` and get `shortKey` automatically,
// as well as printing a help message with all the commands automatically.
parser.register(key: "input", shortKey: "i", index: 0, description: "The location where files should be read from.")
parser.register(key: "output",  shortKey: "o", index: 1, description: "The location where files should be saved.")
parser.register(key: "size", shortKey: "s", description: "The desired file size, in bytes")
parser.register(key: "all", shortKey: "a", description: "Boolean. Whether or not all directories should be included.")
parser.register(key: "help", shortKey: "h", description: "Print this help message")

if (parser.bool(forKey: "h")) {
    parser.printHelp()
}

if parser.bool(forKey: "all") {
    // ...
}

let size = parser.int(forKey: "size") ?? 1024

// Be flexible! Allow users to pass in unnamed arguments, 
// and fall back to them by index. 
// 
// The below code allows
//    script ./path1 ./path2
// or
//    script -i ./path1 -o ./path2
// or
//    script --input ./path1 --output ./path2
//
let input = parser.string(forKey: "input")
let output = parser.string(forKey: "output")

// you can also grab these on the fly like so:
// parser.string(forKey: "path", index: 0)


if let input = input, let output = output {
   // ...
}

谁制作了这个?

好问题。 我是 Chris——我开发 iOS 应用,例如 ChordBankBetter Notes,此外还选择性地进行客户端开发和设计工作。 在此过程中,我编写了大量命令行实用程序。

你有一个有趣且资金充足的 iOS 项目需要帮助吗? 让我们谈谈