swift-argument-parser Swift类型安全参数解析库
Swift 的参数解析器swift-argument-parser挺好用的,尤其是你要搞命令行工具的时候。它是苹果官方出的,类型安全,还支持属性包装器,写起来蛮顺。声明一个结构体,加几个注解,逻辑直接写在run()里,清晰得。
命令行的参数配置,像@Option、@Flag、@Argument这些,都靠属性包装器搞定,不用你手动解析字符串,省心。比如:
import ArgumentParser
struct Repeat: ParsableCommand {
  @Flag(help: "Include a counter with each repetition.")
  var includeCounter = false
@Option(name: .shortAndLong, help: "The number of times to repeat 'phrase'.")
  var count: Int?
@Argument(help: "The phrase to repeat.")
  var phrase: String
func run() throws {
    let times = count ?? 1
    for i in 0..
嗯,结构也直观,代码逻辑跟参数配置分得挺清楚。配合swift run或者打包后的命令行工具用都行,开发体验还不错。
如果你以前用过Python argparse或者Java CLI 工具包,用这个就会觉得简洁多。而且跟 Swift 的语言风格贴合,没那么多奇怪的 API。
如果你平时在写Swift CLI工具,或者想把脚本项目做得更专业点,swift-argument-parser挺值得一试的。
                                        
                                    文件大小:206.17KB
                                
                                
                                
                            
评论区