如何在Swift 3中使用带有命令行参数的getopt?
发布时间:2020-12-14 04:28:51 所属栏目:百科 来源:网络整理
导读:我试图在 Swift 3中使用带有命令行参数的getopt.我从 Michele Dall’Agata’s nice stackoverflow contribution开始: let pattern = "abc:"var buffer = Array( pattern.utf8 ).map { Int8($0) } 当我然后使用此代码时: let option = Int( getopt( Command
我试图在
Swift 3中使用带有命令行参数的getopt.我从
Michele Dall’Agata’s nice stackoverflow contribution开始:
let pattern = "abc:" var buffer = Array( pattern.utf8 ).map { Int8($0) } 当我然后使用此代码时: let option = Int( getopt( CommandLine.argc,CommandLine.arguments,buffer ) ) 我收到此错误:
对于CommandLine.arguments,我试图将其用作argv.有没有人知道getopt的第二个参数的正确语法?提前致谢! 解决方法
@Hamish已经回答了这个问题并解释了如何在Swift中将CommandLine.unsafeArgv传递给getopt()(以及为什么).
这是一个完整的自包含示例如何典型的getopt var aFlag = false var bFlag = false var cValue: String? while case let option = getopt(CommandLine.argc,CommandLine.unsafeArgv,"abc:"),option != -1 { switch UnicodeScalar(CUnsignedChar(option)) { case "a": aFlag = true case "b": bFlag = true case "c": cValue = String(cString: optarg) default: fatalError("Unknown option") } } print(aFlag,bFlag,cValue ?? "?") 备注: >您可以将Swift字符串(此处:“abc:”)直接传递给C while ((option = getopt(argc,argv,"abc:")) != -1) { ... } 在斯威夫特. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |