Skip to content

Permit asynchronous code to be given to the custom completion typeΒ #555

@thafner0

Description

@thafner0

At the moment the custom case of the CompletionKind enumeration supports being given a custom closure to generate completions on the command line. However, since the provided closure must be synchronous, it is difficult to incorporate functionality that requires swift's concurrency features. The lack of this functionality prevents certain tasks from being possible with these completions and causes other tasks to be possible, but only in a non-thread safe way. Examples of this include working with databases (such as those managed by Apple's Core Data framework).

Example:

struct SomeCommand: AsyncParseableCommand {

@Argument(help: "<Helpful help>", completion: .custom({ arguments in
return try? await completionGenerator(arguments: arguments) ?? []
}))
var argument: String

mutating func run() async throws {
// Code that makes the command Run
}
}

@MainActor
private func completionGenerator(arguments: [String]) async throws -> [String] {
// Code that generates completions that has some asynchronous system in use and/or must be run on the main actor.
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions