loathers / grimoire

Apache License 2.0
3 stars 9 forks source link

Allow delayed defaults in Args #121

Open horrible-little-slime opened 1 month ago

horrible-little-slime commented 1 month ago

I have a draft PR to address this, but I feel like I probably did it wrong. What I'd like is to be able to assign a default value to a pref in such a way that it's not actually evaluated until Args.fill is called--the main use case is to prevent mallsearches in garbo before we do our big mallPrices("allitems") but I'm sure there are other use cases

Kasekopf commented 1 month ago

Hmm, interesting idea. Some things I'll have to think about:

Typing

Right now the presence of default changes the type of the generated argument:

Probably an argument with a delayed default would have to allow undefined, and be initialized to undefined before the args are filled.

Help

The help text currently displays the default value if given. I guess delayed defaults should show nothing in the help? Or maybe it is better to compute it and show the computed value.

--

As a workaround, I will also note that the args object is an entirely normal and mutable object (just with some hidden fields). So it should work fine to do something like:

Args.fill(args, command);
if (!args.target) args.target = defaultTarget();

I suppose this is modifying a global, and defined further away from the other arg definitions, so not ideal.