1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
//===--------------- main.swift - Swift Help Main Entrypoint --------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftOptions
import ArgumentParser
import enum TSCBasic.ProcessEnv
import func TSCBasic.exec
import class TSCBasic.Process
import var TSCBasic.localFileSystem
enum HelpTopic: ExpressibleByArgument, CustomStringConvertible {
case driver(DriverKind)
case subcommand(Subcommand)
case intro
init?(argument topicName: String) {
if let kind = DriverKind(rawValue: topicName) {
self = .driver(kind)
} else if let subcommand = Subcommand(rawValue: topicName) {
self = .subcommand(subcommand)
} else if topicName == "intro" {
self = .intro
} else {
return nil
}
}
var description: String {
switch self {
case .driver(let kind):
return kind.rawValue
case .subcommand(let command):
return command.rawValue
case .intro:
return "intro"
}
}
}
enum Subcommand: String, CaseIterable {
case build, package, run, test, repl
var description: String {
switch self {
case .build:
return "Build Swift packages"
case .package:
return "Create and work on packages"
case .run:
return "Run a program from a package"
case .test:
return "Run package tests"
case .repl:
return "Experiment with Swift code interactively"
}
}
}
struct SwiftHelp: ParsableCommand {
@Argument(help: "The topic to display help for.")
var topic: HelpTopic = .driver(.interactive)
@Argument(help: "The help subtopics, if applicable.")
var subtopics: [String] = []
@Flag(name: .customLong("show-hidden", withSingleDash: true),
help: "List hidden (unsupported) options")
var showHidden: Bool = false
enum Color256: CustomStringConvertible {
case reset
case color(foreground: UInt8?, background: UInt8?)
var description: String {
switch self {
case .reset:
return "\u{001B}[0m"
case let .color(foreground, background):
let foreground = foreground.map { "\u{001B}[38;5;\($0)m" } ?? ""
let background = background.map { "\u{001B}[48;5;\($0)m" } ?? ""
return foreground + background
}
}
}
func printIntro() {
let is256Color = ProcessEnv.vars["TERM"] == "xterm-256color"
let orangeRed = is256Color ? "\u{001b}[1;38;5;196m" : ""
let plain = is256Color ? "\u{001b}[0m" : ""
let plainBold = is256Color ? "\u{001b}[1m" : ""
print("""
\(orangeRed)Welcome to Swift!\(plain)
\(plainBold)Subcommands:\(plain)
""")
let maxSubcommandNameLength = Subcommand.allCases.map { $0.rawValue.count }.max()!
for command in Subcommand.allCases {
let padding = String(repeating: " ", count: maxSubcommandNameLength - command.rawValue.count)
print(" \(plainBold)swift \(command.rawValue)\(plain)\(padding) \(command.description)")
}
print("\n Use \(plainBold)`swift --version`\(plain) for Swift version information.")
print("\n Use \(plainBold)`swift --help`\(plain) for descriptions of available options and flags.")
print("\n Use \(plainBold)`swift help <subcommand>`\(plain) for more information about a subcommand.")
print()
}
func run() throws {
let driverOptionTable = OptionTable()
switch topic {
case .driver(let kind):
driverOptionTable.printHelp(driverKind: kind, includeHidden: showHidden)
if kind == .interactive {
printIntro()
}
case .subcommand(.repl):
print("""
USAGE: swift repl <options>
The Swift REPL runs code interactively with LLDB.
For most purposes, you can just run `swift repl`.
OPTIONS:
This mode takes optional Swift Frontend arguments: see `swift --help`.
""")
case .subcommand(let subcommand):
// Try to find the subcommand adjacent to the help tool.
// If we didn't find the tool there, let the OS search for it.
#if os(Windows)
let execName = "swift-\(subcommand.rawValue).exe"
#else
let execName = "swift-\(subcommand.rawValue)"
#endif
let subcommandPath = Process.findExecutable(
CommandLine.arguments[0])?
.parentDirectory
.appending(component: execName)
?? Process.findExecutable(execName)
guard let path = subcommandPath, localFileSystem.isExecutableFile(subcommandPath!) else {
fatalError("cannot find subcommand executable '\(execName)'")
}
// Execute the subcommand with --help.
if subtopics.isEmpty {
try exec(path: path.pathString, args: [execName, "--help"])
} else {
try exec(path: path.pathString, args: [execName, "help"] + subtopics)
}
case .intro:
printIntro()
}
}
}
// SwiftPM executables don't support @main.
SwiftHelp.main()
|