Skip to content

Commit 2372ad1

Browse files
committed
feat: expose short flag in help output
1 parent f2ac63b commit 2372ad1

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Master
44

5+
### Enhancements
6+
7+
- Usage/help output for commands which contain flags will now contain the short
8+
flag, for example, `-v, --verbose`.
9+
[#71](https://github.com/kylef/Commander/issues/71)
10+
511
### Bug Fixes
612

713
- Fixed ordering of flags in the "Unknown Arguments" help output of a command.

Sources/Commander/ArgumentDescription.swift

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,31 @@ class BoxedArgumentDescriptor {
269269
let description: String?
270270
let `default`: String?
271271
let type: ArgumentType
272+
let flag: Character?
273+
274+
var summary: String {
275+
var line = ""
276+
277+
switch type {
278+
case .argument:
279+
line += "\(name)"
280+
case .option:
281+
if let flag = flag {
282+
line += "-\(flag), "
283+
}
284+
line += "--\(name)"
285+
}
286+
287+
if let `default` = `default` {
288+
line += " [default: \(`default`)]"
289+
}
290+
291+
if let description = description {
292+
line += " - \(description)"
293+
}
294+
295+
return line
296+
}
272297

273298
init<T : ArgumentDescriptor>(value: T) {
274299
name = value.name
@@ -277,10 +302,13 @@ class BoxedArgumentDescriptor {
277302

278303
if let value = value as? Flag {
279304
`default` = value.`default`.description
305+
flag = value.flag
280306
} else if let value = value as? Option<String> {
281307
`default` = value.`default`.description
308+
flag = nil
282309
} else if let value = value as? Option<Int> {
283310
`default` = value.`default`.description
311+
flag = nil
284312
} else {
285313
let mirror = Mirror(reflecting: value)
286314

@@ -289,6 +317,8 @@ class BoxedArgumentDescriptor {
289317
} else {
290318
`default` = nil
291319
}
320+
321+
flag = nil
292322
}
293323
}
294324
}
@@ -362,32 +392,14 @@ class Help : Error, ANSIConvertible, CustomStringConvertible {
362392
} else if !arguments.isEmpty {
363393
output.append("Arguments:")
364394
output.append("")
365-
366-
output += arguments.map { argument in
367-
if let description = argument.description {
368-
return " \(argument.name) - \(description)"
369-
} else {
370-
return " \(argument.name)"
371-
}
372-
}
373-
395+
output += arguments.map { " \($0.summary)" }
374396
output.append("")
375397
}
376398

377399
if !options.isEmpty {
378400
output.append("Options:")
379401
for option in options {
380-
var line = " --\(option.name)"
381-
382-
if let `default` = option.default {
383-
line += " [default: \(`default`)]"
384-
}
385-
386-
if let description = option.description {
387-
line += " - \(description)"
388-
}
389-
390-
output.append(line)
402+
output.append(" \(option.summary)")
391403
}
392404
}
393405

Tests/CommanderTests/ArgumentDescriptionSpec.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ let testArgumentDescription: ((ContextType) -> Void) = {
7575
try expect(help.ansiDescription) == "Options:\n \(ANSI.blue)--opt1\(ANSI.reset) [default: example]\n \(ANSI.blue)--flag1\(ANSI.reset) [default: false] - an example\n \(ANSI.blue)--flag2\(ANSI.reset) [default: true]"
7676
}
7777

78+
$0.it("shows option flag") {
79+
let help = Help([
80+
BoxedArgumentDescriptor(value: Flag("verbose", flag: "v", description: "enable verbose mode")),
81+
])
82+
83+
try expect(help.description) == "Options:\n -v, --verbose [default: false] - enable verbose mode"
84+
}
85+
7886
$0.it("shows default for custom types conforming to CustomStringConvertible") {
7987
enum Direction: String, CustomStringConvertible, ArgumentConvertible {
8088
case north

0 commit comments

Comments
 (0)