Creating Command Line Applications Using sbt

There are several components of sbt that may be used to create a command
line application. The launcher and the
command system are the two main ones illustrated here.

As described on the launcher page, a
launched application implements the xsbti.AppMain interface and defines
a brief configuration file that users pass to the launcher to run the
application. To use the command system, an application sets up a
State instance that provides
command implementations and the initial commands to run. A
minimal hello world example is given below.

Hello World Example

There are three files in this example:

build.sbt

Main.scala

hello.build.properties

To try out this example:

Put the first two files in a new directory

In sbt’s shell run publishLocal in that directory

Run sbt @path/to/hello.build.properties to run the application.

Like for sbt itself, you can specify commands from the command line
(batch mode) or run them at an prompt (interactive mode).

Build Definition: build.sbt

The build.sbt file should define the standard settings: name, version,
and organization. To use the sbt command system, a dependency on the
command module is needed. To use the task system, add a dependency on
the task-system module as well.

Application: Main.scala

The application itself is defined by implementing
xsbti.AppMain. The basic steps are

Provide command definitions. These are the commands that are
available for users to run.

Define initial commands. These are the commands that are initially
scheduled to run. For example, an application will typically add
anything specified on the command line (what sbt calls batch mode)
and if no commands are defined, enter interactive mode by running
the ‘shell’ command.

Set up logging. The default setup in the example rotates the log
file after each user interaction and sends brief logging to the
console and verbose logging to the log file.