Skip to content

Add a swift-argument-parser dependency and use it in swift-help #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ endif()
find_package(dispatch QUIET)
find_package(Foundation QUIET)
find_package(Yams CONFIG REQUIRED)
find_package(ArgumentParser CONFIG REQUIRED)

add_subdirectory(Sources)
add_subdirectory(cmake/modules)
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser.git",
"state": {
"branch": null,
"revision": "15351c1cd009eba0b6e438bfef55ea9847a8dc4a",
"version": "0.3.0"
}
},
{
"package": "llbuild",
"repositoryURL": "https://github.com/apple/swift-llbuild.git",
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let package = Package(
/// The help executable.
.target(
name: "swift-help",
dependencies: ["SwiftOptions"]),
dependencies: ["SwiftOptions", "ArgumentParser"]),

/// The `makeOptions` utility (for importing option definitions).
.target(
Expand Down Expand Up @@ -75,10 +75,12 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
package.dependencies += [
.package(url: "https://github.com/apple/swift-tools-support-core.git", .branch("master")),
.package(url: "https://github.com/jpsim/Yams.git", .upToNextMinor(from: "4.0.0")),
.package(url: "https://github.com/apple/swift-argument-parser.git", .exact("0.3.0"))
]
} else {
package.dependencies += [
.package(path: "../swiftpm/swift-tools-support-core"),
.package(path: "../yams"),
.package(path: "../swift-argument-parser"),
]
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ all with CMake:
```
cmake -B <llbuild-build-dir> -G Ninja <llbuild-source-dir> -DLLBUILD_SUPPORT_BINDINGS="Swift"
```
* [swift-argument-parser](https://github.com/apple/swift-argument-parser)
* [Yams](https://github.com/jpsim/Yams)

Once those dependencies have built, build `swift-driver` itself:
```
cmake -B <swift-driver-build-dir> -G Ninja <swift-driver-source-dir> -DTSC_DIR=<swift-tools-support-core-build-dir>/cmake/modules -DLLBuild_DIR=<llbuild-build-dir>/cmake/modules -DYams_DIR=<yamls-build-dir>/cmake/modules
cmake -B <swift-driver-build-dir> -G Ninja <swift-driver-source-dir> -DTSC_DIR=<swift-tools-support-core-build-dir>/cmake/modules -DLLBuild_DIR=<llbuild-build-dir>/cmake/modules -DYams_DIR=<yamls-build-dir>/cmake/modules -DArgumentParser_DIR=<swift-argument-parser-build-dir>
cmake --build <swift-driver-build-dir>
```

Expand Down
3 changes: 2 additions & 1 deletion Sources/swift-help/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
add_executable(swift-help
main.swift)
target_link_libraries(swift-help PUBLIC
SwiftOptions)
SwiftOptions
ArgumentParser)

50 changes: 16 additions & 34 deletions Sources/swift-help/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,24 @@
//
//===----------------------------------------------------------------------===//
import SwiftOptions
import TSCBasic
import TSCLibc
import TSCUtility
import ArgumentParser

extension DriverKind: StringEnumArgument {
public static var completion: ShellCompletion { .none }
}

struct Options {
var driverKind: DriverKind = .interactive
var showHidden: Bool = false
}
extension DriverKind: ExpressibleByArgument {}

let driverOptionTable = OptionTable()
let parser = ArgumentParser(commandName: "swift help",
usage: " ",
overview: "Swift help tool",
seeAlso: nil)
let binder = ArgumentBinder<Options>()
binder.bind(option: parser.add(option: "-show-hidden",
usage: "List hidden (unsupported) options"),
to: { $0.showHidden = $1 })
binder.bind(option: parser.add(option: "-tool", kind: DriverKind.self,
usage: "The tool to list options of"),
to: { $0.driverKind = $1 })
struct SwiftHelp: ParsableCommand {
@ArgumentParser.Option(name: .customLong("tool", withSingleDash: true),
help: "The tool to list options of")
var tool: DriverKind = .interactive

do {
let parseResult = try parser.parse(Array(CommandLine.arguments.dropFirst()))
var options = Options()
try binder.fill(parseResult: parseResult, into: &options)
@Flag(name: .customLong("show-hidden", withSingleDash: true),
help: "List hidden (unsupported) options")
var showHidden: Bool = false

// Print the option table.
driverOptionTable.printHelp(driverKind: options.driverKind,
includeHidden: options.showHidden)
} catch {
stderrStream <<< "error: " <<< error.localizedDescription
stderrStream.flush()
exit(EXIT_FAILURE)
func run() throws {
let driverOptionTable = OptionTable()
driverOptionTable.printHelp(driverKind: tool, includeHidden: showHidden)
}
}

// SwiftPM executables don't support @main.
SwiftHelp.main()