I’m trying to create a small text-processing Swift program. To launch it, I’m planning to use a Shell script, but currently I’m just using Terminal. I need to pass a launch argument to it, but everything I’ve tried so far for it to work has failed.
An answer provided by an LLM was “write your argument after –, then it will be passed to an executable itself” OK:
$ swift run -- "abc.txt"
error: no executable product named 'abc.txt'
So maybe I should specify the exact package path?
swift run --package-path=. -- "abc.txt"
error: no executable product named 'abc.txt'
Maybe the AI answer is not that helpful here, so how about the reference? Documentation
–run
Launch the executable with the provided arguments.
$ swift run --package-path=. --run "abc.txt"
error: no executable product named 'abc.txt'
The same if omitting the package-path option here.
So for now I don’t understand what I’m doing wrong. Please help me find what is my mistake.
Sidenote: swift run doesn't use the "dash-dash" convention to separate/group arguments. When you include -- in your arguments after the product/target name, like in @bbrk24's example:
swift run MyExecutableTarget -- abc.txt
your program will receive two arguments, "--" and "abc.txt", not just one.
Addition after further testing and reading: SwiftPM does use the -- convention to separate flags/options from positional/unnamed arguments. This can be useful e.g. if your executable name starts with a dash/hyphen for some reason: swift run -- --my-executable works, whereas swift run --my-executable will fail with "unknown option '--my-executable'`.
The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the - character.
I thought that there was another convention: to use -- as a separator for splitting arguments into those passed to the called program itself (swift in this case) and those that should be passed to a secondary program (MyExecutable). But I can't find evidence that this is in fact a widely used convention.