From 86ba6bce7e172c84fe49ec6256fc5636b4654053 Mon Sep 17 00:00:00 2001 From: Valentin Knabel Date: Sun, 18 Oct 2015 17:24:41 +0200 Subject: [PATCH 1/2] Added second name and flag parsing as false to Flag --- Commander/ArgumentDescription.swift | 31 ++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/Commander/ArgumentDescription.swift b/Commander/ArgumentDescription.swift index 617c470..3713f4a 100644 --- a/Commander/ArgumentDescription.swift +++ b/Commander/ArgumentDescription.swift @@ -122,35 +122,52 @@ public class Options : ArgumentDescriptor { public class Flag : ArgumentDescriptor { public typealias ValueType = Bool - + public let name:String + public let falseName:String? public let flag:Character? + public let falseFlag:Character? public let description:String? public let `default`:ValueType public var type:ArgumentType { return .Option } - - public init(_ name:String, flag:Character? = nil, description:String? = nil, `default`:Bool = false) { + + public init(_ name:String, falseName:String? = nil, flag:Character? = nil, falseFlag:Character? = nil, description:String? = nil, `default`:Bool = false) { self.name = name + self.falseName = falseName self.flag = flag + self.falseFlag = falseFlag self.description = description self.`default` = `default` } - + public func parse(parser:ArgumentParser) throws -> ValueType { if parser.hasOption("no-\(name)") { return false + } else if let falseName = falseName { + if parser.hasOption("no-\(falseName)") { + return true + } } - + if parser.hasOption(name) { return true + } else if let falseName = falseName { + if parser.hasOption(falseName) { + return false + } } - + if let flag = flag { if parser.hasFlag(flag) { return true } } - + if let falseFlag = falseFlag { + if parser.hasFlag(falseFlag) { + return false + } + } + return `default` } } From 217b9188d8b6e2d4376938a8025d5c4b483839ce Mon Sep 17 00:00:00 2001 From: Valentin Knabel Date: Tue, 3 Nov 2015 11:27:37 +0100 Subject: [PATCH 2/2] Applied required changes for merge request. --- Commander/ArgumentDescription.swift | 36 +++++++++++------------------ 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/Commander/ArgumentDescription.swift b/Commander/ArgumentDescription.swift index 3713f4a..4407db8 100644 --- a/Commander/ArgumentDescription.swift +++ b/Commander/ArgumentDescription.swift @@ -122,52 +122,44 @@ public class Options : ArgumentDescriptor { public class Flag : ArgumentDescriptor { public typealias ValueType = Bool - + public let name:String - public let falseName:String? public let flag:Character? - public let falseFlag:Character? + public let disabledName:String + public let disabledFlag:Character? public let description:String? public let `default`:ValueType public var type:ArgumentType { return .Option } - - public init(_ name:String, falseName:String? = nil, flag:Character? = nil, falseFlag:Character? = nil, description:String? = nil, `default`:Bool = false) { + + public init(_ name:String, flag:Character? = nil, disabledName:String? = nil, disabledFlag:Character? = nil, description:String? = nil, `default`:Bool = false) { self.name = name - self.falseName = falseName + self.disabledName = disabledName ?? "no-\(name)" self.flag = flag - self.falseFlag = falseFlag + self.disabledFlag = disabledFlag self.description = description self.`default` = `default` } - + public func parse(parser:ArgumentParser) throws -> ValueType { - if parser.hasOption("no-\(name)") { + if parser.hasOption(disabledName) { return false - } else if let falseName = falseName { - if parser.hasOption("no-\(falseName)") { - return true - } } - + if parser.hasOption(name) { return true - } else if let falseName = falseName { - if parser.hasOption(falseName) { - return false - } } - + if let flag = flag { if parser.hasFlag(flag) { return true } } - if let falseFlag = falseFlag { - if parser.hasFlag(falseFlag) { + if let disabledFlag = disabledFlag { + if parser.hasFlag(disabledFlag) { return false } } - + return `default` } }