swift-argument-parser icon indicating copy to clipboard operation
swift-argument-parser copied to clipboard

Add AsyncParsableArguments to support validating async arguments

Open luispadron opened this issue 10 months ago • 4 comments

Currently to share arguments across commands you can create a ParsableArguments type which contains your arguments/options, and can use func validate() throws to validate the arguments.

This fails for things that must be validated in an async manner. We should consider adding an async API for validating or AsyncParsableArguments to support this.

luispadron avatar Apr 09 '24 15:04 luispadron

I just encountered the same problem.

CrazyFanFan avatar Apr 09 '24 15:04 CrazyFanFan

Also just hit this.. thought I might've been holding it wrong. I can try and take a look

dcantah avatar Apr 11 '24 18:04 dcantah

Given the headaches and challenges around supporting the sync and async versions of run(), I'm reticent to provide another overload here. As a workaround, you can perform any async validation at the start of your async run() method – if you throw a ValidationError from there you'll get the same output as an error thrown from the validate() method.

If this workaround doesn't work for you, please let me know!

natecook1000 avatar May 20 '24 14:05 natecook1000

Yeah that works and it's what we do today but it lacks a few things:

  • the argument cannot have the correct type, because the async function is not supported. E.g we need to declare the argument as String for example and then validate it converts to whatever type we wanted.
  • this conversion needs to happen in all the commands which use that argument.

luispadron avatar May 20 '24 14:05 luispadron