Skip to content

Commit 5347179

Browse files
committed
[swift-inspect] Consolidate options into OptionGroups shared between subcommands.
1 parent b65d2b5 commit 5347179

File tree

1 file changed

+39
-30
lines changed
  • tools/swift-inspect/Sources/swift-inspect

1 file changed

+39
-30
lines changed

tools/swift-inspect/Sources/swift-inspect/main.swift

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,34 @@ struct SwiftInspect: ParsableCommand {
152152
])
153153
}
154154

155+
struct UniversalOptions: ParsableArguments {
156+
@Argument(help: "The pid or partial name of the target process")
157+
var nameOrPid: String
158+
}
159+
160+
struct BacktraceOptions: ParsableArguments {
161+
@Flag(help: "Show the backtrace for each allocation")
162+
var backtrace: Bool
163+
164+
@Flag(help: "Show a long-form backtrace for each allocation")
165+
var backtraceLong: Bool
166+
167+
var style: Backtrace.Style? {
168+
backtrace ? .oneLine :
169+
backtraceLong ? .long :
170+
nil
171+
}
172+
}
173+
155174
struct DumpConformanceCache: ParsableCommand {
156175
static let configuration = CommandConfiguration(
157176
abstract: "Print the contents of the target's protocol conformance cache.")
158177

159-
@Argument(help: "The pid or partial name of the target process")
160-
var nameOrPid: String
178+
@OptionGroup()
179+
var options: UniversalOptions
161180

162181
func run() throws {
163-
try withReflectionContext(nameOrPid: nameOrPid) { context, _ in
182+
try withReflectionContext(nameOrPid: options.nameOrPid) { context, _ in
164183
try dumpConformanceCache(context: context)
165184
}
166185
}
@@ -169,22 +188,18 @@ struct DumpConformanceCache: ParsableCommand {
169188
struct DumpRawMetadata: ParsableCommand {
170189
static let configuration = CommandConfiguration(
171190
abstract: "Print the target's metadata allocations.")
172-
@Argument(help: "The pid or partial name of the target process")
173191

174-
var nameOrPid: String
175-
176-
@Flag(help: "Show the backtrace for each allocation")
177-
var backtrace: Bool
192+
@OptionGroup()
193+
var universalOptions: UniversalOptions
178194

179-
@Flag(help: "Show a long-form backtrace for each allocation")
180-
var backtraceLong: Bool
195+
@OptionGroup()
196+
var backtraceOptions: BacktraceOptions
181197

182198
func run() throws {
183-
let style = backtrace ? Backtrace.Style.oneLine :
184-
backtraceLong ? Backtrace.Style.long :
185-
nil
186-
try withReflectionContext(nameOrPid: nameOrPid) {
187-
try dumpRawMetadata(context: $0, inspector: $1, backtraceStyle: style)
199+
try withReflectionContext(nameOrPid: universalOptions.nameOrPid) {
200+
try dumpRawMetadata(context: $0,
201+
inspector: $1,
202+
backtraceStyle: backtraceOptions.style)
188203
}
189204
}
190205
}
@@ -193,23 +208,17 @@ struct DumpGenericMetadata: ParsableCommand {
193208
static let configuration = CommandConfiguration(
194209
abstract: "Print the target's generic metadata allocations.")
195210

196-
@Argument(help: "The pid or partial name of the target process")
197-
var nameOrPid: String
211+
@OptionGroup()
212+
var universalOptions: UniversalOptions
198213

199-
@Flag(help: "Show the backtrace for each allocation")
200-
var backtrace: Bool
201-
202-
@Flag(help: "Show a long-form backtrace for each allocation")
203-
var backtraceLong: Bool
214+
@OptionGroup()
215+
var backtraceOptions: BacktraceOptions
204216

205217
func run() throws {
206-
let style = backtrace ? Backtrace.Style.oneLine :
207-
backtraceLong ? Backtrace.Style.long :
208-
nil
209-
try withReflectionContext(nameOrPid: nameOrPid) {
218+
try withReflectionContext(nameOrPid: universalOptions.nameOrPid) {
210219
try dumpGenericMetadata(context: $0,
211220
inspector: $1,
212-
backtraceStyle: style)
221+
backtraceStyle: backtraceOptions.style)
213222
}
214223
}
215224
}
@@ -218,11 +227,11 @@ struct DumpCacheNodes: ParsableCommand {
218227
static let configuration = CommandConfiguration(
219228
abstract: "Print the target's metadata cache nodes.")
220229

221-
@Argument(help: "The pid or partial name of the target process")
222-
var nameOrPid: String
230+
@OptionGroup()
231+
var options: UniversalOptions
223232

224233
func run() throws {
225-
try withReflectionContext(nameOrPid: nameOrPid) {
234+
try withReflectionContext(nameOrPid: options.nameOrPid) {
226235
try dumpMetadataCacheNodes(context: $0,
227236
inspector: $1)
228237
}

0 commit comments

Comments
 (0)