apple / apple/swift-argument-parser
Support/Documentation for Compiling a Single Swift File Against the Argument Parser Package (Without Using SwiftPM for the Executable)
- Dominant language
- Swift
- Stars
- 3.8k
- Forks
- 411
- Avg merge
- 7d 13h
- Merged PRs (30d)
- 17
Description
I posted this topic on the [Swift discussion forums](https://forums.swift.org/t/compile-a-single-swift-file-against-the-argument-parser-package-without-using-swiftpm-for-the-executable/73012) 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](https://github.com/mxcl/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](https://forums.swift.org/t/how-to-build-swift-package-as-xcframework/41414), but I wasn't able to get this to work either.
***
I've taken the following general steps:
1. Download & build the ArgumentParser module.
```bash
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**):
```bash
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...
```text
~/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!
Contributor guide
Research direction
Start by reproducing the documented swift build and swiftc commands using example.swift and the .build/release directory. Read the repository README and package build configuration to determine whether compiling a standalone executable this way is supported. Done means documenting the working command or clearly stating the limitation and supported alternatives.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- cli, documentation
- Issue type
- Documentation
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100