Skip to content

Commit 2719bf4

Browse files
authored
Merge pull request #92 from owenv/help-argument-parser
Add a swift-argument-parser dependency and use it in swift-help
2 parents a065fa9 + 1f0a62a commit 2719bf4

File tree

6 files changed

+33
-37
lines changed

6 files changed

+33
-37
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ endif()
4242
find_package(dispatch QUIET)
4343
find_package(Foundation QUIET)
4444
find_package(Yams CONFIG REQUIRED)
45+
find_package(ArgumentParser CONFIG REQUIRED)
4546

4647
add_subdirectory(Sources)
4748
add_subdirectory(cmake/modules)

Package.resolved

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
{
22
"object": {
33
"pins": [
4+
{
5+
"package": "swift-argument-parser",
6+
"repositoryURL": "https://github.com/apple/swift-argument-parser.git",
7+
"state": {
8+
"branch": null,
9+
"revision": "15351c1cd009eba0b6e438bfef55ea9847a8dc4a",
10+
"version": "0.3.0"
11+
}
12+
},
413
{
514
"package": "llbuild",
615
"repositoryURL": "https://github.com/apple/swift-llbuild.git",

Package.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let package = Package(
5353
/// The help executable.
5454
.target(
5555
name: "swift-help",
56-
dependencies: ["SwiftOptions"]),
56+
dependencies: ["SwiftOptions", "ArgumentParser"]),
5757

5858
/// The `makeOptions` utility (for importing option definitions).
5959
.target(
@@ -82,10 +82,12 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
8282
package.dependencies += [
8383
.package(url: "https://github.com/apple/swift-tools-support-core.git", .branch("master")),
8484
.package(url: "https://github.com/jpsim/Yams.git", .upToNextMinor(from: "4.0.0")),
85+
.package(url: "https://github.com/apple/swift-argument-parser.git", .exact("0.3.0"))
8586
]
8687
} else {
8788
package.dependencies += [
8889
.package(path: "../swiftpm/swift-tools-support-core"),
8990
.package(path: "../yams"),
91+
.package(path: "../swift-argument-parser"),
9092
]
9193
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ all with CMake:
4646
```
4747
cmake -B <llbuild-build-dir> -G Ninja <llbuild-source-dir> -DLLBUILD_SUPPORT_BINDINGS="Swift"
4848
```
49+
* [swift-argument-parser](https://github.com/apple/swift-argument-parser)
4950
* [Yams](https://github.com/jpsim/Yams)
5051

5152
Once those dependencies have built, build `swift-driver` itself:
5253
```
53-
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
54+
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>
5455
cmake --build <swift-driver-build-dir>
5556
```
5657

Sources/swift-help/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
add_executable(swift-help
1010
main.swift)
1111
target_link_libraries(swift-help PUBLIC
12-
SwiftOptions)
12+
SwiftOptions
13+
ArgumentParser)
1314

Sources/swift-help/main.swift

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,24 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
import SwiftOptions
13-
import TSCBasic
14-
import TSCLibc
15-
import TSCUtility
13+
import ArgumentParser
1614

17-
extension DriverKind: StringEnumArgument {
18-
public static var completion: ShellCompletion { .none }
19-
}
20-
21-
struct Options {
22-
var driverKind: DriverKind = .interactive
23-
var showHidden: Bool = false
24-
}
15+
extension DriverKind: ExpressibleByArgument {}
2516

26-
let driverOptionTable = OptionTable()
27-
let parser = ArgumentParser(commandName: "swift help",
28-
usage: " ",
29-
overview: "Swift help tool",
30-
seeAlso: nil)
31-
let binder = ArgumentBinder<Options>()
32-
binder.bind(option: parser.add(option: "-show-hidden",
33-
usage: "List hidden (unsupported) options"),
34-
to: { $0.showHidden = $1 })
35-
binder.bind(option: parser.add(option: "-tool", kind: DriverKind.self,
36-
usage: "The tool to list options of"),
37-
to: { $0.driverKind = $1 })
17+
struct SwiftHelp: ParsableCommand {
18+
@ArgumentParser.Option(name: .customLong("tool", withSingleDash: true),
19+
help: "The tool to list options of")
20+
var tool: DriverKind = .interactive
3821

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

44-
// Print the option table.
45-
driverOptionTable.printHelp(driverKind: options.driverKind,
46-
includeHidden: options.showHidden)
47-
} catch {
48-
stderrStream <<< "error: " <<< error.localizedDescription
49-
stderrStream.flush()
50-
exit(EXIT_FAILURE)
26+
func run() throws {
27+
let driverOptionTable = OptionTable()
28+
driverOptionTable.printHelp(driverKind: tool, includeHidden: showHidden)
29+
}
5130
}
31+
32+
// SwiftPM executables don't support @main.
33+
SwiftHelp.main()

0 commit comments

Comments
 (0)