macOS • Windows • Web • Ubuntu • tvOS • iOS • Android • Amazon Linux • watchOS

文档

SDGCommandLine

SDGCommandLine 提供了实现命令行界面的工具。

יְהַלְלוּ אֶת־שֵׁם יהוה כִּי הוּא צִוָּה וְנִבְרָאוּ׃

愿他们赞美耶和华的名,因为他命令,他们就存在了!

—一位诗人

特性

使用示例

此示例创建一个具有以下界面的工具

$ parrot speak
Squawk!

$ parrot speak •phrase "Hello, world!"
Hello, world!

main.swift 必须包含以下内容

Parrot.main()

其余部分可以位于项目中的任何位置(但建议将其放在一个单独的可测试库模块中)

import SDGCommandLine

struct Parrot: Tool {
  static let applicationIdentifier: StrictString = "tld.Developper.Parrot"
  static let version: Version? = Version(1, 0, 0)
  static let packageURL: URL? = URL(string: "https://website.tld/Parrot")
  static let rootCommand: Command = parrot
}

let parrot = Command(
  name: UserFacing<StrictString, MyLocalizations>({ _ in "parrot" }),
  description: UserFacing<StrictString, MyLocalizations>({ _ in "behaves like a parrot." }),
  subcommands: [speak]
)

let speak = Command(
  name: UserFacing<StrictString, MyLocalizations>({ _ in "speak" }),
  description: UserFacing<StrictString, MyLocalizations>({ _ in "speaks." }),
  directArguments: [],
  options: [phrase],
  execution: { (_, options: Options, output: Command.Output) throws -> Void in

    if let specific = options.value(for: phrase) {
      output.print(specific)
    } else {
      output.print("Squawk!")
    }
  }
)

let phrase = Option<StrictString>(
  name: UserFacing<StrictString, MyLocalizations>({ _ in "phrase" }),
  description: UserFacing<StrictString, MyLocalizations>({ _ in "A custom phrase to speak." }),
  type: ArgumentType.string
)

enum MyLocalizations: String, InputLocalization {
  case english = "en"
  static let cases: [MyLocalizations] = [.english]
  static let fallbackLocalization: MyLocalizations = .english
}

测试很容易设置

func testParrot() {
  switch parrot.execute(with: ["speak", "•phrase", "Hello, world!"]) {
  case .success(let output):
    XCTAssertEqual(output, "Hello, world!")
  case .failure:
    XCTFail("The parrot is not co‐operating.")
  }
}

某些平台缺少某些功能。文档中出现的编译条件定义如下

.define("PLATFORM_LACKS_FOUNDATION_PROCESS", .when(platforms: [.wasi, .tvOS, .iOS, .watchOS])),
.define("PLATFORM_LACKS_FOUNDATION_PROCESS_INFO", .when(platforms: [.wasi])),

导入

SDGCommandLine 提供了与 Swift Package Manager 一起使用的库。

只需在 Package.swift 中将 SDGCommandLine 添加为依赖项,并指定要使用的库

let package = Package(
  name: "MyPackage",
  dependencies: [
    .package(
      url: "https://github.com/SDGGiesbrecht/SDGCommandLine",
      from: Version(3, 0, 6)
    ),
  ],
  targets: [
    .target(
      name: "MyTarget",
      dependencies: [
        .product(name: "SDGCommandLine", package: "SDGCommandLine"),
        .product(name: "SDGCommandLineTestUtilities", package: "SDGCommandLine"),
        .product(name: "SDGExportedCommandLineInterface", package: "SDGCommandLine"),
      ]
    )
  ]
)

然后可以在源文件中导入模块

import SDGCommandLine
import SDGCommandLineTestUtilities
import SDGExportedCommandLineInterface

关于

SDGCommandLine 项目由 Jeremy David Giesbrecht 维护。

如果 SDGCommandLine 为您节省了资金,请考虑将其中的一部分作为 捐赠

如果 SDGCommandLine 为您节省了时间,请考虑将其中的一部分用于 贡献 回到该项目。

Ἄξιος γὰρ ὁ ἐργάτης τοῦ μισθοῦ αὐτοῦ ἐστι.

因为工人配得他的工资。

—‎ישוע/Yeshuʼa