From ab3ab0545141e1f0de318bf98b1ec5627ccc3b1c Mon Sep 17 00:00:00 2001 From: Daniel Strobusch <1847260+dastrobu@users.noreply.github.com> Date: Sat, 5 May 2018 11:41:12 +0200 Subject: [PATCH] Swift 4.0 compatibility (#1) * 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. --- .travis.yml | 1 + Sources/parsers/MultiFlag.swift | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9c641de..77dd844 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ os: - osx env: + - SWIFT_VERSION=4.0 - SWIFT_VERSION=4.1 before_install: diff --git a/Sources/parsers/MultiFlag.swift b/Sources/parsers/MultiFlag.swift index ff01395..5fc7ac5 100644 --- a/Sources/parsers/MultiFlag.swift +++ b/Sources/parsers/MultiFlag.swift @@ -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