I'm trying to create a command line tool, so obviously want to use ArgumentParser. I've added the Package to my project, (as far as I can tell), and initially, when I build, I get these errors:
dyld[20971]: Library not loaded: @rpath/ArgumentParser.framework/Versions/A/ArgumentParser
Referenced from: <5CBCAC0B-63B2-3F9B-AAD2-D77C82D3BD1F> /Users/ben/Library/Developer/Xcode/DerivedData/pdfutil-cpoebwvnlxwjfxgnkrbkoskgcfhz/Build/Products/Debug/pdfutil
Reason: tried: '/System/Volumes/Preboot/Cryptexes/OS@rpath/ArgumentParser.framework/Versions/A/ArgumentParser' (no such file), '/Library/Frameworks/ArgumentParser.framework/Versions/A/ArgumentParser' (no such file), '/System/Library/Frameworks/ArgumentParser.framework/Versions/A/ArgumentParser' (no such file, not in dyld cache)
If I set the Build Options to Disable Library Validation, then the tool runs in Xcode, but the compiled executable still gives the same errors in Terminal.
I've modified very few build settings from the defaults. The project is signed locally.
Am I missing some other build setting, or something else? Thanks for any help.
Have you tried developing the command line tool as a SwiftPM package, with a standalone Package.swift? I don't think these errors are reproducible with a package with an executable target created with swift init --type executabe and build with swift build. I assume you're currently developing it with an .xcodeproj and targets declared within it?
That's not what I mean, you can still open Package.swift with Xcode, which will open not just that manifest file, but the whole package for development, building, and testing. Diagnosing issues with your Package.swift is much easier though, as you'll be able to just paste a snippet here if something goes wrong, and you won't need to manage build settings separately.
Sorry, then I don't know what you mean. My tool itself should be a Package? Or I should create my own 'standalone' version of the ArgumentParser package?
My project is very basic -- I've got a main.swift file, and this package. That's it.
Navigate to an empty directory, then run swift package init --type executable, and xed Package.swift after that. Add ArgumentParser dependency in Package.swift. After package resolution has completed successfully, you can paste your code into a file with @main declaration of this new package to replace "Hello, World!" sample code.
I get an error "Source files for target ArgumentParser should be located under 'Sources/ArgumentParser', or a custom sources path can be set with the 'path' property in Package.swift"
I've got no target, and File > New doesn't offer Target as a choice.
Surely, creating a new Command Line tool in Xcode and Adding the Package should ... 'just work'?
I tried @Max_Desiatov 's command line method again. But it doesn't actually add the ArgumentParser package into the project it creates. And if I try to Add the Package from the Package Manager, it won't let me.
If I try to copy over the Packages.swift file or Sources > ArgumentParser from my original project, the build icon gets crossed out and doesn't work. Not sure I know how "package resolution" is supposed to "take place".
It won't be added automatically. It's up to the developer to add dependencies they want to use in the package. You'll have to edit your Package.swift as described in ArgumentParser's README.md, replacing <command-line-tool> with the name of your executable target.
Try building your package with swift build in the terminal. In case there are any errors in package resolution they may be more discoverable in the command line.
Source files for target ArgumentParser should be located under 'Sources/ArgumentParser', or a custom sources path can be set with the 'path' property in Package.swift
Thanks for providing these details! What you're sharing is Package.swift for the ArgumentParser package. You don't have to copy it anywhere and it doesn't belong to your project. You have to use Package.swift generated by swift package init --type executable and edit that by adding only two lines:
Thanks for your help. This is probably basic and obvious if you're in front of it.
OK. So I now have two projects.
My original one, created in Xcode, for which the Package Manager doesn't create even a boilerplate Package.swift file; but somehow the project builds anyway and references the package when it executes in Xcode, but not outside of it.
A second one, following your instructions, which seems to work with the Hello Word code, but I can't find the target or the Build Options anywhere. When I add my own code, starting thus:
@main
import Foundation
import ArgumentParser
import Quartz
struct pdfutil: ParsableCommand {
static var configuration = CommandConfiguration(
abstract: "A PDF manipulation Utility",
version: "1.0",
subcommands: [count.self, addPage.self, rotate.self, makePNG.self, join.self]
// defaultSubcommand: rotate.self
)
}
I get "@main' attribute cannot be applied to this declaration".
If I paste the Package.swift file from the 2nd project into the first, I get: "No such module "PackageDescription".
The @main attribute is applied to a type, not an import statement, like in your example. Thus, it should be on the line immediately preceding struct pdfutil instead of the top of the file.