Skip to content

Commit e984e82

Browse files
committed
feat: Show option and flag default values in help output
Closes #34
1 parent 1247b28 commit e984e82

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
[#37](https://github.com/kylef/Commander/issues/37)
1111
- Argument descriptions are now printed in command help.
1212
[#33](https://github.com/kylef/Commander/issues/33)
13+
- Default option and flag default values will now be shown in help output.
14+
Only default option types of String and Int are currently supported in help output.
15+
[#34](https://github.com/kylef/Commander/issues/34)
1316

1417
### Bug Fixes
1518

Sources/ArgumentDescription.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,12 @@ class BoxedArgumentDescriptor {
253253

254254
if let value = value as? Flag {
255255
`default` = value.`default`.description
256+
} else if let value = value as? Option<String> {
257+
`default` = value.`default`.description
258+
} else if let value = value as? Option<Int> {
259+
`default` = value.`default`.description
256260
} else {
257-
// TODO, default for Option and Options
261+
// TODO, default for Option of generic type
258262
`default` = nil
259263
}
260264
}
@@ -343,13 +347,17 @@ class Help : Error, ANSIConvertible, CustomStringConvertible {
343347
if !options.isEmpty {
344348
output.append("Options:")
345349
for option in options {
346-
// TODO: default, [default: `\(`default`)`]
350+
var line = " --\(option.name)"
351+
352+
if let `default` = option.default {
353+
line += " [default: \(`default`)]"
354+
}
347355

348356
if let description = option.description {
349-
output.append(" --\(option.name) - \(description)")
350-
} else {
351-
output.append(" --\(option.name)")
357+
line += " - \(description)"
352358
}
359+
360+
output.append(line)
353361
}
354362
}
355363

@@ -401,13 +409,17 @@ class Help : Error, ANSIConvertible, CustomStringConvertible {
401409
if !options.isEmpty {
402410
output.append("Options:")
403411
for option in options {
404-
// TODO: default, [default: `\(`default`)`]
412+
var line = " \(ANSI.blue)--\(option.name)\(ANSI.reset)"
413+
414+
if let `default` = option.default {
415+
line += " [default: \(`default`)]"
416+
}
405417

406418
if let description = option.description {
407-
output.append(" \(ANSI.blue)--\(option.name)\(ANSI.reset) - \(description)")
408-
} else {
409-
output.append(" \(ANSI.blue)--\(option.name)\(ANSI.reset)")
419+
line += " - \(description)"
410420
}
421+
422+
output.append(line)
411423
}
412424
}
413425

Tests/CommanderTests/ArgumentDescriptionSpec.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,16 @@ public func testArgumentDescription() {
1313
try expect(help.description) == "Arguments:\n\n arg1\n arg2 - an example\n"
1414
try expect(help.ansiDescription) == "Arguments:\n\n \(ANSI.blue)arg1\(ANSI.reset)\n \(ANSI.blue)arg2\(ANSI.reset) - an example\n"
1515
}
16+
17+
$0.it("shows options") {
18+
let help = Help([
19+
BoxedArgumentDescriptor(value: Option<String>("opt1", "example")),
20+
BoxedArgumentDescriptor(value: Flag("flag1", description: "an example")),
21+
BoxedArgumentDescriptor(value: Flag("flag2", default: true)),
22+
])
23+
24+
try expect(help.description) == "Options:\n --opt1 [default: example]\n --flag1 [default: false] - an example\n --flag2 [default: true]"
25+
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]"
26+
}
1627
}
1728
}

0 commit comments

Comments
 (0)