Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Deal with empty arguments in Args.

  • Loading branch information...
commit 9fa0aca887b347da1a732e236dbdaebb0550afdb 1 parent 52b87a3
@azymnis azymnis authored
View
19 src/main/scala/com/twitter/scalding/Args.scala
@@ -34,14 +34,17 @@ object Args {
def startingDashes(word : String) = word.takeWhile { _ == '-' }.length
new Args(
//Fold into a list of (arg -> List[values])
- args.foldLeft(List("" -> List[String]())){(acc, arg) =>
- val noDashes = arg.dropWhile{ _ == '-'}
- if(arg == noDashes)
- (acc.head._1 -> (arg :: acc.head._2)) :: acc.tail
- else
- (noDashes -> List()) :: acc
- }. //Now reverse the values to keep the same order
- map {case (key, value) => key -> value.reverse}.toMap
+ args
+ .filter{ _ != "" }
+ .foldLeft(List("" -> List[String]())) { (acc, arg) =>
+ val noDashes = arg.dropWhile{ _ == '-'}
+ if(arg == noDashes)
+ (acc.head._1 -> (arg :: acc.head._2)) :: acc.tail
+ else
+ (noDashes -> List()) :: acc
+ }
+ //Now reverse the values to keep the same order
+ .map {case (key, value) => key -> value.reverse}.toMap
)
}
}
View
8 src/test/scala/com/twitter/scalding/ArgTest.scala
@@ -30,6 +30,14 @@ class ArgTest extends Specification {
map.required("three") must be_==("3")
map.optional("three") must be_==(Some("3"))
}
+ "remove empty args in lists" in {
+ val map = Args(Array("", "hello", "--one", "1", "", "--two", "2", "", "3"))
+ map("") must be_==("hello")
+ map.list("") must be_==(List("hello"))
+ map("one") must be_==("1")
+ map.list("one") must be_==(List("1"))
+ map.list("two") must be_==(List("2", "3"))
+ }
"put initial args into the empty key" in {
val map =Args(List("hello", "--one", "1"))
map("") must be_==("hello")
Please sign in to comment.
Something went wrong with that request. Please try again.