Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CommandLine.arguments and Processinfo.processinfo.arguments in swift

I wanna know how these are different CommandLine ProcessInfo

let elements = CommandLine.arguments
let elements = Processinfo.processinfo.arguments

In my thinking, ProcessInfo’s arguments have all of the Commandline's concept. So there is no differences for dealing with argument.

The code below, using CommandLine.arguments, is for practicing read and write file.

If I put Processinfo.processinfo.arguments at location of CommanLine.arguments. Nothing changed.

static func makeInOutFile() -> (inputFile: String, outputFile: String)? {
    let elements = CommandLine.arguments
    let inputFile: String
    let outputFile: String
    switch elements.count {
    case 2:
        inputFile = elements[1]
        outputFile = Message.ofDefaultJSONFileName.description
        return (inputFile: inputFile, outputFile: outputFile)
    case 3:
        inputFile = elements[1]
        outputFile = elements[2]
        return (inputFile: inputFile, outputFile: outputFile)
    default:
        print (Message.ofFailedProcessingFile)
        return nil
    }
}
like image 774
Jung Hoon Choi Avatar asked Jan 11 '18 07:01

Jung Hoon Choi


1 Answers

CommandLine is part of the Swift standard library and only provides the command line arguments and the argument count.

ProcessInfo is part of the Foundation framework (not part of the language). While ProcessInfo.arguments does gives you the same results as CommandLine.arguments, there is a lot more to the ProcessInfo class.

While the two arguments are functionally the same, if all you want is the command line arguments, use CommandLine. It's simpler, it doesn't rely on any additional frameworks, and it would be more portable to other Swift runtimes.

like image 66
rmaddy Avatar answered Sep 30 '22 15:09

rmaddy