【问题标题】:swift main run / build from command line从命令行快速主运行/构建
【发布时间】:2020-12-08 17:09:24
【问题描述】:

我将在 Swift 中构建一个 CLI 工具。我已经用这个命令swift package init --type executable

创建了项目

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

我定义了 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


    【解决方案1】:

    问题

    命令行指定子命令但错过可执行产品

    $ 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"
    

    【讨论】:

    • 哇,太棒了,它是如此简单。 :) 编辑有线文件名是什么意思?
    • @Alex "/Users/alexanderhess/.zshrc" 仅在用户名是“alexanderhess”时才有效,所以我说这是 *wired ;-) ...取消连线它可以使用类似FileManager.default.homeDirectoryForCurrentUser 或查看this answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多