diff --git a/README.md b/README.md index bd7ebb6b..137fe0f2 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ import caseapp._ ```scala case class Options( user: Option[String], - enableFoo: Boolean, + enableFoo: Boolean = false, file: List[String] ) @@ -32,17 +32,22 @@ CaseApp.parse[Options]( -### Default values +### Required and optional arguments -Most primitive types have default values, like `0` for `Int`, or `false` -for `Double`. Options default to `None`, and sequences to `Nil`. -Full list in the [source](https://github.com/alexarchambault/case-app/blob/master/src/main/scala/caseapp/core/Default.scala). +All arguments are required by default. To define an optional argument simply +wrap its type into `Option[T]`. + +Optional arguments can also be defined by providing a default value. +There are two ways to do that: +- providing default value ad hoc in the case class definition +- defining default value for a type with [Default](https://github.com/alexarchambault/case-app/blob/master/src/main/scala/caseapp/core/Default.scala) +type class ```scala case class Options( user: Option[String], - enableFoo: Boolean, - file: List[String] + enableFoo: Boolean = false, + file: List[String] = Nil ) CaseApp.parse[Options](Seq()) == Right((Options(None, false, Nil), Seq.empty)) @@ -51,19 +56,6 @@ CaseApp.parse[Options](Seq()) == Right((Options(None, false, Nil), Seq.empty)) -Alternatively, default values can be manually specified, like -```scala -case class Options( - user: String = "default", - enableFoo: Boolean = true -) - -CaseApp.parse[Options](Seq()) == Right((Options("default", true), Seq.empty)) -``` - - - - ### Lists Some arguments can be specified several times on the command-line. These @@ -72,7 +64,7 @@ should be typed as lists, e.g. `file` in ```scala case class Options( user: Option[String], - enableFoo: Boolean, + enableFoo: Boolean = false, file: List[String] ) diff --git a/core/shared/src/main/scala/caseapp/core/Default.scala b/core/shared/src/main/scala/caseapp/core/Default.scala index fac15bdb..a0e87715 100644 --- a/core/shared/src/main/scala/caseapp/core/Default.scala +++ b/core/shared/src/main/scala/caseapp/core/Default.scala @@ -26,19 +26,7 @@ object Default extends PlatformDefaults { ): Default[CC] = Default.instance(gen.from(defaultOr.value(default.value()))) - implicit val unit: Default[Unit] = Default.instance(()) - implicit val int: Default[Int] = Default.instance(0) - implicit val long: Default[Long] = Default.instance(0L) - implicit val float: Default[Float] = Default.instance(0f) - implicit val double: Default[Double] = Default.instance(0d) - implicit val bigDecimal: Default[BigDecimal] = Default.instance(BigDecimal(0)) - implicit val boolean: Default[Boolean] = Default.instance(false) - implicit val counter: Default[Int @@ Counter] = Default.instance(Tag of 0) - implicit val string: Default[String] = Default.instance("", str => s""""$str"""") - implicit def option[T]: Default[Option[T]] = Default.instance[Option[T]](None) - implicit def list[T]: Default[List[T]] = Default.instance[List[T]](Nil) - } trait DefaultOr[L <: HList, D <: HList] { @@ -76,3 +64,17 @@ object DefaultOr extends LowPriorityDefaultOr { d :: tail(td) } } + +object Defaults { + implicit val unit: Default[Unit] = Default.instance(()) + implicit val int: Default[Int] = Default.instance(0) + implicit val long: Default[Long] = Default.instance(0L) + implicit val float: Default[Float] = Default.instance(0f) + implicit val double: Default[Double] = Default.instance(0d) + implicit val bigDecimal: Default[BigDecimal] = Default.instance(BigDecimal(0)) + implicit val boolean: Default[Boolean] = Default.instance(false) + implicit val counter: Default[Int @@ Counter] = Default.instance(Tag of 0) + implicit val string: Default[String] = Default.instance("", str => s""""$str"""") + + implicit def list[T]: Default[List[T]] = Default.instance[List[T]](Nil) +} diff --git a/core/shared/src/test/scala/caseapp/Tests.scala b/core/shared/src/test/scala/caseapp/Tests.scala index d01cf27f..5ccb8ffd 100644 --- a/core/shared/src/test/scala/caseapp/Tests.scala +++ b/core/shared/src/test/scala/caseapp/Tests.scala @@ -5,6 +5,7 @@ import org.scalatest._ class Tests extends FlatSpec with Matchers { import Definitions._ + import core.Defaults._ "A parser" should "parse no args" in { Parser[NoArgs].apply(Seq.empty) shouldEqual Right((NoArgs(), Seq.empty)) diff --git a/doc/README.md b/doc/README.md index d4028b7a..ebb1ce66 100644 --- a/doc/README.md +++ b/doc/README.md @@ -20,7 +20,7 @@ import caseapp._ ```tut:silent case class Options( user: Option[String], - enableFoo: Boolean, + enableFoo: Boolean = false, file: List[String] ) @@ -37,17 +37,22 @@ assert( ) ``` -### Default values +### Required and optional arguments -Most primitive types have default values, like `0` for `Int`, or `false` -for `Double`. Options default to `None`, and sequences to `Nil`. -Full list in the [source](https://github.com/alexarchambault/case-app/blob/master/src/main/scala/caseapp/core/Default.scala). +All arguments are required by default. To define an optional argument simply +wrap its type into `Option[T]`. + +Optional arguments can also be defined by providing a default value. +There are two ways to do that: +- providing default value ad hoc in the case class definition +- defining default value for a type with [Default](https://github.com/alexarchambault/case-app/blob/master/core/shared/src/main/scala/caseapp/core/Default.scala) +type class ```tut:silent case class Options( user: Option[String], - enableFoo: Boolean, - file: List[String] + enableFoo: Boolean = false, + file: List[String] = Nil ) CaseApp.parse[Options](Seq()) == Right((Options(None, false, Nil), Seq.empty)) @@ -59,22 +64,6 @@ assert( ) ``` -Alternatively, default values can be manually specified, like -```tut:silent -case class Options( - user: String = "default", - enableFoo: Boolean = true -) - -CaseApp.parse[Options](Seq()) == Right((Options("default", true), Seq.empty)) -``` - -```tut:invisible -assert( - CaseApp.parse[Options](Seq()) == Right((Options("default", true), Seq.empty)) -) -``` - ### Lists Some arguments can be specified several times on the command-line. These @@ -83,7 +72,7 @@ should be typed as lists, e.g. `file` in ```tut:silent case class Options( user: Option[String], - enableFoo: Boolean, + enableFoo: Boolean = false, file: List[String] )