Skip to content

Commit a7f9284

Browse files
committed
[SR-6347] Added "SEE ALSO" to --help for Swift commands
1 parent e36eebf commit a7f9284

File tree

10 files changed

+41
-8
lines changed

10 files changed

+41
-8
lines changed

Sources/Commands/SwiftBuildTool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class SwiftBuildTool: SwiftTool<BuildToolOptions> {
3737
toolName: "build",
3838
usage: "[options]",
3939
overview: "Build sources into binary products",
40-
args: args
40+
args: args,
41+
seeAlso: "swift run, swift package, swift test"
4142
)
4243
}
4344

Sources/Commands/SwiftPackageTool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
3737
toolName: "package",
3838
usage: "[options] subcommand",
3939
overview: "Perform operations on Swift packages",
40-
args: args
40+
args: args,
41+
seeAlso: "swift build, swift run, swift test"
4142
)
4243
}
4344
override func runImpl() throws {

Sources/Commands/SwiftRunTool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public class SwiftRunTool: SwiftTool<RunToolOptions> {
8989
toolName: "run",
9090
usage: "[options] [executable [arguments ...]]",
9191
overview: "Build and run an executable product",
92-
args: args
92+
args: args,
93+
seeAlso: "swift build, swift package, swift test"
9394
)
9495
}
9596

Sources/Commands/SwiftTestTool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public class SwiftTestTool: SwiftTool<TestToolOptions> {
115115
toolName: "test",
116116
usage: "[options]",
117117
overview: "Build and run tests",
118-
args: args
118+
args: args,
119+
seeAlso: "swift build, swift run, swift package"
119120
)
120121
}
121122

Sources/Commands/SwiftTool.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,16 @@ public class SwiftTool<Options: ToolOptions> {
149149
/// Create an instance of this tool.
150150
///
151151
/// - parameter args: The command line arguments to be passed to this tool.
152-
public init(toolName: String, usage: String, overview: String, args: [String]) {
152+
public init(toolName: String, usage: String, overview: String, args: [String], seeAlso: String? = nil) {
153153
// Capture the original working directory ASAP.
154154
originalWorkingDirectory = currentWorkingDirectory
155155

156156
// Create the parser.
157157
parser = ArgumentParser(
158158
commandName: "swift \(toolName)",
159159
usage: usage,
160-
overview: overview)
160+
overview: overview,
161+
seeAlso: seeAlso)
161162

162163
// Create the binder.
163164
let binder = ArgumentBinder<Options>()

Sources/Utility/ArgumentParser.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ public final class ArgumentParser {
597597

598598
/// Overview text of this parser.
599599
let overview: String
600+
601+
/// See more text of this parser.
602+
let seeAlso: String?
600603

601604
/// If this parser is a subparser.
602605
private let isSubparser: Bool
@@ -613,11 +616,13 @@ public final class ArgumentParser {
613616
/// Otherwise, first command line argument will be used.
614617
/// - usage: The "usage" line of the generated usage text.
615618
/// - overview: The "overview" line of the generated usage text.
616-
public init(commandName: String? = nil, usage: String, overview: String) {
619+
/// - seeAlso: The "see also" line of generated usage text.
620+
public init(commandName: String? = nil, usage: String, overview: String, seeAlso: String? = nil) {
617621
self.isSubparser = false
618622
self.commandName = commandName
619623
self.usage = usage
620624
self.overview = overview
625+
self.seeAlso = seeAlso
621626
}
622627

623628
/// Create a subparser with its help text.
@@ -626,6 +631,7 @@ public final class ArgumentParser {
626631
self.commandName = nil
627632
self.usage = ""
628633
self.overview = overview
634+
self.seeAlso = nil
629635
}
630636

631637
/// Adds an option to the parser.
@@ -901,6 +907,12 @@ public final class ArgumentParser {
901907
print(formatted: argument.name, usage: usage, on: stream)
902908
}
903909
}
910+
911+
if let seeAlso = seeAlso {
912+
stream <<< "\n\n"
913+
stream <<< "SEE ALSO: \(seeAlso)"
914+
}
915+
904916
stream <<< "\n"
905917
stream.flush()
906918
}

Tests/CommandsTests/BuildToolTests.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ final class BuildToolTests: XCTestCase {
2020
}
2121

2222
func testUsage() throws {
23-
XCTAssert(try execute(["-help"]).contains("USAGE: swift build"))
23+
XCTAssert(try execute(["--help"]).contains("USAGE: swift build"))
24+
}
25+
26+
func testSeeAlso() throws {
27+
XCTAssert(try execute(["--help"]).contains("SEE ALSO: swift run, swift package, swift test"))
2428
}
2529

2630
func testVersion() throws {

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ final class PackageToolTests: XCTestCase {
2929
XCTAssert(try execute(["--help"]).contains("USAGE: swift package"))
3030
}
3131

32+
func testSeeAlso() throws {
33+
XCTAssert(try execute(["--help"]).contains("SEE ALSO: swift build, swift run, swift test"))
34+
}
35+
3236
func testVersion() throws {
3337
XCTAssert(try execute(["--version"]).contains("Swift Package Manager"))
3438
}

Tests/CommandsTests/RunToolTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ final class RunToolTests: XCTestCase {
2424
XCTAssert(try execute(["--help"]).contains("USAGE: swift run [options] [executable [arguments ...]]"))
2525
}
2626

27+
func testSeeAlso() throws {
28+
XCTAssert(try execute(["--help"]).contains("SEE ALSO: swift build, swift package, swift test"))
29+
}
30+
2731
func testVersion() throws {
2832
XCTAssert(try execute(["--version"]).contains("Swift Package Manager"))
2933
}

Tests/CommandsTests/TestToolTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ final class TestToolTests: XCTestCase {
2222
XCTAssert(try execute(["--help"]).contains("USAGE: swift test"))
2323
}
2424

25+
func testSeeAlso() throws {
26+
XCTAssert(try execute(["--help"]).contains("SEE ALSO: swift build, swift run, swift package"))
27+
}
28+
2529
func testVersion() throws {
2630
XCTAssert(try execute(["--version"]).contains("Swift Package Manager"))
2731
}

0 commit comments

Comments
 (0)