Skip to content

Commit

Permalink
Merge branch 'master' into feature/enumsAsArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
nekrich authored Nov 28, 2017
2 parents 415198d + fdb28de commit bcef806
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
12 changes: 10 additions & 2 deletions Commandant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

/* Begin PBXBuildFile section */
680759651FC5A25D00D6CA70 /* OptionsWithEnumProtocolSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 680759631FC5A1F600D6CA70 /* OptionsWithEnumProtocolSpec.swift */; };
680759661FC5B39600D6CA70 /* OptionSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE281A20741C00109F8C /* OptionSpec.swift */; };
CD2ED3411C1E6C5D0076092B /* Argument.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2ED3401C1E6C5D0076092B /* Argument.swift */; };
CD2ED3431C1E6D540076092B /* ArgumentProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = CD2ED3421C1E6D540076092B /* ArgumentProtocol.swift */; };
CDCE78341FBAB047005A9F76 /* OrderedSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCE78331FBAB047005A9F76 /* OrderedSet.swift */; };
CDCE78361FBAB0A2005A9F76 /* OrderedSetSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCE78351FBAB0A2005A9F76 /* OrderedSetSpec.swift */; };
CDFC88361C3C0612003AC8F8 /* CommandSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDFC88351C3C0612003AC8F8 /* CommandSpec.swift */; };
D00CCDDF1A20717400109F8C /* Commandant.h in Headers */ = {isa = PBXBuildFile; fileRef = D00CCDDE1A20717400109F8C /* Commandant.h */; settings = {ATTRIBUTES = (Public, ); }; };
D00CCDE51A20717400109F8C /* Commandant.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D00CCDD91A20717400109F8C /* Commandant.framework */; };
D00CCE241A2073DA00109F8C /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D00CCE221A2073DA00109F8C /* Nimble.framework */; };
D00CCE251A2073DA00109F8C /* Quick.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D00CCE231A2073DA00109F8C /* Quick.framework */; };
D00CCE271A20741300109F8C /* Command.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE261A20741300109F8C /* Command.swift */; };
D00CCE291A20741C00109F8C /* OptionSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE281A20741C00109F8C /* OptionSpec.swift */; };
D00CCE2B1A20748500109F8C /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE2A1A20748500109F8C /* Errors.swift */; };
D00CCE2D1A2075ED00109F8C /* ArgumentParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE2C1A2075ED00109F8C /* ArgumentParser.swift */; };
D00CCE2F1A2075F700109F8C /* Option.swift in Sources */ = {isa = PBXBuildFile; fileRef = D00CCE2E1A2075F700109F8C /* Option.swift */; };
Expand All @@ -40,6 +42,8 @@
6CAD549C1D371A4E00A2D031 /* LinuxMain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LinuxMain.swift; path = Tests/LinuxMain.swift; sourceTree = SOURCE_ROOT; };
CD2ED3401C1E6C5D0076092B /* Argument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Argument.swift; sourceTree = "<group>"; };
CD2ED3421C1E6D540076092B /* ArgumentProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ArgumentProtocol.swift; sourceTree = "<group>"; };
CDCE78331FBAB047005A9F76 /* OrderedSet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderedSet.swift; sourceTree = "<group>"; };
CDCE78351FBAB0A2005A9F76 /* OrderedSetSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OrderedSetSpec.swift; sourceTree = "<group>"; };
CDFC88351C3C0612003AC8F8 /* CommandSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CommandSpec.swift; sourceTree = "<group>"; };
D00CCDD91A20717400109F8C /* Commandant.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Commandant.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D00CCDDD1A20717400109F8C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -145,6 +149,7 @@
CDFC88351C3C0612003AC8F8 /* CommandSpec.swift */,
D00CCE281A20741C00109F8C /* OptionSpec.swift */,
680759631FC5A1F600D6CA70 /* OptionsWithEnumProtocolSpec.swift */,
CDCE78351FBAB0A2005A9F76 /* OrderedSetSpec.swift */,
D00CCDE91A20717400109F8C /* Supporting Files */,
6CAD549C1D371A4E00A2D031 /* LinuxMain.swift */,
);
Expand Down Expand Up @@ -237,6 +242,7 @@
D00CCE261A20741300109F8C /* Command.swift */,
D00CCE2A1A20748500109F8C /* Errors.swift */,
D00CCE2E1A2075F700109F8C /* Option.swift */,
CDCE78331FBAB047005A9F76 /* OrderedSet.swift */,
D8169D861ACB942D00923FB0 /* Switch.swift */,
);
name = Core;
Expand Down Expand Up @@ -361,6 +367,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
CDCE78341FBAB047005A9F76 /* OrderedSet.swift in Sources */,
CD2ED3411C1E6C5D0076092B /* Argument.swift in Sources */,
D0BF14FB1A4C8957003147BC /* HelpCommand.swift in Sources */,
CD2ED3431C1E6D540076092B /* ArgumentProtocol.swift in Sources */,
Expand All @@ -377,8 +384,9 @@
buildActionMask = 2147483647;
files = (
CDFC88361C3C0612003AC8F8 /* CommandSpec.swift in Sources */,
680759661FC5B39600D6CA70 /* OptionSpec.swift in Sources */,
D00CCE291A20741C00109F8C /* OptionSpec.swift in Sources */,
680759651FC5A25D00D6CA70 /* OptionsWithEnumProtocolSpec.swift in Sources */,
CDCE78361FBAB0A2005A9F76 /* OrderedSetSpec.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions Sources/Commandant/ArgumentParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private enum RawArgument: Equatable {
case value(String)

/// One or more flag arguments (e.g 'r' and 'f' for `-rf`)
case flag(Set<Character>)
case flag(OrderedSet<Character>)
}

private func ==(lhs: RawArgument, rhs: RawArgument) -> Bool {
Expand Down Expand Up @@ -72,7 +72,7 @@ public final class ArgumentParser {
if opt.first == "-" {
return .key(String(opt.dropFirst()))
} else {
return .flag(Set(opt))
return .flag(OrderedSet(opt))
}
} else {
return .value(arg)
Expand Down
51 changes: 51 additions & 0 deletions Sources/Commandant/OrderedSet.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/// A poor man's ordered set.
internal struct OrderedSet<T: Hashable> {
fileprivate var values: [T] = []

init<S: Sequence>(_ sequence: S) where S.Element == T {
for e in sequence where !values.contains(e) {
values.append(e)
}
}

@discardableResult
mutating func remove(_ member: T) -> T? {
if let index = values.index(of: member) {
return values.remove(at: index)
} else {
return nil
}
}
}

extension OrderedSet: Equatable {
static func == (_ lhs: OrderedSet, rhs: OrderedSet) -> Bool {
return lhs.values == rhs.values
}
}

extension OrderedSet: Collection {
subscript(position: Int) -> T {
return values[position]
}

var count: Int {
return values.count
}

var isEmpty: Bool {
return values.isEmpty
}

var startIndex: Int {
return values.startIndex
}

var endIndex: Int {
return values.endIndex
}

func index(after i: Int) -> Int {
return values.index(after: i)
}
}
22 changes: 22 additions & 0 deletions Tests/CommandantTests/OrderedSetSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@testable import Commandant
import Foundation
import Quick
import Nimble

class OrderedSetSpec: QuickSpec {
override func spec() {
describe("OrderedSet") {
it("should remove duplicate entries") {
let input = ["a", "c", "b", "a"]
let output = OrderedSet(input)
expect(output.count) == 3
}

it("should preserve the order of the given input") {
let input = ["a", "c", "b", "a"]
expect(Set(input).joined()) != "acb"
expect(OrderedSet(input).joined()) == "acb"
}
}
}
}
3 changes: 2 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import Quick
Quick.QCKMain([
CommandWrapperSpec.self,
OptionsProtocolSpec.self,
OptionsWithEnumProtocolSpec.self
OrderedSetSpec.self,
OptionsWithEnumProtocolSpec.self,
])

0 comments on commit bcef806

Please sign in to comment.