Skip to content

Instantly share code, notes, and snippets.

@Pablo-Leon
Created May 28, 2016 20:01
Show Gist options
  • Save Pablo-Leon/1d482116ab282e0d28a3772ec831af39 to your computer and use it in GitHub Desktop.
Save Pablo-Leon/1d482116ab282e0d28a3772ec831af39 to your computer and use it in GitHub Desktop.
#!/bin/sh
unset LEVEL
_x4563="$@" # don't have a clue why "$@" does not work on next line
if expr "$_x4563" : '--trace *$\|--trace \|.* --trace$\|.* --trace '>/dev/null ; then LEVEL='DEBUG'; fi
prog=`basename $0`
exec scala \
-save \
-classpath "$HOME/lib/scopt_2.11-3.4.0.jar:$HOME/lib/slf4j-api-1.7.21.jar:$HOME/lib/logback-classic-1.1.7.jar:$HOME/lib/logback-core-1.1.7.jar" \
-Dprogname=${prog} \
-Dlogback.configurationFile=$HOME/lib/logback.xml \
-Dlog.level=${LEVEL} \
"$0" "$@"
!#
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scopt._
import scala.io.Source
import java.io.File
val script="neu_script.scala"
// object HelloSbt extends App {
// *** would need to call main() in a script
// }
val logger = LoggerFactory.getLogger(script)
case class Config(
trace: Boolean = false
// ,numero: Int = -1
// ,cadena: String = "(default)"
,files: Seq[File] = Seq()
)
val parser = new scopt.OptionParser[Config](script) {
head(script, "0.1")
help("help") text("prints this usage text")
// opt[Int]('n', "numero") action { (x, c) =>
// c.copy(numero = x) } text("foo is an integer property")
// opt[String]('c', "cadena") action { (x, c) =>
// c.copy(cadena = x) } text("cadena is an string property")
// opt[Unit]("flag") action { (_, c) =>
// c.copy(flag = true) } text("this option is not hidden in the usage text")
opt[Unit]("trace") hidden() action { (x, c) =>
c.copy(trace = true) } text("this option is hidden in the usage text")
arg[File]("<file>...") unbounded() optional() action { (x, c) =>
c.copy(files = c.files :+ x) } text("optional unbounded args")
// note("...")
}
logger.info("Start")
// parser.parse returns Option[C]
val result = parser.parse(args, Config()) map { config =>
logger.debug("trace:[" + config.trace + "] "
+ "files:[" + config.files + "] ")
// do stuff
config.files.foreach( f => for (line <- Source.fromFile(f).getLines) {
println(line)
})
} getOrElse {
// arguments are bad, usage message will have been displayed
}
logger.info("Finish")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment