swift-argument-parser icon indicating copy to clipboard operation
swift-argument-parser copied to clipboard

Support/Documentation for Compiling a Single Swift File Against the Argument Parser Package (Without Using SwiftPM for the Executable)

Open tree-frog opened this issue 1 year ago • 0 comments

I posted this topic on the Swift discussion forums a couple of weeks ago, but didn't get very far. I'm hoping that someone with deeper working knowledge of swift-argument-parser's internals could provide some guidance.

I'm having a terrible time figuring out how to compile a single .swift file using swiftc against a downloaded & locally-built copy of ArgumentParser, without using SPM for the executable.


For background on why I want to do this seemingly-strange thing:

I'm not a professional programmer, but rather an advanced AppleScript user, who's bumping his head against the ceiling of what is possible in ASObjC (namely: blocks, completion handlers).

For this, I have many small (1 - 20 line) compiled helper executables that I can run from AppleScript, which let me use the methods unavailable in ASObjC. I initially wrote these in Objective-C, but have transitioned to Swift. Right now, I am manually parsing the command-line arguments to these executables, but would really like to use swift-argument-parser.

These executables don't require any other 3rd-party packages, as they're only used to access macOS APIs. For several reasons, it's not currently feasible to have these executables be swift packages, nor is something like the excellent swift-sh the right solution.

I've done some extensive googling on the subject, but haven't found any workable solutions after many evenings spent on this. There's some mention of being able to bundle a SPM package into an XCFramework, but I wasn't able to get this to work either.


I've taken the following general steps:

  1. Download & build the ArgumentParser module.

    mkdir ~/Desktop/SPMDownload
    cd ~/Desktop/SPMDownload
    git clone https://github.com/apple/swift-argument-parser
    cd swift-argument-parser
    swift build -c release
    
  2. Import & use ArgumentParser in the desired source file, here ~/Desktop/example.swift:

    import ArgumentParser
    
    struct Greet: ParsableCommand {
        @Argument(help: "The thing to greet.")
        var name: String = "World"
    
        mutating func run() throws {
            print("Hello, \(name)!")
        }
    }
    
    Greet.main()
    
  3. Compile example.swift against ArgumentParser (not working):

    xcrun -sdk macosx swiftc -o ~/Desktop/example ~/Desktop/example.swift
    

    I've tried multiple command-line options here (both with & without trailing whitespace)... -I -L -l

    ...with various paths in the built ArgumentParser release folder located at...

    ~/Desktop/SPMDownload/swift-argument-parser/.build/release
    

    ...with no success.

What am I missing to be able to link example.swift against ArgumentParser?


It would be great for this feature to be documented somewhere (either here, or in the readme). It would also be great to know if this is supported vs. unsupported but possible vs. impossible. Thanks!

tree-frog avatar Jul 19 '24 18:07 tree-frog