Idea: support defaults in Opt functions
Closed this issue · 6 comments
It would be nice to be able to supply a default value in the Opt functions directly to avoid passing around and unwrapping a result directly. This would allow the following:
Cli.build {
inputDir: <-
Opt.str {
short: "i",
long: "input-directory",
help: "The directory containing the templates to be compiled",
default: ".",
},
}
instead of having to use Opt.maybeStr
and defaulting the result manually.
What do you think @smores56?
I totally agree that this would be preferred, just not sure how to do it.
The above approach doesn't work IMO. Optional record values need to be resolvable by a value if they aren't provided. So if we want Opt.str
to be able to take a default value or not, we either need to default to some sentinel value that when detected would mean a default was not passed. (We need to know whether a default was passed to determine whether we should error if a value was passed or not.) What if someone wants to use this default value? You couldn't use an empty string as a default. That makes this a weak option for me.
Maybe we can use a tag union. We can always default to a tag called NoDefault
, and the default's type is Value T
. We'd want a third variant like Generate ({} -> T)
A bit clunky, but doesn't have the "cursed value" issue that the first option does.
We could also provide a Arg.withDefault
function that does more clearly what I've done in the examples/
dir. You would call Opt.str { ... } |> Arg.withDefault "abc"
, or maybe Opt.generateDefault \{} -> "abc"
for a thunk version. It's more generic/versatile interface, but is more verbose as well.
Do you have a preference between the two? I'm not sure myself.
The above approach doesn't work IMO.
Ah yes good point!
I like the tag approach, it seems simple and easily discoverable. I also like that it allows for a simple approach to using a thunks.
Okay, then let me make a branch and try it out. If I like how it works, I'll make a PR and ping you. Heads up, I'm about to go to Japan for a week, so even though I'll have a laptop, I'm not sure how long it'll take.
Sounds great. Enjoy your trip!
Thanks for doing this!