我正在使用ArgumentParser
包进行命令行解析,并希望将其与Swift并发性的async
API一起使用:
struct Foo: ParsableCommand {
@Argument(
help: "File to be parsed. If not present, parses stdin.",
transform: URL.init(fileURLWithPath:)
)
var file: URL?
mutating func run() async throws {
let handle: FileHandle
if let file {
handle = try .init(forReadingFrom: file)
} else {
handle = .standardInput
}
for try await line in handle.bytes.lines {
// do something with each line
}
try handle.close()
}
}
但是当我这样做的时候,我总是看到"用法"文本:
USAGE: foo [<file>]
ARGUMENTS:
<file> File to be parsed. If not present, parses stdin.
OPTIONS:
-h, --help Show help information.
我没有收到任何编译错误,但无论我是否提供参数,我都会看到"Usage"文本.