Skip to content

Instantly share code, notes, and snippets.

@kenbod
Last active August 29, 2015 14:25
Show Gist options
  • Save kenbod/7146f46ae05205176fbe to your computer and use it in GitHub Desktop.
Save kenbod/7146f46ae05205176fbe to your computer and use it in GitHub Desktop.

Introduction

Process.scala is my attempt at creating an application skeleton for a command-line application that has to process (in this case) a single JSON file.

I'm new to Scala and would love feedback on the proper way to do this.

Design Comments

  • I know I should have a package statement at the top. This is just test code for now.
  • For a simple application that just processes one or two inputs, I'm not interested in using a third-party package to parse command-line arguments. Pointers are appeciated, however, especially to routines present in the libraries that ship with Scala.
  • As you can see, this template does not actually process the input file. That's a feature, not a bug.
import java.nio.file.{Paths,Files}
object Process {
def fileExists(path: String): Boolean = {
Files.exists(Paths.get(path))
}
def validInput(args: Array[String]): Boolean = {
val onlyOne = args.length match {
case 1 => true
case _ => false
}
val validExtension = args(0) matches ".*[.]json"
onlyOne && validExtension && fileExists(args(0))
}
def main (args: Array[String]) = {
val isValid = validInput(args)
if (!isValid) {
println("Usage: scala Process.scala <input.json>")
System.exit(1)
}
val inputFile = args(0)
println(s"Input File: $inputFile")
System.exit(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment