|
10 | 10 | //===----------------------------------------------------------------------===//
|
11 | 11 |
|
12 | 12 | /// A type that can be executed as part of a nested tree of commands.
|
13 |
| -public protocol AsyncParsableCommand: ParsableArguments { |
14 |
| - /// Runs this command. |
15 |
| - /// |
16 |
| - /// After implementing this method, you can run your command-line |
17 |
| - /// application by calling the static `main()` method on the root type. |
18 |
| - /// This method has a default implementation that prints help text |
19 |
| - /// for this command. |
| 13 | +public protocol AsyncParsableCommand: ParsableCommand { |
20 | 14 | mutating func run() async throws
|
21 | 15 | }
|
22 | 16 |
|
23 | 17 | extension AsyncParsableCommand {
|
24 |
| - public mutating func run() async throws { |
25 |
| - throw CleanExit.helpRequest(Self.asCommand) |
| 18 | + public mutating func run() throws { |
| 19 | + throw CleanExit.helpRequest(self) |
26 | 20 | }
|
27 | 21 | }
|
28 | 22 |
|
29 |
| -// MARK: - API |
30 |
| - |
31 |
| -extension AsyncParsableCommand { |
32 |
| - /// Parses an instance of this type, or one of its subcommands, from |
33 |
| - /// command-line arguments. |
34 |
| - /// |
35 |
| - /// - Parameter arguments: An array of arguments to use for parsing. If |
36 |
| - /// `arguments` is `nil`, this uses the program's command-line arguments. |
37 |
| - /// - Returns: A new instance of this type, one of its subcommands, or a |
38 |
| - /// command type internal to the `ArgumentParser` library. |
39 |
| - public static func parseAsRoot( |
40 |
| - _ arguments: [String]? = nil |
41 |
| - ) throws -> AsyncParsableCommand { |
42 |
| - var parser = CommandParser(self.asCommand) |
43 |
| - let arguments = arguments ?? Array(CommandLine.arguments.dropFirst()) |
44 |
| - return try parser.parse(arguments: arguments).get() as! AsyncParsableCommand |
45 |
| - } |
46 |
| - |
47 |
| - /// Returns the text of the help screen for the given subcommand of this |
48 |
| - /// command. |
49 |
| - /// |
50 |
| - /// - Parameters: |
51 |
| - /// - subcommand: The subcommand to generate the help screen for. |
52 |
| - /// `subcommand` must be declared in the subcommand tree of this |
53 |
| - /// command. |
54 |
| - /// - columns: The column width to use when wrapping long line in the |
55 |
| - /// help screen. If `columns` is `nil`, uses the current terminal |
56 |
| - /// width, or a default value of `80` if the terminal width is not |
57 |
| - /// available. |
58 |
| - public static func helpMessage( |
59 |
| - for subcommand: ParsableCommand.Type, |
60 |
| - columns: Int? = nil |
61 |
| - ) -> String { |
62 |
| - let stack = CommandParser(self.asCommand).commandStack(for: subcommand) |
63 |
| - return HelpGenerator(commandStack: stack).rendered(screenWidth: columns) |
64 |
| - } |
| 23 | +public protocol AsyncMain { |
| 24 | + associatedtype Command: ParsableCommand |
| 25 | +} |
65 | 26 |
|
66 |
| - /// Parses an instance of this type, or one of its subcommands, from |
67 |
| - /// the given arguments and calls its `run()` method, exiting with a |
68 |
| - /// relevant error message if necessary. |
69 |
| - /// |
70 |
| - /// - Parameter arguments: An array of arguments to use for parsing. If |
71 |
| - /// `arguments` is `nil`, this uses the program's command-line arguments. |
72 |
| - public static func main(_ arguments: [String]?) async { |
| 27 | +extension AsyncMain { |
| 28 | + public static func main() async { |
73 | 29 | do {
|
74 |
| - var command = try parseAsRoot(arguments) |
75 |
| - try await command.run() |
| 30 | + var command = try Command.parseAsRoot() |
| 31 | + if var command = command as? AsyncParsableCommand { |
| 32 | + try await command.run() |
| 33 | + } else { |
| 34 | + try command.run() |
| 35 | + } |
76 | 36 | } catch {
|
77 |
| - exit(withError: error) |
| 37 | + Command.exit(withError: error) |
78 | 38 | }
|
79 | 39 | }
|
80 |
| - |
81 |
| - /// Parses an instance of this type, or one of its subcommands, from |
82 |
| - /// command-line arguments and calls its `run()` method, exiting with a |
83 |
| - /// relevant error message if necessary. |
84 |
| - public static func main() async { |
85 |
| - await self.main(nil) |
86 |
| - } |
87 | 40 | }
|
0 commit comments