How do you access command line arguments in Swift?

前端 未结 6 1754
野趣味
野趣味 2020-12-04 07:02

How do you access command line arguments for a command line application in Swift?

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 07:22

    Apple has released the ArgumentParser library for doing just this:

    We’re delighted to announce ArgumentParser, a new open-source library that makes it straightforward — even enjoyable! — to parse command-line arguments in Swift.

    https://swift.org/blog/argument-parser/


    Swift Argument Parser

    https://github.com/apple/swift-argument-parser

    Begin by declaring a type that defines the information you need to collect from the command line. Decorate each stored property with one of ArgumentParser's property wrappers, and declare conformance to ParsableCommand.

    The ArgumentParser library parses the command-line arguments, instantiates your command type, and then either executes your custom run() method or exits with useful a message.

提交回复
热议问题