/
CompletionsGenerator.swift
129 lines (113 loc) · 4.11 KB
/
CompletionsGenerator.swift
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
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 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
//
//===----------------------------------------------------------------------===//
#if canImport(Glibc)
import Glibc
#elseif canImport(Darwin)
import Darwin
#elseif canImport(CRT)
import CRT
#elseif canImport(WASILibc)
import WASILibc
#endif
/// A shell for which the parser can generate a completion script.
public struct CompletionShell: RawRepresentable, Hashable, CaseIterable {
public var rawValue: String
/// Creates a new instance from the given string.
public init?(rawValue: String) {
switch rawValue {
case "zsh", "bash", "fish":
self.rawValue = rawValue
default:
return nil
}
}
/// An instance representing `zsh`.
public static var zsh: CompletionShell { CompletionShell(rawValue: "zsh")! }
/// An instance representing `bash`.
public static var bash: CompletionShell { CompletionShell(rawValue: "bash")! }
/// An instance representing `fish`.
public static var fish: CompletionShell { CompletionShell(rawValue: "fish")! }
/// Returns an instance representing the current shell, if recognized.
public static func autodetected() -> CompletionShell? {
#if os(Windows)
return nil
#else
// FIXME: This retrieves the user's preferred shell, not necessarily the one currently in use.
guard let shellVar = getenv("SHELL") else { return nil }
let shellParts = String(cString: shellVar).split(separator: "/")
return CompletionShell(rawValue: String(shellParts.last ?? ""))
#endif
}
/// An array of all supported shells for completion scripts.
public static var allCases: [CompletionShell] {
[.zsh, .bash, .fish]
}
}
struct CompletionsGenerator {
var shell: CompletionShell
var command: ParsableCommand.Type
init(command: ParsableCommand.Type, shell: CompletionShell?) throws {
guard let _shell = shell ?? .autodetected() else {
throw ParserError.unsupportedShell()
}
self.shell = _shell
self.command = command
}
init(command: ParsableCommand.Type, shellName: String?) throws {
if let shellName = shellName {
guard let shell = CompletionShell(rawValue: shellName) else {
throw ParserError.unsupportedShell(shellName)
}
try self.init(command: command, shell: shell)
} else {
try self.init(command: command, shell: nil)
}
}
/// Generates a Bash completion script for this generators shell and command..
func generateCompletionScript() -> String {
switch shell {
case .zsh:
return ZshCompletionsGenerator.generateCompletionScript(command)
case .bash:
return BashCompletionsGenerator.generateCompletionScript(command)
case .fish:
return FishCompletionsGenerator.generateCompletionScript(command)
default:
fatalError("Invalid CompletionShell: \(shell)")
}
}
}
extension ArgumentDefinition {
/// Returns a string with the arguments for the callback to generate custom completions for
/// this argument.
func customCompletionCall(_ commands: [ParsableCommand.Type]) -> String {
let subcommandNames = commands.dropFirst().map { $0._commandName }.joined(separator: " ")
let argumentName = names.preferredName?.synopsisString
?? self.help.keys.first?.name ?? "---"
return "---completion \(subcommandNames) -- \(argumentName)"
}
}
extension ParsableCommand {
fileprivate static var compositeCommandName: [String] {
if let superCommandName = configuration._superCommandName {
return [superCommandName] + _commandName.split(separator: " ").map(String.init)
} else {
return _commandName.split(separator: " ").map(String.init)
}
}
}
extension Sequence where Element == ParsableCommand.Type {
func completionFunctionName() -> String {
"_" + self.flatMap { $0.compositeCommandName }
.uniquingAdjacentElements()
.joined(separator: "_")
}
}