Skip to content

Commit 1247b28

Browse files
committed
feat: Show arguments description in help output
Closes #33
1 parent 31e1229 commit 1247b28

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
- Adds support for variadic options, allowing the user to repeat options to
99
provide additional values.
1010
[#37](https://github.com/kylef/Commander/issues/37)
11+
- Argument descriptions are now printed in command help.
12+
[#33](https://github.com/kylef/Commander/issues/33)
1113

1214
### Bug Fixes
1315

1416
- `VaradicArgument` has been renamed to `VariadicArgument`.
1517

18+
1619
## 0.5.0
1720

1821
### Enhancements

Sources/ArgumentDescription.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,19 @@ class Help : Error, ANSIConvertible, CustomStringConvertible {
324324
output.append(" + \(command.name)")
325325
}
326326
}
327+
output.append("")
328+
} else if !arguments.isEmpty {
329+
output.append("Arguments:")
330+
output.append("")
331+
332+
output += arguments.map { argument in
333+
if let description = argument.description {
334+
return " \(argument.name) - \(description)"
335+
} else {
336+
return " \(argument.name)"
337+
}
338+
}
339+
327340
output.append("")
328341
}
329342

@@ -369,6 +382,19 @@ class Help : Error, ANSIConvertible, CustomStringConvertible {
369382
output.append(" + \(ANSI.green)\(command.name)\(ANSI.reset)")
370383
}
371384
}
385+
output.append("")
386+
} else if !arguments.isEmpty {
387+
output.append("Arguments:")
388+
output.append("")
389+
390+
output += arguments.map { argument in
391+
if let description = argument.description {
392+
return " \(ANSI.blue)\(argument.name)\(ANSI.reset) - \(description)"
393+
} else {
394+
return " \(ANSI.blue)\(argument.name)\(ANSI.reset)"
395+
}
396+
}
397+
372398
output.append("")
373399
}
374400

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Spectre
2+
@testable import Commander
3+
4+
5+
public func testArgumentDescription() {
6+
describe("help") {
7+
$0.it("shows arguments") {
8+
let help = Help([
9+
BoxedArgumentDescriptor(value: Argument<String>("arg1")),
10+
BoxedArgumentDescriptor(value: Argument<String>("arg2", description: "an example")),
11+
])
12+
13+
try expect(help.description) == "Arguments:\n\n arg1\n arg2 - an example\n"
14+
try expect(help.ansiDescription) == "Arguments:\n\n \(ANSI.blue)arg1\(ANSI.reset)\n \(ANSI.blue)arg2\(ANSI.reset) - an example\n"
15+
}
16+
}
17+
}

Tests/CommanderTests/XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class CommanderTests: XCTestCase {
55
func testRunCommander() {
66
testArgumentParser()
77
testArgumentConvertible()
8+
testArgumentDescription()
89
testCommandType()
910
testCommand()
1011
testGroup()

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import CommanderTests
22

33
testArgumentParser()
44
testArgumentConvertible()
5+
testArgumentDescription()
56
testCommandType()
67
testCommand()
78
testGroup()

0 commit comments

Comments
 (0)