Skip to content

Commit

Permalink
Merge pull request #32 from quantifind/pierre/sumacdebuglog
Browse files Browse the repository at this point in the history
provide a --sumac.debugArgs argument that will list the final values
  • Loading branch information
Mortimerp9 committed May 2, 2014
2 parents 88e26e5 + e5a6ede commit 0a6a2db
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
6 changes: 6 additions & 0 deletions core/src/main/scala/com/quantifind/sumac/Args.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ trait Args extends ExternalConfig with Serializable {
parsedArgs.foreach { case (argAssignable, valueHolder) =>
argAssignable.setValue(valueHolder.value)
}
if(kvPairs.contains("sumac.debugArgs")) {
println("Sumac setup the following args:")
getArgs("").foreach { arg =>
println(s"\t--${arg.getName}\t${arg.getCurrentValue}")
}
}
if (validation)
runValidation()
}
Expand Down
31 changes: 19 additions & 12 deletions core/src/main/scala/com/quantifind/sumac/ArgumentParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,29 @@ import collection._

class ArgumentParser[T <: ArgAssignable] (val argHolders: Seq[T]) {
lazy val nameToHolder:Map[String,T] = (LinkedHashMap.empty ++ argHolders.map(a => a.getName -> a)).withDefault { arg =>
throw new FeedbackException("unknown option %s\n%s".format(arg, helpMessage))
throw new ArgException("unknown option %s\n%s".format(arg, helpMessage))
}

def parse(args: Array[String]): Map[T, ValueHolder[_]] = {
parse(ArgumentParser.argListToKvMap(args))
}

def parse(rawKvs: Map[String,String]): Map[T, ValueHolder[_]] = {
if (rawKvs.contains("help"))
if (rawKvs.contains("help")) {
throw new FeedbackException(helpMessage)
rawKvs.map{case(argName, argValue) =>
val holder = nameToHolder(argName)
val result = try {
ParseHelper.parseInto(argValue, holder.getType, holder.getCurrentValue) getOrElse {
throw new FeedbackException("don't know how to parse type: " + holder.getType)
}
rawKvs.collect {
case(argName, argValue) if !ArgumentParser.isReserved(argName) =>
val holder = nameToHolder(argName)
val result = try {
ParseHelper.parseInto(argValue, holder.getType, holder.getCurrentValue) getOrElse {
throw new FeedbackException("don't know how to parse type: " + holder.getType)
}
} catch {
case ae: ArgException => throw ae
case e: Throwable => throw new ArgException("Error parsing \"%s\" into field \"%s\" (type = %s)\n%s".format(argValue, argName, holder.getType, helpMessage), e)
}
} catch {
case ae: ArgException => throw ae
case e: Throwable => throw new ArgException("Error parsing \"%s\" into field \"%s\" (type = %s)\n%s".format(argValue, argName, holder.getType, helpMessage), e)
}
holder -> result
holder -> result
}
}

Expand All @@ -42,6 +44,11 @@ class ArgumentParser[T <: ArgAssignable] (val argHolders: Seq[T]) {
}

object ArgumentParser {

val reservedArguments = Seq("help", "sumac.debugArgs")

def isReserved(name: String) = reservedArguments.contains(name)

def apply[T <: ArgAssignable](argHolders: Traversable[T]) = {
// ignore things we don't know how to parse
new ArgumentParser(argHolders.toSeq.filter(t => ParseHelper.findParser(t.getType).isDefined))
Expand Down
11 changes: 11 additions & 0 deletions core/src/test/scala/com/quantifind/sumac/ArgumentParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ class ArgumentParserTest extends FunSuite with Matchers {
}
}

test("reserved arguments should be filtered") {
val c = SimpleClass("a", 0, 1.4, 2)
val fieldArgs = classOf[SimpleClass].getDeclaredFields.map{f => FieldArgAssignable("",f, c)}
val argParser = new ArgumentParser(fieldArgs)

val parsed = getSimpleNameToArgMap(argParser.parse(Array("--count", "5", "--dummy", "7.4e3", "--name", "ooga", "--sumac.debugArgs", "true")))

parsed should not contain key("sumac.debugArgs")

}

def getSimpleNameToArgMap(parsedArgs : Map[_ <: ArgAssignable, ValueHolder[_]]) = {
parsedArgs.map{kv => kv._1.getName -> kv._2.value}.toMap[String, Any]
}
Expand Down

0 comments on commit 0a6a2db

Please sign in to comment.