【发布时间】:2020-12-08 17:09:24
【问题描述】:
我将在 Swift 中构建一个 CLI 工具。我已经用这个命令swift package init --type executable
当我构建我的项目并在 Xcode 中解析 read-aliases 参数并单击“播放”按钮时,一切正常。
然后我收到正确的输出,它是以下 aliases 来自我的 .zsh 文件
These are your Aliases
zshconfig="mate ~/.zshrc"
ohmyzsh="mate ~/.oh-my-zsh"
python="/usr/local/bin/python3.7"
python2="/usr/bin/python2"
Program ended with exit code: 0
但是当我在命令行的项目中运行以下命令 swift run 时
到目前为止,这些似乎有效,这些是来自我的工具的消息。
当我像这样解析相同的参数时
$ swift run read-aliases
我收到了这个错误
error: no executable product named 'read-aliases'
这是我的代码
import Foundation
import ArgumentParser
struct Alias: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Make Editing Your .zshrc Much Easier",
subcommands: [readAliases.self])
}
extension Alias {
struct readAliases: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Reads All The Aliases In Your .zshrc File")
func run() {
print("These are your Aliases");
readFile(path: "/Users/alexanderhess/.zshrc")
}
func readFile(path: String) -> Int {
errno = 0
if freopen(path, "r", stdin) == nil {
perror(path)
return 1
}
while let line = readLine() {
if(line.starts(with: "# alias")){
print(line.dropFirst(8));
}
}
return 0
}
}
}
Alias.main();
我的Package.swift
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "easy-aliaser",
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.2.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "easy-aliaser",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser")
]),
.testTarget(
name: "easy-aliaserTests",
dependencies: ["easy-aliaser"]),
]
)
这是我的github repository,如果你想复制它。
那么为什么我在命令行中收到此错误,而在 Xcode 中却没有?
提前致谢。
【问题讨论】:
标签: swift import command-line compiler-errors