Skip to content

Commit b58be37

Browse files
committed
switches argument to --style, defaulting to github flavored markdown, and accepting as 'docc' as an alternative
1 parent b5e2661 commit b58be37

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

Sources/ArgumentParserTestHelpers/TestHelpers.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ extension XCTest {
525525
command = [
526526
"generate-docc-reference", commandURL.path,
527527
"--output-directory", "-",
528-
"--docc-flavored", "true",
528+
"--style", "docc",
529529
]
530530
} else {
531531
command = [

Tools/generate-docc-reference/Extensions/ArgumentParser+Markdown.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ extension CommandInfoV0 {
3737
/// Recursively parses a command to generate markdown content that describes the command.
3838
/// - Parameters:
3939
/// - path: The path of subcommands from the root command.
40-
/// - doccFlavored: a Boolean value that indicates whether to use docc-flavored markdown.
40+
/// - markdownStyle: The flavor of markdown to emit, either `docc` or `github`
4141
/// - Returns: A multi-line markdown file that describes the command.
4242
///
4343
/// If `path` is empty, it represents a top-level command.
4444
/// Otherwise it's a subcommand, potentially recursive to multiple levels.
45-
func toMarkdown(_ path: [String], doccFlavored: Bool) -> String {
45+
func toMarkdown(_ path: [String], markdownStyle: OutputStyle) -> String {
4646
var result =
4747
String(repeating: "#", count: path.count + 1)
4848
+ " \(self.doccReferenceTitle)\n\n"
@@ -72,11 +72,13 @@ extension CommandInfoV0 {
7272
continue
7373
}
7474

75-
if doccFlavored {
75+
switch markdownStyle {
76+
case .docc:
7677
result += "- term **\(arg.identity()):**\n\n"
77-
} else {
78+
case .github:
7879
result += "**\(arg.identity()):**\n\n"
7980
}
81+
8082
if let abstract = arg.abstract {
8183
result += "*\(abstract)*\n\n"
8284
}
@@ -90,7 +92,7 @@ extension CommandInfoV0 {
9092
for subcommand in self.subcommands ?? [] {
9193
result +=
9294
subcommand.toMarkdown(
93-
path + [self.commandName], doccFlavored: doccFlavored) + "\n\n"
95+
path + [self.commandName], markdownStyle: markdownStyle) + "\n\n"
9496
}
9597

9698
return result

Tools/generate-docc-reference/GenerateDoccReference.swift

+14-6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ extension GenerateDoccReferenceError: CustomStringConvertible {
3636
}
3737
}
3838

39+
/// The flavor of generated markdown to emit.
40+
enum OutputStyle: String, EnumerableFlag, ExpressibleByArgument {
41+
/// DocC-supported markdown
42+
case docc
43+
/// GitHub-flavored markdown
44+
case github
45+
}
46+
3947
@main
4048
struct GenerateDoccReference: ParsableCommand {
4149
static let configuration = CommandConfiguration(
@@ -53,7 +61,7 @@ struct GenerateDoccReference: ParsableCommand {
5361
@Option(
5462
name: .shortAndLong,
5563
help: "Use docc flavored markdown for the generated output.")
56-
var doccFlavored: Bool = false
64+
var style: OutputStyle = .github
5765

5866
func validate() throws {
5967
if outputDirectory != "-" {
@@ -112,12 +120,12 @@ struct GenerateDoccReference: ParsableCommand {
112120
do {
113121
if self.outputDirectory == "-" {
114122
try self.generatePages(
115-
from: toolInfo.command, savingTo: nil, doccFlavored: doccFlavored)
123+
from: toolInfo.command, savingTo: nil, flavor: style)
116124
} else {
117125
try self.generatePages(
118126
from: toolInfo.command,
119127
savingTo: URL(fileURLWithPath: outputDirectory),
120-
doccFlavored: doccFlavored)
128+
flavor: style)
121129
}
122130
} catch {
123131
throw GenerateDoccReferenceError.failedToGenerateDoccReference(
@@ -129,14 +137,14 @@ struct GenerateDoccReference: ParsableCommand {
129137
/// - Parameters:
130138
/// - command: The command to parse into a markdown output.
131139
/// - directory: The directory to save the generated markdown file, printing it if `nil`.
132-
/// - doccFlavored: A Boolean value the indicates whether to generate docc-flavored markdown.
140+
/// - flavor: The flavor of markdown to use when generating the content.
133141
/// - Throws: An error if the markdown file cannot be generated or saved.
134142
func generatePages(
135-
from command: CommandInfoV0, savingTo directory: URL?, doccFlavored: Bool
143+
from command: CommandInfoV0, savingTo directory: URL?, flavor: OutputStyle
136144
)
137145
throws
138146
{
139-
let page = command.toMarkdown([], doccFlavored: doccFlavored)
147+
let page = command.toMarkdown([], markdownStyle: style)
140148

141149
if let directory = directory {
142150
let fileName = command.doccReferenceFileName

0 commit comments

Comments
 (0)