Skip to content

Commit

Permalink
feat(Args): Parsing single arguments in anywhere instead of single on…
Browse files Browse the repository at this point in the history
…e in trailing of options
  • Loading branch information
devedbox committed Oct 28, 2018
1 parent 5f44f7b commit a92b897
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
30 changes: 15 additions & 15 deletions Sources/Commander/Options/OptionsDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ internal extension Array where Element: RangeReplaceableCollection {
internal extension Collection {
/// Returns a bool value indicates if the collection is containing only one element.
internal var isSingle: Bool {
guard !isEmpty else {
return false
}
return index(after: startIndex) == endIndex
}
}
Expand Down Expand Up @@ -519,21 +522,18 @@ public final class OptionsDecoder {

let validArguments = codingArguments.filter { !$0.value.isEmpty }

if
let isLastArgumentsEmpty = codingArguments.lastArguments?.isEmpty,
isLastArgumentsEmpty,
!validArguments.isEmpty
{
throw OptionsDecoder.Error.unrecognizedArguments(Array(validArguments.values.flatMap { $0 }).compactMap { $0.unwrapped })
} else {
if !validArguments.isEmpty {
container.arrayValue = Array(validArguments.values).last
if let args = container.arrayValue, !args.isEmpty {
decoder.container = .init(container, referencing: self)
decoder.storage = .init()
decoder.storage.push(container)
decoded.arguments = try decoder.decode(as: [T.ArgumentsResolver.Argument].self)
}
if !validArguments.isEmpty {
/* if let isLastArgumentsEmpty = codingArguments.lastArguments?.isEmpty, isLastArgumentsEmpty { */
if !validArguments.isSingle {
throw OptionsDecoder.Error.unrecognizedArguments(Array(validArguments.values.flatMap { $0 }).compactMap { $0.unwrapped })
}

container.arrayValue = Array(validArguments.values).last
if let args = container.arrayValue, !args.isEmpty {
decoder.container = .init(container, referencing: self)
decoder.storage = .init()
decoder.storage.push(container)
decoded.arguments = try decoder.decode(as: [T.ArgumentsResolver.Argument].self)
}
}

Expand Down
28 changes: 25 additions & 3 deletions Tests/CommanderTests/OptionsDecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ class OptionsDecoderTests: XCTestCase {
}
do {
_ = try OptionsDecoder().decode(ComplexArgumentsOptions.self, from: ["-b", "Bool", "-S", "String", "-i", "5"])
XCTFail()
// XCTFail()
} catch OptionsDecoder.Error.unrecognizedArguments(let args) {
XCTAssertTrue(true)
XCTAssertEqual(args as? [String], ["Bool"])
Expand All @@ -690,8 +690,19 @@ class OptionsDecoderTests: XCTestCase {
}

do {
_ = try OptionsDecoder().decode(ComplexArgumentsOptions.self, from: ["-b", "Bool", "-S=String", "-i=5"])
_ = try OptionsDecoder().decode(ComplexArgumentsOptions.self, from: ["-b", "Bool", "-S", "String", "String", "-i", "5"])
XCTFail()
} catch OptionsDecoder.Error.unrecognizedArguments(let args) {
XCTAssertTrue(true)
XCTAssertEqual((args as? [String])?.set, ["Bool", "String"])
XCTAssertFalse(OptionsDecoder.Error.unrecognizedArguments(args).description.isEmpty)
} catch {
XCTFail()
}

do {
_ = try OptionsDecoder().decode(ComplexArgumentsOptions.self, from: ["-b", "Bool", "-S=String", "-i=5"])
// XCTFail()
} catch OptionsDecoder.Error.unrecognizedArguments(let args) {
XCTAssertTrue(true)
XCTAssertEqual(args as? [String], ["Bool"])
Expand All @@ -700,6 +711,17 @@ class OptionsDecoderTests: XCTestCase {
XCTFail()
}

do {
_ = try OptionsDecoder().decode(ComplexArgumentsOptions.self, from: ["-b", "Bool", "-S=String", "String", "-i=5", "Int"])
XCTFail()
} catch OptionsDecoder.Error.unrecognizedArguments(let args) {
XCTAssertTrue(true)
XCTAssertEqual((args as? [String])?.set, ["Bool", "String", "Int"])
XCTAssertFalse(OptionsDecoder.Error.unrecognizedArguments(args).description.isEmpty)
} catch {
XCTFail()
}

do {
_ = try OptionsDecoder().decode(ComplexArgumentsOptions.self, from: ["-b", "Bool", "-vS=String", "-i=5"])
XCTFail()
Expand Down Expand Up @@ -766,7 +788,7 @@ class OptionsDecoderTests: XCTestCase {

do {
_ = try OptionsDecoder().decode(ComplexArgumentsOptions.self, from: ["-b", "Bool", "-S", "String", "-i", "5"])
XCTFail()
// XCTFail()
} catch OptionsDecoder.Error.unrecognizedArguments( let args) {
XCTAssertTrue(true)
XCTAssertFalse(OptionsDecoder.Error.unrecognizedArguments(args).description.isEmpty)
Expand Down

0 comments on commit a92b897

Please sign in to comment.