diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorDoccReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorDoccReference().md index 77ea1653..7e9ea67f 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorDoccReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorDoccReference().md @@ -28,7 +28,7 @@ This is optional. Show subcommand help information. ``` -color help [...] +color help [...] ``` - term **subcommands**: diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorMarkdownReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorMarkdownReference().md index 966fd9cd..6d16c6da 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorMarkdownReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testColorMarkdownReference().md @@ -28,7 +28,7 @@ This is optional. Show subcommand help information. ``` -color help [...] +color help [...] ``` **subcommands:** diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesDoccReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesDoccReference().md index 77b80fe5..48d93d7c 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesDoccReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesDoccReference().md @@ -3,7 +3,8 @@ ``` -count-lines [] [--prefix=] [--verbose] [--help] +count-lines [] [--prefix=] [--verbose] + [--help] ``` - term **input-file**: @@ -31,7 +32,7 @@ count-lines [] [--prefix=] [--verbose] [--help] Show subcommand help information. ``` -count-lines help [...] +count-lines help [...] ``` - term **subcommands**: diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesMarkdownReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesMarkdownReference().md index 88b97b53..619e81d1 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesMarkdownReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testCountLinesMarkdownReference().md @@ -31,7 +31,7 @@ count-lines [] [--prefix=] [--verbose] [--help] Show subcommand help information. ``` -count-lines help [...] +count-lines help [...] ``` **subcommands:** diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathDoccReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathDoccReference().md index 79cb53e5..847a7646 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathDoccReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathDoccReference().md @@ -53,7 +53,8 @@ math add [--hex-output] [...] [--version] [--help] Print the product of the values. ``` -math multiply [--hex-output] [...] [--version] [--help] +math multiply [--hex-output] [...] [--version] + [--help] ``` - term **--hex-output**: @@ -101,7 +102,8 @@ math stats [--version] [--help] Print the average of the values. ``` -math stats average [--kind=] [...] [--version] [--help] +math stats average [--kind=] [...] [--version] + [--help] ``` - term **--kind=\**: @@ -156,7 +158,12 @@ math stats stdev [...] [--version] [--help] Print the quantiles of the values (TBD). ``` -math stats quantiles [] [] [] [...] [--file=] [--directory=] [--shell=] [--custom=] [--custom-deprecated=] [--version] [--help] +math stats quantiles [] [] + [] [...] [--file=] + [--directory=] [--shell=] + [--custom=] + [--custom-deprecated=] [--version] + [--help] ``` - term **one-of-four**: @@ -207,7 +214,7 @@ math stats quantiles [] [] [] [< Show subcommand help information. ``` -math help [...] [--version] +math help [...] [--version] ``` - term **subcommands**: diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathMarkdownReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathMarkdownReference().md index 78419e67..00997feb 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathMarkdownReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testMathMarkdownReference().md @@ -156,7 +156,10 @@ math stats stdev [...] [--version] [--help] Print the quantiles of the values (TBD). ``` -math stats quantiles [] [] [] [...] [--file=] [--directory=] [--shell=] [--custom=] [--custom-deprecated=] [--version] [--help] +math stats quantiles [] [] [] + [...] [--file=] [--directory=] [--shell=] + [--custom=] [--custom-deprecated=] [--version] + [--help] ``` **one-of-four:** @@ -207,7 +210,7 @@ math stats quantiles [] [] [] [< Show subcommand help information. ``` -math help [...] [--version] +math help [...] [--version] ``` **subcommands:** diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatDoccReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatDoccReference().md index 9e6e982d..642159fc 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatDoccReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatDoccReference().md @@ -3,7 +3,8 @@ ``` -repeat [--count=] [--include-counter] [--help] +repeat [--count=] [--include-counter] + [--help] ``` - term **--count=\**: @@ -31,7 +32,7 @@ repeat [--count=] [--include-counter] [--help] Show subcommand help information. ``` -repeat help [...] +repeat help [...] ``` - term **subcommands**: diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatMarkdownReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatMarkdownReference().md index 8d0714b4..37ed4816 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatMarkdownReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRepeatMarkdownReference().md @@ -31,7 +31,7 @@ repeat [--count=] [--include-counter] [--help] Show subcommand help information. ``` -repeat help [...] +repeat help [...] ``` **subcommands:** diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRollDoccReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRollDoccReference().md index 5db09afb..306fae0d 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRollDoccReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRollDoccReference().md @@ -3,7 +3,8 @@ ``` -roll [--times=] [--sides=] [--seed=] [--verbose] [--help] +roll [--times=] [--sides=] [--seed=] [--verbose] + [--help] ``` - term **--times=\**: @@ -38,7 +39,7 @@ Use this option to override the default value of a six-sided die. Show subcommand help information. ``` -roll help [...] +roll help [...] ``` - term **subcommands**: diff --git a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRollMarkdownReference().md b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRollMarkdownReference().md index d84e3e2b..8e0bd387 100644 --- a/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRollMarkdownReference().md +++ b/Tests/ArgumentParserGenerateDoccReferenceTests/Snapshots/testRollMarkdownReference().md @@ -38,7 +38,7 @@ Use this option to override the default value of a six-sided die. Show subcommand help information. ``` -roll help [...] +roll help [...] ``` **subcommands:** diff --git a/Tools/generate-docc-reference/Extensions/ArgumentParser+Markdown.swift b/Tools/generate-docc-reference/Extensions/ArgumentParser+Markdown.swift index 5e8f5787..170e8970 100644 --- a/Tools/generate-docc-reference/Extensions/ArgumentParser+Markdown.swift +++ b/Tools/generate-docc-reference/Extensions/ArgumentParser+Markdown.swift @@ -47,6 +47,14 @@ extension CommandInfoV0 { String(repeating: "#", count: path.count + 1) + " \(self.doccReferenceTitle)\n\n" + // sets the max width for generating code blocks of content based + // on the style + let blockWrapLength: Int = + switch markdownStyle { + case .docc: 60 + case .github: 80 + } + if path.count == 0 { result += "\n\n" } @@ -57,8 +65,11 @@ extension CommandInfoV0 { if let args = self.arguments, args.count != 0 { result += "```\n" + let commandString = (path + [self.commandName]).joined(separator: " ") result += - (path + [self.commandName]).joined(separator: " ") + " " + self.usage() + commandString + + self.usage( + startlength: commandString.count, wraplength: blockWrapLength) result += "\n```\n\n" } @@ -98,12 +109,34 @@ extension CommandInfoV0 { return result } - public func usage() -> String { + /// Returns a mutl-line string that presents the arguments for a command. + /// - Parameters: + /// - startlength: The starting width of the line this multi-line string appends onto. + /// - wraplength: The maximum width of the multi-linecode block. + /// - Returns: A wrapped, multi-line string that wraps the commands arguments into a text block. + public func usage(startlength: Int, wraplength: Int) -> String { guard let args = self.arguments else { return "" } - return args.map { $0.usage() }.joined(separator: " ") + var multilineString = "" + // This is a greedy algorithm to wrap the arguments into a + // multi-line string that is expected to be returned within + // a markdown code block (pre-formatted text). + var currentLength = startlength + for arg in args where arg.shouldDisplay { + let nextUsage = arg.usage() + if currentLength + arg.usage().count > wraplength { + // the next usage() string exceeds the max width, wrap it. + multilineString.append("\n \(nextUsage)") + currentLength = nextUsage.count + 2 // prepend spacing length of 2 + } else { + // the next usage() string doesn't exceed the max width + multilineString.append(" \(nextUsage)") + currentLength += nextUsage.count + 1 + } + } + return multilineString } }