|
1 | 1 | package stringcalc
|
2 | 2 |
|
3 |
| -import java.nio.file.Files |
4 |
| -import replpp.{Config, InteractiveShell} |
| 3 | +import java.nio.file.{Files, Path} |
| 4 | +import scopt.OParser |
5 | 5 |
|
6 |
| -object Main { |
7 |
| - @main def startRepl(): Unit = { |
8 |
| - val predefFileTmp = Files.createTempFile("scala-repl-pp-demo-project-predef", ".sc") |
9 |
| - Files.writeString( |
10 |
| - predefFileTmp, |
11 |
| - s"""import stringcalc._ |
12 |
| - |import StringCalculator._ |
13 |
| - | |
14 |
| - |def help: Unit = println("try this: `add(One, Two)`") |
15 |
| - """.stripMargin |
16 |
| - ) |
17 |
| - |
18 |
| - InteractiveShell.run( |
19 |
| - Config( |
20 |
| - prompt = Some("stringcalc"), |
21 |
| - greeting = "Welcome to the magical world of string calculation! \nType `help` for help", |
22 |
| - predefFiles = Seq(predefFileTmp), |
| 6 | +def main(args: Array[String]) = { |
| 7 | + Config.parse(args) match { |
| 8 | + case Some(config) => |
| 9 | + replpp.Main.run( |
| 10 | + replpp.Config( |
| 11 | + scriptFile = config.scriptFile, |
| 12 | + prompt = Some("stringcalc"), |
| 13 | + greeting = Some("Welcome to the magical world of string calculation! \nType `help` for help"), |
| 14 | + verbose = config.verbose, |
| 15 | + runBefore = Seq( |
| 16 | + "import stringcalc.*", |
| 17 | + "import StringCalculator.*", |
| 18 | + """def help: Unit = println("try this: `add(One, Two)`")""" |
| 19 | + ) |
| 20 | + ) |
23 | 21 | )
|
| 22 | + case None => System.exit(1) |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +case class Config(verbose: Boolean = false, scriptFile: Option[Path] = None) |
| 27 | +object Config { |
| 28 | + def parse(args: Array[String]): Option[Config] = |
| 29 | + OParser.parse(parser, args, Config()) |
| 30 | + |
| 31 | + private val builder = OParser.builder[Config] |
| 32 | + private val parser = { |
| 33 | + import builder._ |
| 34 | + OParser.sequence( |
| 35 | + programName("stringcalc"), |
| 36 | + opt[Boolean]('v', "verbose") |
| 37 | + .action((x, c) => c.copy(verbose = x)) |
| 38 | + .text("enable verbose mode"), |
| 39 | + opt[Path]("script") |
| 40 | + .action((x, c) => c.copy(scriptFile = Option(x))) |
| 41 | + .text("path to script file") |
| 42 | + .validate(path => |
| 43 | + if (Files.exists(path)) success |
| 44 | + else failure(s"script file $path does not exist") |
| 45 | + ) |
24 | 46 | )
|
25 | 47 | }
|
26 | 48 | }
|
0 commit comments