`AsyncParsableCommand` doesn't work on `main.swift`

Note: this is all assuming the Package.swift contains the proper minimum platform versions.

If I try and make a root AsyncParseableCommand in a main.swift of an executable target like so:

import ArgumentParser

struct MyCommand: AsyncParsableCommand {
  func run() async {
    print("Hello, World!")
  }
}
MyCommand.main()

the resulting binary will print the help text instead of running the command. If I use @main in a main.swift, the Swift compiler complains that I cannot use @main in a file that contains top-level code. If, instead, I make another file (let's say foo.swift) like so:

import ArgumentParser

@main
struct MyCommand: AsyncParsableCommand {
  func run() async {
    print("Hello, World!")
  }
}

the binary will run the command as expected.

Is this a known issue?

Well, if you want to call AsyncParsableCommand.main directly it should be inferred like this

extension AsyncParsableCommand {


     static func main() async {}
}

so in your code you have to call like

await MyCommand.main(nil)

But it looks like you're calling this.
Which is actually not support for AsyncParsableCommand.

1 Like