在Swift中,如何访问命令行应用程序的命令行参数?
在Swift中,如何访问命令行应用程序的命令行参数?
更新01/17/17:已经为Swift 3更新了示例。Process
已更名为CommandLine
。
更新09/30/2015:已将示例更新为适用于Swift 2。
实际上,可以在没有Foundation 或者 C_ARGV
和C_ARGC
的情况下完成这个任务。
Swift标准库包含一个名为CommandLine
的结构体,其中有一个名为arguments
的String
集合。因此,您可以像这样切换参数:
for argument in CommandLine.arguments {
switch argument {
case "arg1":
print("first argument")
case "arg2":
print("second argument")
default:
print("an argument")
}
}
Process.arguments
和NSProcessInfo.processInfo().arguments
是相同的吗? - Franklin YuProcess
对象现在被称为CommandLine
对象。一旦Swift 3.0正式发布,这可能会被完全整合进去。 - TheSoundDefense使用顶层常量C_ARGC
和C_ARGV
。
for i in 1..C_ARGC {
let index = Int(i);
let arg = String.fromCString(C_ARGV[index])
switch arg {
case "this":
println("this yo");
case "that":
println("that yo")
default:
println("dunno bro")
}
}
请注意,我使用了1..C_ARGC
范围,因为C_ARGV
“数组”的第一个元素是应用程序的路径。
C_ARGV
变量实际上不是一个数组,但可以像数组一样进行下标操作。
Process.argc
和 Process.arguments
来替代这个,尽管最近语言的更新似乎将其更改为 CommandLine.argc
和 CommandLine.arguments
。请注意不要改变原意。 - TheSoundDefense苹果公司发布了 ArgumentParser
库,用于处理此类问题:
我们很高兴地宣布
ArgumentParser
,这是一个新的开源库,可使在 Swift 中解析命令行参数变得简单甚至是愉快的。
https://github.com/apple/swift-argument-parser
首先声明一个类型来定义您需要从命令行中收集的信息。对每个存储属性使用 ArgumentParser 的属性包装器之一进行修饰,并声明符合 ParsableCommand 协议。
ArgumentParser 库解析命令行参数,实例化您的命令类型,然后执行自定义的 run() 方法或退出,并提供有用的消息。
任何想使用旧的"getopt"(在Swift中可用)的人都可以将此用作参考。我制作了一个Swift版本的GNU C示例,可以在以下链接中找到:
http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
它带有完整的说明,已测试并且完全功能正常。它也不需要Foundation。
var aFlag = 0
var bFlag = 0
var cValue = String()
let pattern = "abc:"
var buffer = Array(pattern.utf8).map { Int8($0) }
while true {
let option = Int(getopt(C_ARGC, C_ARGV, buffer))
if option == -1 {
break
}
switch "\(UnicodeScalar(option))"
{
case "a":
aFlag = 1
println("Option -a")
case "b":
bFlag = 1
println("Option -b")
case "c":
cValue = String.fromCString(optarg)!
println("Option -c \(cValue)")
case "?":
let charOption = "\(UnicodeScalar(Int(optopt)))"
if charOption == "c" {
println("Option '\(charOption)' requires an argument.")
} else {
println("Unknown option '\(charOption)'.")
}
exit(1)
default:
abort()
}
}
println("aflag ='\(aFlag)', bflag = '\(bFlag)' cvalue = '\(cValue)'")
for index in optind..<C_ARGC {
println("Non-option argument '\(String.fromCString(C_ARGV[Int(index)])!)'")
}
CommandLine.arguments
数组创建一个参数解析器,并添加任何逻辑。arguments.swift
的文件。//Remember the first argument is the name of the executable
print("you passed \(CommandLine.arguments.count - 1) argument(s)")
print("And they are")
for argument in CommandLine.arguments {
print(argument)
}
$ swiftc arguments.swift
$ ./arguments argument1 argument2 argument3