Skip to content

Commit e371989

Browse files
Add support for customizing default value string displayed in Help (#87) (#94)
1 parent 72fb0b1 commit e371989

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Sources/ArgumentParser/Parsable Types/ExpressibleByArgument.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public protocol ExpressibleByArgument {
1414
/// Creates a new instance of this type from a command-line-specified
1515
/// argument.
1616
init?(argument: String)
17+
18+
/// Default representation value in help.
19+
///
20+
/// Implement this method to customize default value representation in help.
21+
var defaultValueDescription: String { get }
1722
}
1823

1924
extension String: ExpressibleByArgument {
@@ -68,7 +73,7 @@ extension Bool: ExpressibleByArgument {}
6873

6974
extension ExpressibleByArgument {
7075

71-
var defaultValueDescription: String {
76+
public var defaultValueDescription: String {
7277

7378
let mirror = Mirror(reflecting: self)
7479

Tests/ArgumentParserUnitTests/HelpGenerationTests.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ import ArgumentParserTestHelpers
1616
final class HelpGenerationTests: XCTestCase {
1717
}
1818

19+
extension URL: ExpressibleByArgument {
20+
public init?(argument: String) {
21+
guard let url = URL(string: argument) else {
22+
return nil
23+
}
24+
self = url
25+
}
26+
27+
public var defaultValueDescription: String {
28+
self.absoluteString == FileManager.default.currentDirectoryPath
29+
? "current directory"
30+
: String(describing: self)
31+
}
32+
}
33+
1934
// MARK: -
2035

2136
extension HelpGenerationTests {
@@ -117,6 +132,7 @@ extension HelpGenerationTests {
117132
}
118133
}
119134

135+
120136
struct D: ParsableCommand {
121137
@Argument(default: "--", help: "Your occupation.")
122138
var occupation: String
@@ -138,11 +154,14 @@ extension HelpGenerationTests {
138154

139155
@Option(default: .bachelor, help: "Your degree.", transform: Degree.degreeTransform)
140156
var degree: Degree
157+
158+
@Option(default: URL(string: FileManager.default.currentDirectoryPath)!, help: "Directory.")
159+
var directory: URL
141160
}
142161

143162
func testHelpWithDefaultValues() {
144163
AssertHelp(for: D.self, equals: """
145-
USAGE: d [<occupation>] [--name <name>] [--middle-name <middle-name>] [--age <age>] [--logging <logging>] [--optional] [--required] [--degree <degree>]
164+
USAGE: d [<occupation>] [--name <name>] [--middle-name <middle-name>] [--age <age>] [--logging <logging>] [--optional] [--required] [--degree <degree>] [--directory <directory>]
146165
147166
ARGUMENTS:
148167
<occupation> Your occupation. (default: --)
@@ -155,6 +174,7 @@ extension HelpGenerationTests {
155174
--logging <logging> Whether logging is enabled. (default: false)
156175
--optional/--required Vegan diet. (default: optional)
157176
--degree <degree> Your degree. (default: bachelor)
177+
--directory <directory> Directory. (default: current directory)
158178
-h, --help Show help information.
159179
160180
""")

0 commit comments

Comments
 (0)