如何在Swift中访问命令行参数? [英] How do you access command line arguments in Swift?

查看:128
本文介绍了如何在Swift中访问命令行参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Swift中访问命令行应用程序的命令行参数?

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

推荐答案

Apple为此发布了ArgumentParser库:

Apple has released the ArgumentParser library for doing just this:

我们很高兴地宣布ArgumentParser,这是一个新的开源库,使它变得简单明了-甚至令人愉快! —在Swift中解析命令行参数.

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/


快速参数解析器

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


Swift Argument Parser

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

首先声明一个定义您需要从命令行收集的信息的类型.用ArgumentParser的属性包装之一装饰每个存储的属性,并声明符合ParsableCommand.

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.

ArgumentParser库将解析命令行参数,实例化您的命令类型,然后执行您的自定义run()方法或退出并显示一条有用的消息.

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.

这篇关于如何在Swift中访问命令行参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆