@@ -25,6 +25,35 @@ public protocol AsyncParsableCommand: ParsableCommand {
2525
2626@available ( macOS 10 . 15 , macCatalyst 13 , iOS 13 , tvOS 13 , watchOS 6 , * )
2727extension AsyncParsableCommand {
28+ /// Parses a new instance of this type from command-line arguments.
29+ ///
30+ /// - Parameter arguments: An array of arguments to use for parsing. If
31+ /// `arguments` is `nil`, this uses the program's command-line arguments.
32+ /// - Returns: A new instance of this type.
33+ /// - Throws: If parsing failed or arguments contains a help request.
34+ public static func parse(
35+ _ arguments: [ String ] ? = nil
36+ ) async throws -> Self {
37+ // Parse the command and unwrap the result if necessary.
38+ try parse ( try await parseAsRoot ( arguments) )
39+ }
40+
41+ /// Parses an instance of this type, or one of its subcommands, from
42+ /// command-line arguments.
43+ ///
44+ /// - Parameter arguments: An array of arguments to use for parsing. If
45+ /// `arguments` is `nil`, this uses the program's command-line arguments.
46+ /// - Returns: A new instance of this type, one of its subcommands, or a
47+ /// command type internal to the `ArgumentParser` library.
48+ /// - Throws: If parsing fails.
49+ public static func parseAsRoot(
50+ _ arguments: [ String ] ? = nil
51+ ) async throws -> ParsableCommand {
52+ var parser = CommandParser ( self )
53+ let arguments = arguments ?? Array ( CommandLine . _staticArguments. dropFirst ( ) )
54+ return try await parser. parse ( arguments: arguments) . get ( )
55+ }
56+
2857 /// Executes this command, or one of its subcommands, with the given arguments.
2958 ///
3059 /// This method parses an instance of this type, one of its subcommands, or
@@ -36,7 +65,7 @@ extension AsyncParsableCommand {
3665 /// `arguments` is `nil`, this uses the program's command-line arguments.
3766 public static func main( _ arguments: [ String ] ? ) async {
3867 do {
39- var command = try parseAsRoot ( arguments)
68+ var command = try await parseAsRoot ( arguments)
4069 if var asyncCommand = command as? AsyncParsableCommand {
4170 try await asyncCommand. run ( )
4271 } else {
0 commit comments