iOS命令行工具开发

2022-01-14  本文已影响0人  我阿郑

命令行工具的本质: 命令行工具本质也是一个可执行文件,跟App差不多,只不过App是由:可执行文件 + 资源构成的

一、仿照喵神的FengNiao实现命令行工具

FengNiao 是喵大创建并开源的一款 Mac 命令行工具, 在 GitHub 创建项目命名为 FengNiaoCopy ,接着最重要的是添加 .gitignore 文件

1-Swift Package Manager

Swift Package Manager 是 Swift 使用的一个包管理器,和 CocoaPods、Carthage 类似,都是可以用来做依赖管理的。

# 查看本地上的 Swift Package Manager 版本号
swift package --version 

cd FengNiaoCopy
# 创建命令行项目
swift package init --type executable

# 生成 `xcodeproj` 文件,不然 Xcode 无法打开该项目
 swift package generate-xcodeproj 

通过Xcode打开 FengNiaoCopy.xcodeproj

image.png

接着需要修改 Package.swift 文件,添加依赖

let package = Package(
    name: "FengNiaoCopy",
    platforms: [
        .macOS(.v10_12)
      ],
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/onevcat/Rainbow.git", from: "4.0.1"),
        .package(name: "CommandLineKit", url: "https://github.com/objecthub/swift-commandlinekit.git", from: "0.3.4"),
        .package(url: "https://github.com/kylef/PathKit.git", from:"1.0.1"),
    ],
    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: "FengNiaoKitCopy",
            dependencies: []),
        .target(
            name: "FengNiaoCopy",
            dependencies: ["FengNiaoKitCopy","Rainbow","PathKit", "CommandLineKit"]),
        .testTarget(
            name: "FengNiaoCopyTests",
            dependencies: ["FengNiaoCopy"]),
    ]
)

Package.swift文件这个文件是用来让 package manager 知道你的package是什么, 安装或寻找其他的package的时候, package manager会在项目的仓库里寻找Package.swift 文件, 并且执行这个文件, 提取出以package为名字的变量,然后进行安装

更新完 Package.swift文件后 需要在终端执行以下命令:

# 编译
swift build
# 需要重新生成 `xcodeproj` 文件
swift package generate-xcodeproj

编译以后的文件在当前文件夹下 .build/debug/FengNiaoCopy

2- CommandLineKit 使用

import Foundation
import CommandLineKit


// Create a new flags object for the system-provided command-line arguments
var flags = Flags()

// Define the various flags

/// shortFlag: "f":  表示 -f     longFlag: "filepath":  表示 --filepath
let filePaths  = flags.strings("f", "filepath",
                               description: "Adds file path in which programs are searched for.")
let quiet      = flags.option("q", "quiet",
                              description: "In quiet mode, optional messages are not printed.")
let help       = flags.option("h", "help",
                              description: "Show description of usage and options of this tools.")


// Parse the command-line arguments and return error message if parsing fails
if let failure = flags.parsingFailure() {
  print(failure)
  exit(1)
}
print("Hello, world!")

然后在终端执行如下命令:

# 编译
swift build
# 需要重新生成 `xcodeproj` 文件
swift package generate-xcodeproj

# 测试一下命令行 
.build/debug/FengNiaoCopy  -f
.build/debug/FengNiaoCopy  --filepath
image.png
上一篇 下一篇

猜你喜欢

热点阅读