harryg
harryg

Reputation: 24107

Getting started with a SBT project

I have been given a skeleton SBT project to work on. The directory structure is as follows:

|-- build.sbt
|-- project
|   |-- build.properties
|   |-- plugins.sbt
|   |-- project
|   `-- target
|-- README.md
`-- src
    |-- main
    |   `-- scala
    |       `-- com
    |           `-- app-name
    |               |-- domain
    |               |-- exception
    |               |-- repository
    |               `-- util
    `-- test
        `-- scala
`-- Vagrantfile

The instructions are to create an app entry point which should take a single command line argument and run some logic.

I have managed to get a simple "hello world" sbt project working but I'm new to scala/sbt. Where would I place this entry point and how can I accept a command line argument?

Upvotes: 0

Views: 678

Answers (1)

Ruslan
Ruslan

Reputation: 2799

The root folder for source files would be src/main/scala.

Parameters are referenced using the args array within your entry point object.

The entry point is any object under that source tree which extends App. Since this is a hello world example and you're just getting started, I'd drop it right into the root of the sources (src/main/scala/MyApp.scala).

Something like this:

object MyApp extends App {
    println(args.length match {
        case 0 => "You passed in no arguments!"
        case 1 => s"You passed in 1 argument, which was ${args(0)}"
        case x => s"You passed in $x arguments! They are: ${args.mkString(",")}"
    })
}

To run your app, issue the sbt run command in the project root. To run with parameters, do sbt run "arg1".

Upvotes: 1

Related Questions