Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser for list of parameters #8

Merged
merged 14 commits into from
Feb 15, 2017
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ This framework also contains [helper methods for `Stencil.Extension` and `Stenci
This framework contains an additional parser, meant to parse a list of parameters from the CLI. For example, using [Commander](https://github.com/kylef/Commander), if you receive a `[String]` from a `VariadicOption<String>`, you can use the parser to convert it into a structured dictionary. For example:

```swift
["foo=1", "bar=2", "baz.qux=hello", "baz.items=a", "baz.items=b"]
["foo=1", "bar=2", "baz.qux=hello", "baz.items=a", "baz.items=b", "something"]
```

will become
Expand All @@ -67,6 +67,7 @@ will become
"a",
"b"
]
]
],
something: true
]
```
4 changes: 2 additions & 2 deletions Sources/Parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public enum Parameters {
public static func parse(items: [String]) throws -> StringDict {
let parameters: [Parameter] = try items.map { item in
let parts = item.components(separatedBy: "=")
if parts.count == 2 {
return (key: parts[0], value: parts[1])
if parts.count >= 2 {
return (key: parts[0], value: parts.dropFirst().joined(separator: "="))
} else if let part = parts.first, parts.count == 1 && validate(key: part) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean that foo.bar.baz isn't a valid boolean key? (as validate(key: "foo.bar.baz") will return false)?

return (key: part, value: true)
} else {
Expand Down
7 changes: 4 additions & 3 deletions Tests/StencilSwiftKitTests/ParametersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import StencilSwiftKit

class ParametersTests: XCTestCase {
func testBasic() {
let items = ["a=1", "b=hello", "c"]
let items = ["a=1", "b=hello", "c=x=y", "d"]
let result = try! Parameters.parse(items: items)

XCTAssertEqual(result.count, 3, "3 parameters should have been parsed")
XCTAssertEqual(result.count, 4, "4 parameters should have been parsed")
XCTAssertEqual(result["a"] as? String, "1")
XCTAssertEqual(result["b"] as? String, "hello")
XCTAssertEqual(result["c"] as? Bool, true)
XCTAssertEqual(result["c"] as? String, "x=y")
XCTAssertEqual(result["d"] as? Bool, true)
}

func testStructured() {
Expand Down