Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing arguments to async main #568

Merged
merged 4 commits into from
May 17, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 24 additions & 6 deletions Sources/ArgumentParser/Parsable Types/AsyncParsableCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ public protocol AsyncParsableCommand: ParsableCommand {

@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
extension AsyncParsableCommand {
/// Executes this command, or one of its subcommands, with the program's
/// command-line arguments.
/// Executes this command, or one of its subcommands, with the given arguments.
///
/// Instead of calling this method directly, you can add `@main` to the root
/// command for your command-line tool.
public static func main() async {
/// This method parses an instance of this type, one of its subcommands, or
/// another built-in `AsyncParsableCommand` type, from command-line
/// (or provided) arguments, and then calls its `run()` method, exiting
/// with a relevant error message if necessary.
///
/// - Parameter arguments: An array of arguments to use for parsing. If
/// `arguments` is `nil`, this uses the program's command-line arguments.
public static func main(_ arguments: [String]?) async {
do {
var command = try parseAsRoot()
var command = try parseAsRoot(arguments)
if var asyncCommand = command as? AsyncParsableCommand {
try await asyncCommand.run()
} else {
Expand All @@ -42,6 +46,20 @@ extension AsyncParsableCommand {
exit(withError: error)
}
}

/// Executes this command, or one of its subcommands, with the program's
/// command-line arguments.
///
/// Instead of calling this method directly, you can add `@main` to the root
/// command for your command-line tool.
///
/// This method parses an instance of this type, one of its subcommands, or
/// another built-in `AsyncParsableCommand` type, from command-line arguments,
/// and then calls its `run()` method, exiting with a relevant error message
/// if necessary.
public static func main() async {
await self.main(nil)
}
}

/// A type that can designate an `AsyncParsableCommand` as the program's
Expand Down