Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boolean handling is confusing #231

Open
balhoff opened this issue Oct 2, 2020 · 5 comments
Open

boolean handling is confusing #231

balhoff opened this issue Oct 2, 2020 · 5 comments

Comments

@balhoff
Copy link

balhoff commented Oct 2, 2020

If an option type is boolean, and a default value of true is provided, there doesn't seem to be a way to set it to false on the command line:

[info] Starting scala interpreter...
Welcome to Scala 2.13.3 (Java HotSpot(TM) 64-Bit Server VM, Java 11.0.4).
Type in expressions for evaluation. Or try :help.

scala> import caseapp._
import caseapp._

scala> case class Options(enableFoo: Boolean = false)
class Options

scala> CaseApp.parse[Options](Seq())
val res0: Either[caseapp.core.Error,(Options, Seq[String])] = Right((Options(false),List()))

scala> CaseApp.parse[Options](Seq("--enable-foo", "true"))
val res2: Either[caseapp.core.Error,(Options, Seq[String])] = Right((Options(true),List(true)))

scala> CaseApp.parse[Options](Seq("--enable-foo", "false"))
val res3: Either[caseapp.core.Error,(Options, Seq[String])] = Right((Options(true),List(false)))

scala> case class Options2(enableFoo: Boolean = true)
class Options2

scala> CaseApp.parse[Options2](Seq("--enable-foo", "false"))
val res5: Either[caseapp.core.Error,(Options2, Seq[String])] = Right((Options2(true),List(false)))

scala> CaseApp.parse[Options2](Seq())
val res6: Either[caseapp.core.Error,(Options2, Seq[String])] = Right((Options2(true),List()))

For the case of res3 above, I would expect to be able to explicitly pass false, although I can get a false value by not providing the argument at all (e.g. res0). Unexpectedly --enable-foo false sets the value to true, because "false" is treated as an extra argument.

For the case of the default true value (Options2), there doesn't seem to be any command line that can set it to false.

@balhoff
Copy link
Author

balhoff commented Oct 2, 2020

I did want to add that I've used several Scala command line parsers, and case-app is easily my favorite—thanks!

@alexarchambault
Copy link
Owner

alexarchambault commented Oct 5, 2020

@balhoff It is possible to pass an explicit value to boolean options, like --enable-foo=true or --enable-foo=false.

@balhoff
Copy link
Author

balhoff commented Oct 5, 2020

@alexarchambault thank you for pointing that out, I completely missed that it was possible to use = between options and values. Based on the examples in the readme I thought a space was required. To me it's a little confusing that this works differently between, e.g., Boolean and String values. Is it safe then to use = for all arguments?

@alexarchambault
Copy link
Owner

Yes, = can be used for all options. = is optional for options requiring a value, such as string options. For options accepting an optional value (it's only booleans IIRC), the = is mandatory if you want to pass a value.

@balhoff
Copy link
Author

balhoff commented Oct 5, 2020

Thanks! I'll leave it up to you if you want to go ahead and close this. It would be great to add that to the docs if it isn't somewhere already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants