一个纯 Swift 库,用于创建命令行界面。
注意:CommandLineKit master
分支需要 Xcode 9 / Swift 4.0。 如果您使用的是旧版本的 Swift,请查看早期版本。
CommandLine 的目标是拥有一个简单且不言自明的 API。
import CommandLineKit
let cli = CommandLineKit.CommandLine()
let filePath = StringOption(shortFlag: "f", longFlag: "file", required: true,
helpMessage: "Path to the output file.")
let compress = BoolOption(shortFlag: "c", longFlag: "compress",
helpMessage: "Use data compression.")
let help = BoolOption(shortFlag: "h", longFlag: "help",
helpMessage: "Prints a help message.")
let verbosity = CounterOption(shortFlag: "v", longFlag: "verbose",
helpMessage: "Print verbose messages. Specify multiple times to increase verbosity.")
cli.addOptions(filePath, compress, help, verbosity)
do {
try cli.parse()
} catch {
cli.printUsage(error)
exit(EX_USAGE)
}
print("File path is \(filePath.value!)")
print("Compress is \(compress.value)")
print("Verbosity is \(verbosity.value)")
有关其他 Option 类型,请参阅 Option.swift
。
要在您的项目中使用 CommandLineKit,请将其添加到您的工作区,然后将 CommandLineKit.framework 添加到目标的Build Phases / Link Binary With Libraries设置中。
如果您正在构建命令行工具并且需要将此框架和其他框架嵌入到其中,请按照http://colemancda.github.io/2015/02/12/embedded-swift-frameworks-osx-command-line-tools/中的步骤将 Swift 框架链接到您的命令行工具。
如果您正在构建独立的命令行工具,则需要将 CommandLineKit 源文件直接添加到您的目标,因为 Xcode 尚无法构建包含 Swift 代码的静态库。
Usage: example [options]
-f, --file:
Path to the output file.
-c, --compress:
Use data compression.
-h, --help:
Prints a help message.
-v, --verbose:
Print verbose messages. Specify multiple times to increase verbosity.
您可以通过提供 formatOutput
函数来完全自定义用法消息。 例如,Rainbow 提供了一种方便的方式来生成彩色输出
import Rainbow
cli.formatOutput = { s, type in
var str: String
switch(type) {
case .Error:
str = s.red.bold
case .OptionFlag:
str = s.green.underline
case .OptionHelp:
str = s.blue
default:
str = s
}
return cli.defaultFormat(str, type: type)
}
这些命令行是等效的
$ ./example -c -v -f /path/to/file
$ ./example -cvf /path/to/file
$ ./example -c --verbose --file /path/to/file
$ ./example -cv --file /path/to/file
$ ./example --compress -v --file=/path/to/file
可以使用 '--' 停止选项处理,如同 getopt(3) 中一样。
这会将负 42 传递给整数选项,并将负 3.1419 传递给浮点数选项
$ ./example2 -i -42 --float -3.1419
当区域设置使用备用小数点字符时,浮点数将被正确处理
$ LC_NUMERIC=sv_SE.UTF-8 ./example2 --float 3,1419
enum Operation: String {
case create = "c"
case extract = "x"
case list = "l"
case verify = "v"
}
let cli = CommandLineKit.CommandLine()
let op = EnumOption<Operation>(shortFlag: "o", longFlag: "operation", required: true,
helpMessage: "File operation - c for create, x for extract, l for list, or v for verify.")
cli.setOptions(op)
do {
try cli.parse()
} catch {
cli.printUsage(error)
exit(EX_USAGE)
}
switch op.value {
case Operation.Create:
// Create file
case Operation.Extract:
// Extract file
// Remainder of cases
}
注意:枚举必须可以从 String 值初始化。
$ ./example3 -👍 --👻
(请不要真的这样做)
版权所有 (c) 2014 Ben Gollmer。
根据 Apache License, Version 2.0(“许可证”)获得许可; 除非遵守许可证,否则您不得使用此文件。 您可以在以下位置获得许可证的副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则根据“原样”分发的软件不包含任何形式的保证或条件,无论是明示的还是暗示的。 有关管理权限和限制的特定语言,请参阅许可证。