cli.scala
package app
import scala.scalajs.js
import js.{JSApp,|}
import js.Dynamic.{literal => lit}
import js.{Array => arr}
import js.annotation._
import scopt._
import io.scalajs.nodejs._
object Main extends JSApp {
case class AppConfig(
a: Option[String] = None,
b: Option[Int] = None)
val parser = new scopt.OptionParser[AppConfig]("cli-app") {
override def terminate(exitState: Either[String, Unit]): Unit = {
process.exit()
}
opt[String]('a', "a-arg").valueName("<arg>")
.text("Argument a")
.action((x,c) => c.copy(a = Option(x)))
opt[Int]('b', "b-arg").valueName("<int>")
.text("Argument b")
.action((x,c)=> c.copy(b = Option(x)))
help("help").text("cli application")
}
def main(): Unit = {
val config: AppConfig = parser.parse(process.argv.drop(2), AppConfig()) match {
case Some(c) => c // Ok!
case _ => process.exit(-1); return // return keeps type checker happy
}
import ContentFormatting._
val notProvided = notProvidedChalk(Messages.notProvidedMessage)
val output = Table.table(
arr(
arr(Chalk.bold("Parameter"), Chalk.bold("Value")),
arr("a", config.a.map(hasValue(_)).getOrElse(notProvided)),
arr("b", config.b.map(i => hasValue(i.toString)).getOrElse(notProvided))),
new TableOptions(
border = Table.getBorderCharacters("ramac")
))
println("Program arguments:")
println(output)
}
//
// FFI to JS libraries that do not have or we are not using a scala facade.
//
@js.native
@JSImport("table", JSImport.Namespace)
object Table extends js.Object {
def table(data: js.Array[js.Array[Any]], options: TableOptions): String = js.native
def getBorderCharacters(templateName: String): js.Dictionary[String] = js.native
}
@ScalaJSDefined
class TableOptions(
val columns: js.UndefOr[js.Dynamic] = js.undefined,
val columnDefault: js.UndefOr[js.Dynamic] = js.undefined,
val border: js.UndefOr[String|js.Dictionary[String]] = js.undefined,
val drawJoin: js.UndefOr[js.Function2[Int, Int, Unit]] = js.undefined
// ...
) extends js.Object
@ScalaJSDefined
class Column(
alignment: js.UndefOr[String] = js.undefined,
width: js.UndefOr[Int] = js.undefined,
truncate: js.UndefOr[Int] = js.undefined,
paddingLeft: js.UndefOr[Int] = js.undefined,
paddingRight: js.UndefOr[Int] = js.undefined,
wrapWord: js.UndefOr[Boolean] = js.undefined
) extends js.Object
@js.native
@JSImport("chalk", JSImport.Namespace)
object Chalk extends js.Object {
val supportsColor: Boolean = js.native
def enabled: Boolean = js.native
def enabled_=(v: Boolean): Unit = js.native
def bold: js.Dynamic = js.native
def bgRed: js.Dynamic = js.native
def white: js.Dynamic = js.native
def green: js.Dynamic = js.native
}
/**
* Colorize a string to alert user that a command line arg was not provided.
* This is an one-off function for formatting. Provided is replaced with
* another path specified in the webpack config.
*/
@js.native
@JSImport("Provided/formatting.js", JSImport.Default)
object notProvidedChalk extends js.Object {
def apply(arg: String): String = js.native
}
/** A set of messages to use.
* This shows js files next to scala files.
*/
@js.native
@JSImport("./messages.js", JSImport.Namespace)
object Messages extends js.Object {
val notProvidedMessage: String = js.native
}
/**
* This shows "module "style importing as if it
* was a node.js module. No relative path information
* in the "import".
*/
@js.native
@JSImport("ContentFormatting", JSImport.Namespace)
object ContentFormatting extends js.Object {
def hasValue(arg: String): String = js.native
}
}scalajs compilation
babel
Last updated