Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public protocol ExpressibleByArgument {
/// Creates a new instance of this type from a command-line-specified
/// argument.
init?(argument: String)

/// Default representation value in help.
///
/// Implement this method to customize default value representation in help.
var defaultValueDescription: String { get }
}

extension String: ExpressibleByArgument {
Expand Down Expand Up @@ -68,7 +73,7 @@ extension Bool: ExpressibleByArgument {}

extension ExpressibleByArgument {

var defaultValueDescription: String {
public var defaultValueDescription: String {

let mirror = Mirror(reflecting: self)

Expand Down
22 changes: 21 additions & 1 deletion Tests/ArgumentParserUnitTests/HelpGenerationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ import ArgumentParserTestHelpers
final class HelpGenerationTests: XCTestCase {
}

extension URL: ExpressibleByArgument {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can you indent with 2 spaces instead of 4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I fixed it now.

public init?(argument: String) {
guard let url = URL(string: argument) else {
return nil
}
self = url
}

public var defaultValueDescription: String {
self.absoluteString == FileManager.default.currentDirectoryPath
? "current directory"
: String(describing: self)
}
}

// MARK: -

extension HelpGenerationTests {
Expand Down Expand Up @@ -117,6 +132,7 @@ extension HelpGenerationTests {
}
}


struct D: ParsableCommand {
@Argument(default: "--", help: "Your occupation.")
var occupation: String
Expand All @@ -138,11 +154,14 @@ extension HelpGenerationTests {

@Option(default: .bachelor, help: "Your degree.", transform: Degree.degreeTransform)
var degree: Degree

@Option(default: URL(string: FileManager.default.currentDirectoryPath)!, help: "Directory.")
var directory: URL
}

func testHelpWithDefaultValues() {
AssertHelp(for: D.self, equals: """
USAGE: d [<occupation>] [--name <name>] [--middle-name <middle-name>] [--age <age>] [--logging <logging>] [--optional] [--required] [--degree <degree>]
USAGE: d [<occupation>] [--name <name>] [--middle-name <middle-name>] [--age <age>] [--logging <logging>] [--optional] [--required] [--degree <degree>] [--directory <directory>]

ARGUMENTS:
<occupation> Your occupation. (default: --)
Expand All @@ -155,6 +174,7 @@ extension HelpGenerationTests {
--logging <logging> Whether logging is enabled. (default: false)
--optional/--required Vegan diet. (default: optional)
--degree <degree> Your degree. (default: bachelor)
--directory <directory> Directory. (default: current directory)
-h, --help Show help information.

""")
Expand Down