Execute async/await method from Swift Command Line (CLI) Application

If you are writing a Swift CLI application and want to use asynchronous tasks, then you can use a special Swift attribute called @main. This is the inbuilt way to handle asynchronous operations and also prevents the Command Line Application/Tool from exiting before the asynchronous operation completes. Here's how you do it:

1. Rename main.swift to AppName.swift

First of all, you need to rename the main.swift file to AppName.swift. For example, if your app name is AppBoxCLI then rename main.swift to AppBoxCLI.swift.

2. Create a struct with the @main attribute

Now create a new AppName struct in AppName.swift file with the @main attribute. For example:

struct AppBoxCLI {

3. Add the main function with async

Now in the AppName struct, you need to add a main function with async. You can also add throws if you need to handle errors here. For example:

struct AppBoxCLI {
  static func main() async throws {
    let appBoxCore = AppBoxCore.init()
    try await appBoxCore.upload()


Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.