How to use Getopt with command line parameters in SWIFT 3?

I tried to use getopt with command line parameters in Swift 3. I started with Michele Dall’Agata’s nice stackoverflow contribution:

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 receive this error:

< /p>

Cannot convert value of type'[String]’ to expected argument type
‘UnsafePointer?>!’

For CommandLine .arguments, I tried to use it as argv. Does anyone know the correct syntax for the second argument of getopt? Thanks in advance!

@Hamish has already answered this question and explained how to pass CommandLine.unsafeArgv to getopt() (and why).

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

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":< br /> bFlag = true
case "c":
cValue = String(cString: optarg)
default:
fatalError("Unknown option")
}< br />}

print(aFlag, bFlag, cValue ?? "?")

Remarks:

>You can add Swift string (this Place: “abc:”) directly passed to C
The function expects a (constant) C string, and the compiler will automatically execute
to generate a temporary UTF-8 representation.
> getopt() returns -1( If the parameter list is exhausted) or unsigned char is converted to int. So it is safe to convert the return value to CUnsignedChar (UInt8 in Swift).
>Use (abuse?) to match the pattern plus one The additional
Boolean conditions implement the typical C pattern

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

In Swift.

I tried Swift 3 uses getopt with command line parameters. I started with Michele Dall’Agata’s nice stackoverflow contribution:

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?>!’

For CommandLine.arguments, I am trying to use it as argv. Does anyone Do you know the correct syntax for the second parameter of getopt? Thanks in advance!

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

< /p>

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

var aFlag = false
var bFlag = false
var cValue: String?

while case let option = getopt(CommandLine.argc, CommandLine.unsafeArgv, "abc:"), option != -1 {< br /> 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 the Swift string (here: “abc:”) directly to C< br>The function expects a (constant) C string, and the compiler will automatically execute
to generate a temporary UTF-8 representation.
> getopt() returns -1 (if the parameter list is exhausted) or unsigned char is converted to int. So it is safe
The return value is converted to CUnsignedChar (UInt8 in Swift).
>Use (abuse?) to match the pattern plus an extra
Boolean condition to implement a typical C pattern

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

In Sri Lanka Wift.

Leave a Comment

Your email address will not be published.