Skip to content

Commit 2bd6ee0

Browse files
committed
Add support for default help command.
1 parent d7a515b commit 2bd6ee0

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

OptionParser/Command.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,17 @@ extension OptionParser {
9191
}
9292

9393
override func parse(from context: inout ParseContext, _ parentRecord: Record) throws {
94+
let toolName = context.toolName
95+
let printer = context.printer
9496
try syntax.parse(from: &context, parentRecord: parentRecord) { record in
9597
if let commandName = record.command {
9698
guard let command = self.parentSyntax!.commandsByName[commandName] else {
9799
throw OptionError("Unknown command '\(commandName)'")
98100
}
99-
try command.printHelp(toolName: context.toolName, printer: context.printer)
101+
try command.printHelp(toolName: toolName, printer: printer)
100102
}
101103
else {
102-
try self.parentSyntax!.printHelp(toolName: context.toolName, printer: context.printer)
104+
try self.parentSyntax!.printHelp(toolName: toolName, printer: printer)
103105
}
104106
}
105107
}

OptionParser/OptionParser.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public struct OptionError: Error {
1212

1313
public struct OptionParser<Record> {
1414
let syntax: Syntax<Void, Record>
15-
let action: (Record) throws -> Void
15+
let action: ((Record) throws -> Void)?
1616

1717
public init(docs: String,
1818
initial: Record,
@@ -26,6 +26,17 @@ public struct OptionParser<Record> {
2626
action: defaultAction)
2727
}
2828

29+
public init(docs: String,
30+
initial: Record,
31+
options: [Option] = [],
32+
commands: [AnyCommand]) {
33+
self.init(_docs: docs,
34+
initial: initial,
35+
options: options,
36+
commands: commands,
37+
action: nil)
38+
}
39+
2940
public init(docs: String,
3041
initial: Record,
3142
options: [Option] = [],
@@ -43,7 +54,7 @@ public struct OptionParser<Record> {
4354
options: [Option] = [],
4455
parameters: [Parameter] = [],
4556
commands: [AnyCommand] = [],
46-
action: @escaping (Record) throws -> Void) {
57+
action: ((Record) throws -> Void)?) {
4758

4859
self.syntax = Syntax<Void, Record>(docs: docs,
4960
initial: initial,

OptionParser/Syntax.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class Syntax<Record, Subrecord> {
5656
self.init(name: name, docs: docs, initial: initial, options: options, parameters: parameters, commands: [])
5757
}
5858

59-
func parse(from context: inout ParseContext, parentRecord: Record, action: (Subrecord) throws -> Void) throws {
59+
func parse(from context: inout ParseContext, parentRecord: Record, action: ((Subrecord) throws -> Void)?) throws {
6060
var record = try initial(parentRecord)
6161
var arguments: [String] = []
6262
var parametersOnly = false
@@ -92,7 +92,12 @@ final class Syntax<Record, Subrecord> {
9292
}
9393
}
9494
try parsePositionalArguments(arguments, record: &record)
95-
try action(record)
95+
if let action = action {
96+
try action(record)
97+
}
98+
else {
99+
try self.printHelp(toolName: context.toolName, printer: context.printer)
100+
}
96101
}
97102

98103
private func parsePositionalArguments(_ arguments: [String], record: inout Subrecord) throws {

0 commit comments

Comments
 (0)