I’m trying to build a command line tool that uses async/await. ArgumentParser supports this, by simply inheriting from AsyncParsableCommand
and making the run
method async.
Unfortunately, when I do that, it fails to parse the command line arguments, and displays the help text. If I change it to by synchronous, it parses things just fine.
Am I missing something (I don’t think I'm doing anything different from the sample code?
This works:
@main
struct
Raise3D : ParsableCommand
{
static
var
configuration = CommandConfiguration(commandName: "raise3d",
abstract: "A utility to interact witih Raise3D printers.",
subcommands: [])
@Option(help: "The printer’s local address.")
var addr : String
@Option(help: "The printer’s password.")
var password : String
mutating
func
run()
// async
throws
{
print("Monitoring printer at: \(self.addr)")
let api = Raise3DAPI(host: self.addr, password: self.password)
print("Signature: \(api.calculateSignature(time: Date()))")
}
}
Monitoring printer at: 192.168.2.136:10800
Signature: password=foobar×tamp=1713318296281
This does not (output below):
@main
struct
Raise3D : AsyncParsableCommand
{
static
var
configuration = CommandConfiguration(commandName: "raise3d",
abstract: "A utility to interact witih Raise3D printers.",
subcommands: [])
@Option(help: "The printer’s local address.")
var addr : String
@Option(help: "The printer’s password.")
var password : String
mutating
func
run()
async
throws
{
print("Monitoring printer at: \(self.addr)")
let api = Raise3DAPI(host: self.addr, password: self.password)
print("Signature: \(api.calculateSignature(time: Date()))")
}
}
OVERVIEW: A utility to interact witih Raise3D printers.
USAGE: raise3d --addr <addr> --password <password>
OPTIONS:
--addr <addr> The printer’s local address.
--password <password> The printer’s password.
-h, --help Show help information.