Skip to content

Commit add50ec

Browse files
authored
Swift 4 2 (#7)
* made init of Command public. * updated for swift 4.2 * added discardable result. * moved createTable to Help class. * fixed config to runt with Swift 4.0 as well. * updated xcode build image for travis.
1 parent ad2779a commit add50ec

File tree

4 files changed

+39
-36
lines changed

4 files changed

+39
-36
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sudo: required
33
# do not use swift as language, since otherwise the build always runs on a macOS VM.
44
language: generic
55
dist: trusty # Ubuntu 14.04 LTS
6-
osx_image: xcode9.3beta
6+
osx_image: xcode10.1
77

88
compiler:
99
- clang
@@ -17,6 +17,7 @@ os:
1717
env:
1818
- SWIFT_VERSION=4.0
1919
- SWIFT_VERSION=4.1
20+
- SWIFT_VERSION=4.2
2021

2122
before_install:
2223
- uname -a

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# argtree
22

3-
[![Swift Version](https://img.shields.io/badge/swift-4.1-blue.svg)](https://swift.org)
3+
[![Swift Version](https://img.shields.io/badge/swift-4.2-blue.svg)](https://swift.org)
44
![Platform](https://img.shields.io/badge/platform-osx--64|linux--64-lightgrey.svg)
55
[![Build Travis-CI Status](https://travis-ci.org/dastrobu/argtree.svg?branch=master)](https://travis-ci.org/dastrobu/argtree)
66

@@ -343,7 +343,7 @@ Instead of defining separate Flag instances, different actions can also be perfo
343343
```swift
344344
let help = Flag(longName: "help", shortName: "h")
345345
let foo = Command(name: "foo", parsers: [help])
346-
help.parsed = { _, path in
346+
help.parsed = { path in
347347
switch path.last {
348348
case let cmd as Command where cmd === foo:
349349
print("foo help")

Sources/ArgTree.swift

+3-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public class ArgTree: ParserNode {
116116
rows.append([" ", argument, description])
117117
})
118118
self.writeToOutStream(
119-
"\(description)\n\(createTable(rows))")
119+
"\(description)\n\(Help.createTable(rows))")
120120
helpPrinted()
121121
}
122122
// add help as first parse, to play together with the var arg parser
@@ -217,6 +217,7 @@ public extension ArgTree {
217217
self.parsers.insert(parsers, at: i)
218218
}
219219

220+
@discardableResult
220221
public func remove(at i: Int) -> Parser {
221222
return parsers.remove(at: i)
222223
}
@@ -225,6 +226,7 @@ public extension ArgTree {
225226
parsers.removeSubrange(bounds)
226227
}
227228

229+
@discardableResult
228230
public func removeFirst() -> Parser {
229231
return parsers.removeFirst()
230232
}

Sources/parsers/Help.swift

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
/** create a simple ascii table */
2-
internal func createTable(_ rows: [[String]]) -> String {
3-
var colMaxCount: [Int] = []
4-
// determine the widths of the columns
5-
rows.forEach { row in
6-
row.enumerated().forEach { i, col in
7-
if colMaxCount.count <= i {
8-
colMaxCount.append(col.count)
1+
/**
2+
* Flag to show a help text
3+
*/
4+
public class Help: Flag {
5+
6+
/** create a simple ascii table */
7+
public static func createTable(_ rows: [[String]]) -> String {
8+
var colMaxCount: [Int] = []
9+
// determine the widths of the columns
10+
rows.forEach { row in
11+
row.enumerated().forEach { i, col in
12+
if colMaxCount.count <= i {
13+
colMaxCount.append(col.count)
14+
}
15+
colMaxCount[i] = max(colMaxCount[i], col.count)
916
}
10-
colMaxCount[i] = max(colMaxCount[i], col.count)
1117
}
12-
}
1318

14-
// set the last col count to zero, to avoid padding the last col
15-
if colMaxCount.popLast() != nil {
16-
colMaxCount.append(0)
17-
}
19+
// set the last col count to zero, to avoid padding the last col
20+
if colMaxCount.popLast() != nil {
21+
colMaxCount.append(0)
22+
}
1823

19-
// format the columns
20-
let r = rows.map { row in
21-
return row
22-
.enumerated()
23-
.map({ i, col in
24-
if col.count < colMaxCount[i] {
25-
// right pad texts with whitespace
26-
return col + String(repeating: " ", count: colMaxCount[i] - col.count)
27-
}
28-
return col
29-
}).joined(separator: " ")
24+
// format the columns
25+
let r = rows.map { row in
26+
return row
27+
.enumerated()
28+
.map({ i, col in
29+
if col.count < colMaxCount[i] {
30+
// right pad texts with whitespace
31+
return col + String(repeating: " ", count: colMaxCount[i] - col.count)
32+
}
33+
return col
34+
}).joined(separator: " ")
35+
}
36+
return r.joined(separator: "\n")
3037
}
31-
return r.joined(separator: "\n")
32-
}
33-
34-
/**
35-
* Flag to show a help text
36-
*/
37-
public class Help: Flag {
3838

3939
public init(longName: String? = "help",
4040
shortName: Character? = "h",

0 commit comments

Comments
 (0)