Skip to content

Commit

Permalink
Swift 4.0 compatibility (#1)
Browse files Browse the repository at this point in the history
* made swift 4.0 compatible

* added Swift 4.0 to .travis.yml

* added backport of compactMap for swift 4.0

* changed extension to Array

* handling swift version inline instead of trying to backport.
  • Loading branch information
dastrobu authored May 5, 2018
1 parent c0a4000 commit ab3ab05
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ os:
- osx

env:
- SWIFT_VERSION=4.0
- SWIFT_VERSION=4.1

before_install:
Expand Down
28 changes: 19 additions & 9 deletions Sources/parsers/MultiFlag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,25 @@ public class MultiFlag: Parser, ParserNode, ParsePathSegment {
prefixString + String($0)
})
// find a valid flag parser for each flag
let matched: [(parser: Flag, flag: String)] =
flags.compactMap({ flag in
if let parser = parsers
.compactMap({ $0 as? Flag })
.first(where: { parser in parser.aliases.contains(flag) }) {
return (parser: parser, flag: flag)
}
return nil
})
#if swift(>=4.1)
let matched: [(parser: Flag, flag: String)] = flags.compactMap({ flag in
if let parser = self.parsers
.compactMap({ $0 as? Flag })
.first(where: { parser in parser.aliases.contains(flag) }) {
return (parser: parser, flag: flag)
}
return nil
})
#else
let matched: [(parser: Flag, flag: String)] = flags.flatMap({ flag in
if let parser = self.parsers
.flatMap({ $0 as? Flag })
.first(where: { parser in parser.aliases.contains(flag) }) {
return (parser: parser, flag: flag)
}
return nil
})
#endif
// check if all flags are valid flags, do not treat this as multi flag otherwise
if matched.count == flags.count {
try matched.forEach { parser, flag in
Expand Down

0 comments on commit ab3ab05

Please sign in to comment.