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

Fix AsyncParseableCommand hierarchy #436

Merged
merged 9 commits into from Jun 13, 2022
38 changes: 37 additions & 1 deletion Sources/ArgumentParser/Parsable Types/AsyncParsableCommand.swift
Expand Up @@ -31,6 +31,10 @@ extension AsyncParsableCommand {
/// Instead of calling this method directly, you can add `@main` to the root
/// command for your command-line tool.
public static func main() async {
#if DEBUG
configuration.subcommands.forEach(checkAsycHierarchy)
#endif

KeithBird marked this conversation as resolved.
Show resolved Hide resolved
do {
var command = try parseAsRoot()
if var asyncCommand = command as? AsyncParsableCommand {
Expand All @@ -42,6 +46,21 @@ extension AsyncParsableCommand {
exit(withError: error)
}
}

#if DEBUG
@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
internal static func checkAsycHierarchy(_ command: ParsableCommand.Type) {
command.configuration.subcommands.forEach(checkAsycHierarchy)

if command is AsyncParsableCommand.Type { return }

for sub in command.configuration.subcommands {
guard !(sub is AsyncParsableCommand.Type) else {
fatalError("`\(sub)` subcommand can't run asynchronously in `\(command)` command")
}
}
}
#endif
}

/// A type that can designate an `AsyncParsableCommand` as the program's
Expand All @@ -61,6 +80,10 @@ extension AsyncMainProtocol {
/// Executes the designated command type, or one of its subcommands, with
/// the program's command-line arguments.
public static func main() async {
#if DEBUG
Command.configuration.subcommands.forEach(checkAsycHierarchy)
#endif

do {
var command = try Command.parseAsRoot()
if var asyncCommand = command as? AsyncParsableCommand {
Expand All @@ -72,5 +95,18 @@ extension AsyncMainProtocol {
Command.exit(withError: error)
}
}

#if DEBUG
internal static func checkAsycHierarchy(_ command: ParsableCommand.Type) {
command.configuration.subcommands.forEach(checkAsycHierarchy)

if command is AsyncParsableCommand.Type { return }

for sub in command.configuration.subcommands {
guard !(sub is AsyncParsableCommand.Type) else {
fatalError("`\(sub)` subcommand can't run asynchronously in `\(command)` command")
}
}
}
#endif
}

24 changes: 23 additions & 1 deletion Sources/ArgumentParser/Parsable Types/ParsableCommand.swift
Expand Up @@ -84,7 +84,7 @@ extension ParsableCommand {
public static func helpMessage(
for subcommand: ParsableCommand.Type,
columns: Int? = nil
) -> String {
) -> String {
helpMessage(for: subcommand, includeHidden: false, columns: columns)
}

Expand Down Expand Up @@ -124,6 +124,13 @@ extension ParsableCommand {
/// - 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]?) {

#if DEBUG
if #available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *) {
checkAsycHierarchy(self)
}
#endif

do {
var command = try parseAsRoot(arguments)
try command.run()
Expand Down Expand Up @@ -164,4 +171,19 @@ extension ParsableCommand {
internal static var defaultIncludesUnconditionalArguments: Bool {
configuration.defaultSubcommand?.includesUnconditionalArguments == true
}

#if DEBUG
@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
internal static func checkAsycHierarchy(_ command: ParsableCommand.Type) {
KeithBird marked this conversation as resolved.
Show resolved Hide resolved
command.configuration.subcommands.forEach(checkAsycHierarchy)

if command is AsyncParsableCommand.Type { return }

for sub in command.configuration.subcommands {
guard !(sub is AsyncParsableCommand.Type) else {
fatalError("`\(sub)` subcommand can't run asynchronously in `\(command)` command")
KeithBird marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
#endif
}