/
DumpHelpGenerator.swift
171 lines (154 loc) · 5.57 KB
/
DumpHelpGenerator.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
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
//===----------------------------------------------------------*- 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
//
//===----------------------------------------------------------------------===//
@_implementationOnly import Foundation
@_implementationOnly import ArgumentParserToolInfo
internal struct DumpHelpGenerator {
var toolInfo: ToolInfoV0
init(_ type: ParsableArguments.Type) {
self.init(commandStack: [type.asCommand])
}
init(commandStack: [ParsableCommand.Type]) {
self.toolInfo = ToolInfoV0(commandStack: commandStack)
}
func rendered() -> String {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
if #available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *) {
encoder.outputFormatting.insert(.sortedKeys)
}
guard let encoded = try? encoder.encode(self.toolInfo) else { return "" }
return String(data: encoded, encoding: .utf8) ?? ""
}
}
fileprivate extension BidirectionalCollection where Element == ParsableCommand.Type {
/// Returns the ArgumentSet for the last command in this stack, including
/// help and version flags, when appropriate.
func allArguments() -> ArgumentSet {
guard var arguments = self.last.map({ ArgumentSet($0, visibility: .private, parent: .root) })
else { return ArgumentSet() }
self.versionArgumentDefinition().map { arguments.append($0) }
self.helpArgumentDefinition().map { arguments.append($0) }
return arguments
}
}
fileprivate extension ArgumentSet {
func mergingCompositeArguments() -> ArgumentSet {
var arguments = ArgumentSet()
var slice = self[...]
while var argument = slice.popFirst() {
if argument.help.isComposite {
// If this argument is composite, we have a group of arguments to
// merge together.
let groupEnd = slice
.firstIndex { $0.help.keys != argument.help.keys }
?? slice.endIndex
let group = [argument] + slice[..<groupEnd]
slice = slice[groupEnd...]
switch argument.kind {
case .named:
argument.kind = .named(group.flatMap(\.names))
case .positional, .default:
break
}
argument.help.valueName = group.map(\.valueName).first { !$0.isEmpty } ?? ""
argument.help.defaultValue = group.compactMap(\.help.defaultValue).first
argument.help.abstract = group.map(\.help.abstract).first { !$0.isEmpty } ?? ""
argument.help.discussion = group.map(\.help.discussion).first { !$0.isEmpty } ?? ""
}
arguments.append(argument)
}
return arguments
}
}
fileprivate extension ToolInfoV0 {
init(commandStack: [ParsableCommand.Type]) {
self.init(command: CommandInfoV0(commandStack: commandStack))
}
}
fileprivate extension CommandInfoV0 {
init(commandStack: [ParsableCommand.Type]) {
guard let command = commandStack.last else {
preconditionFailure("commandStack must not be empty")
}
let parents = commandStack.dropLast()
var superCommands = parents.map { $0._commandName }
if let superName = parents.first?.configuration._superCommandName {
superCommands.insert(superName, at: 0)
}
let defaultSubcommand = command.configuration.defaultSubcommand?
.configuration.commandName
let subcommands = command.configuration.subcommands
.map { subcommand -> CommandInfoV0 in
var commandStack = commandStack
commandStack.append(subcommand)
return CommandInfoV0(commandStack: commandStack)
}
let arguments = commandStack
.allArguments()
.mergingCompositeArguments()
.compactMap(ArgumentInfoV0.init)
self = CommandInfoV0(
superCommands: superCommands,
commandName: command._commandName,
abstract: command.configuration.abstract,
discussion: command.configuration.discussion,
defaultSubcommand: defaultSubcommand,
subcommands: subcommands,
arguments: arguments)
}
}
fileprivate extension ArgumentInfoV0 {
init?(argument: ArgumentDefinition) {
guard let kind = ArgumentInfoV0.KindV0(argument: argument) else { return nil }
self.init(
kind: kind,
shouldDisplay: argument.help.visibility.base == .default,
sectionTitle: argument.help.parentTitle.nonEmpty,
isOptional: argument.help.options.contains(.isOptional),
isRepeating: argument.help.options.contains(.isRepeating),
names: argument.names.map(ArgumentInfoV0.NameInfoV0.init),
preferredName: argument.names.preferredName.map(ArgumentInfoV0.NameInfoV0.init),
valueName: argument.valueName,
defaultValue: argument.help.defaultValue,
allValues: argument.help.allValues,
abstract: argument.help.abstract,
discussion: argument.help.discussion)
}
}
fileprivate extension ArgumentInfoV0.KindV0 {
init?(argument: ArgumentDefinition) {
switch argument.kind {
case .named:
switch argument.update {
case .nullary:
self = .flag
case .unary:
self = .option
}
case .positional:
self = .positional
case .default:
return nil
}
}
}
fileprivate extension ArgumentInfoV0.NameInfoV0 {
init(name: Name) {
switch name {
case let .long(n):
self.init(kind: .long, name: n)
case let .short(n, _):
self.init(kind: .short, name: String(n))
case let .longWithSingleDash(n):
self.init(kind: .longWithSingleDash, name: n)
}
}
}