apple / swift-argument-parser

Straightforward, type-safe argument parsing for Swift
Apache License 2.0
3.34k stars 321 forks source link

AsyncParsableCommand never runs. Only shows help text. #662

Open myleshyson opened 1 month ago

myleshyson commented 1 month ago

I'm using Swift 6.0, and Im trying to use adhere to to the AsyncParsableCommand protocol. However I'm noticing that no matter what, my program always returns the help text instead of actually running.

There's post here describing this issue as well. Seems like the compiler favors the sync run command over the async run command.

Here's my main file. Just trying to print a string right now.

import ArgumentParser

@main
struct PhpFlayer: AsyncParsableCommand {
    mutating func run() async throws {
        print("I'm working!")
    }
}

ArgumentParser version: main branch Swift version: swift-driver version: 1.113 Apple Swift version 6.0 (swiftlang-6.0.0.7.6 clang-1600.0.24.1) Target: arm64-apple-macosx14.0.

Checklist

Steps to Reproduce

  1. Create an empty project
    • mkdir MyCLI && cd MyCLI
    • swift package init --name MyCLI --type executable
  2. Setup the entry file and add the argument parser package

    • Add the argument parser package to Package.swift
    • rename Sources/main.swift to Sources/MyCLI.swift
    • setup the following main struct
      
      import ArgumentParser

    @main struct MyCLI: AsyncParsableCommand { mutating func run() async throws { print("I'm working!") } }

  3. Run the program and see only help text show up instead of "I'm Working!"

Expected behavior

I'd expect when running swift run MyCLI that i would see "I'm working!" in the terminal.

Actual behavior

Right now this always displays the help text.

sgade commented 1 month ago

Same with Swift 5.10. Using ParsableCommand works just fine.

rgoldberg commented 3 weeks ago

Have you tried removing mutating?

myleshyson commented 3 weeks ago

Yea. Same result unfortunately.

rgoldberg commented 3 weeks ago

My code structured like the following works when running command sub. It uses Swift Argument Parser 1.5.0 on Swift 5.7.1 targeting macOS 10.15+. I do not have mutating anywhere in my code:

import ArgumentParser

@main
struct Command: AsyncParsableCommand {
    static let configuration = CommandConfiguration(
        abstract: "…",
        subcommands: [
            Sub.self
        ]
    )
}

extension Command {
    struct Sub: AsyncParsableCommand {
        func run() async throws {
            try await …
        }
    }
}