如何在Swift 3中使用带有命令行参数的getopt?

时间:2022-09-06 14:04:44

I'm trying to use getopt with command line arguments in Swift 3. I have from Michele Dall'Agata's nice * contribution:

我正在尝试在Swift 3中使用带有命令行参数的getopt。我有来自Michele Dall'Agata的很好的*贡献:

let pattern = "abc:"
var buffer = Array( pattern.utf8 ).map { Int8($0) }

When I then use this code:

当我然后使用此代码时:

let option = Int( getopt( CommandLine.argc, CommandLine.arguments, buffer ) )

I get this error:

我收到此错误:

Cannot convert value of type '[String]' to expected argument type 'UnsafePointer<UnsafeMutablePointer<Int8>?>!'

无法将'[String]'类型的值转换为预期的参数类型'UnsafePointer ?>!'

for CommandLine.arguments, which I am trying to use as argv. Does anyone know the proper syntax for the 2nd argument for getopt? Thanks in advance!

对于CommandLine.arguments,我试图将其用作argv。有没有人知道getopt的第二个参数的正确语法?提前致谢!

2 个解决方案

#1


6  

@Hamish already answered the question and explained how to pass CommandLine.unsafeArgv to getopt() in Swift (and why).

@Hamish已经回答了这个问题并解释了如何在Swift中将CommandLine.unsafeArgv传递给getopt()(以及为什么)。

Here is a complete self-contained example how a typical getopt loop can be implemented in Swift 3:

这是一个完整的自包含示例,如何在Swift 3中实现典型的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 ?? "?")

Remarks:

备注:

  • You can pass a Swift string (here: "abc:") directly to a C function expecting a (constant) C string, the compiler will automatically generate a temporary UTF-8 representation.
  • 您可以将Swift字符串(此处:“abc:”)直接传递给期望(常量)C字符串的C函数,编译器将自动生成临时UTF-8表示。
  • getopt() return either -1 (if the argument list is exhausted) or an unsigned char converted to an int. Therefore it is safe to convert the return value to CUnsignedChar (which is UInt8 in Swift).
  • getopt()返回-1(如果参数列表已用尽)或unsigned char转换为int。因此,将返回值转换为CUnsignedChar(在Swift中为UInt8)是安全的。
  • while is used (abused?) with pattern matching plus an additional boolean condition to implement the typical C pattern

    虽然使用(滥用?)模式匹配加上一个额外的布尔条件来实现典型的C模式

    while ((option = getopt(argc, argv, "abc:")) != -1) { ... }
    

    in Swift.

    在斯威夫特。

#2


5  

CommandLine.arguments gives you a friendly Swift [String] of the arguments passed – however you're looking to send the arguments straight back to C. Therefore you can simply use CommandLine.unsafeArgv instead, which will give you the actual raw value of argv passed to your program.

CommandLine.arguments为您提供了传递的参数的友好Swift [String] - 但是您希望将参数直接发送回C.因此您只需使用CommandLine.unsafeArgv,它将为您提供argv的实际原始值传递给你的程序。

let option = Int( getopt( CommandLine.argc, CommandLine.unsafeArgv, buffer ) )

#1


6  

@Hamish already answered the question and explained how to pass CommandLine.unsafeArgv to getopt() in Swift (and why).

@Hamish已经回答了这个问题并解释了如何在Swift中将CommandLine.unsafeArgv传递给getopt()(以及为什么)。

Here is a complete self-contained example how a typical getopt loop can be implemented in Swift 3:

这是一个完整的自包含示例,如何在Swift 3中实现典型的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 ?? "?")

Remarks:

备注:

  • You can pass a Swift string (here: "abc:") directly to a C function expecting a (constant) C string, the compiler will automatically generate a temporary UTF-8 representation.
  • 您可以将Swift字符串(此处:“abc:”)直接传递给期望(常量)C字符串的C函数,编译器将自动生成临时UTF-8表示。
  • getopt() return either -1 (if the argument list is exhausted) or an unsigned char converted to an int. Therefore it is safe to convert the return value to CUnsignedChar (which is UInt8 in Swift).
  • getopt()返回-1(如果参数列表已用尽)或unsigned char转换为int。因此,将返回值转换为CUnsignedChar(在Swift中为UInt8)是安全的。
  • while is used (abused?) with pattern matching plus an additional boolean condition to implement the typical C pattern

    虽然使用(滥用?)模式匹配加上一个额外的布尔条件来实现典型的C模式

    while ((option = getopt(argc, argv, "abc:")) != -1) { ... }
    

    in Swift.

    在斯威夫特。

#2


5  

CommandLine.arguments gives you a friendly Swift [String] of the arguments passed – however you're looking to send the arguments straight back to C. Therefore you can simply use CommandLine.unsafeArgv instead, which will give you the actual raw value of argv passed to your program.

CommandLine.arguments为您提供了传递的参数的友好Swift [String] - 但是您希望将参数直接发送回C.因此您只需使用CommandLine.unsafeArgv,它将为您提供argv的实际原始值传递给你的程序。

let option = Int( getopt( CommandLine.argc, CommandLine.unsafeArgv, buffer ) )