swift main 运行 / 从命令行构建

swift main run / build from command line

我将在 Swift 中构建一个 CLI 工具。我用这个命令创建了项目 swift package init --type executable

当我构建我的项目并解析 Xcode 中的 read-aliases 参数并单击“播放”按钮时,一切正常。

我定义了 read-aliases 参数

然后我收到正确的输出如下,来自我的 .zsh 文件

aliases
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 run read-aliases

解决方案

你必须在子命令

之前使用可执行产品

swift run easy-aliaser read-aliases

重现步骤

[so-test]$ git clone https://github.com/CreaTorAleXander/easy-aliaser
Cloning into 'easy-aliaser'...
remote: Enumerating objects: 41, done.
remote: Counting objects: 100% (41/41), done.
remote: Compressing objects: 100% (32/32), done.
remote: Total 41 (delta 2), reused 38 (delta 1), pack-reused 0
Unpacking objects: 100% (41/41), done.
[so-test]$ cd easy-aliaser 

#
# [easy-aliaser (main)]$ swift package generate-xcodeproj
# edited the wired filename in your code just to refer to an existing file
# run the project in XCode without problems  
# back to the command line 
#

[easy-aliaser (main)]$ swift run easy-aliaser read-aliases
Fetching https://github.com/apple/swift-argument-parser
Cloning https://github.com/apple/swift-argument-parser
Resolving https://github.com/apple/swift-argument-parser at 0.3.1
/Users/me/projects/so-test/easy-aliaser/Sources/easy-aliaser/main.swift:23:13: warning: result of call to 'readFile(path:)' is unused
            readFile(path: "/Users/me/projects/so-test/65203567/myzshrc")
            ^       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[3/3] Linking easy-aliaser
These are your Aliases
who_listening='sudo lsof -nP -iTCP -sTCP:LISTEN'
du-docker="du -h ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/Docker.qcow2 && docker images"
du-openshift=" /Users/ronda/.docker/machine/machines/openshift/disk.vmdk && du -h ~/.docker/machine/machines/openshift/boot2docker.iso"