Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve Argument help message #129

Merged
merged 1 commit into from
May 29, 2018
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
26 changes: 24 additions & 2 deletions Sources/Commandant/Argument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,39 @@ public struct Argument<T> {
/// be shown in help messages.
public let usage: String

public init(defaultValue: T? = nil, usage: String) {
/// A human-readable string that describes this argument as a paramater shown
/// in the list of possible parameters in help messages (e.g. for "paths", the
/// user would see <paths…>).
public let usageParameter: String?

public init(defaultValue: T? = nil, usage: String, usageParameter: String? = nil) {
self.defaultValue = defaultValue
self.usage = usage
self.usageParameter = usageParameter
}

fileprivate func invalidUsageError<ClientError>(_ value: String) -> CommandantError<ClientError> {
let description = "Invalid value for '\(self)': \(value)"
let description = "Invalid value for '\(self.usageParameterDescription)': \(value)"
return .usageError(description: description)
}
}

extension Argument {
/// A string describing this argument as a parameter in help messages. This falls back
/// to `"\(self)"` if `usageParameter` is `nil`
internal var usageParameterDescription: String {
return self.usageParameter.map { "<\($0)>" } ?? "\(self)"
}
}

extension Argument where T: Sequence {
/// A string describing this argument as a parameter in help messages. This falls back
/// to `"\(self)"` if `usageParameter` is `nil`
internal var usageParameterDescription: String {
return self.usageParameter.map { "<\($0)…>" } ?? "\(self)"
}
}

// MARK: - Operators

extension CommandMode {
Expand Down
8 changes: 6 additions & 2 deletions Sources/Commandant/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ internal func informativeUsageError<T: ArgumentProtocol, ClientError>(_ argument
var example = ""

var valueExample = ""
if let defaultValue = argument.defaultValue {
if argument.usageParameter != nil {
valueExample = argument.usageParameterDescription
} else if let defaultValue = argument.defaultValue {
valueExample = "\(defaultValue)"
}

Expand All @@ -106,7 +108,9 @@ internal func informativeUsageError<T: ArgumentProtocol, ClientError>(_ argument
var example = ""

var valueExample = ""
if let defaultValue = argument.defaultValue {
if argument.usageParameter != nil {
valueExample = argument.usageParameterDescription
} else if let defaultValue = argument.defaultValue {
valueExample = "\(defaultValue)"
}

Expand Down