Hi @erhan,
For the specific case of logging, my advice is to use the Logs library or – failing that – to just copy the approach that it takes verbatim. Logs
looks after some global mutable state that contains the current logging level of the program, so you don’t have to bother with propagating this information throughout your program. When I’m reading OCaml code, I’m not concerned with whether any particular function might emit log lines, so I don’t need this to be made painfully obvious at each call-site. If you do use Logs
, it comes pre-packaged with Cmdliner specifications for setting the logging level in the logs.cli
package (example here).
Generally, there are several options for propagating state throughout an OCaml program. In roughly decreasing order of explicitness:
-
pass all params explicitly to the functions that need them, precisely as you’re doing right now.
-
pack params into a “context” record (or object) that is passed explicitly where it’s needed. (c.f. Dune’s
Context
andSuper_context
.) It’s possible to hide this record passing in aReader
monad, which I have toyed with in the past but don’t recommend with today’s OCaml. -
pack params into a “context” module that is then used to instantiate functors elsewhere in your program. (c.f.
Ppxlib.Ast_builder
as a way of propagating a~loc
flag everywhere.) -
use global mutable state, as in
Logs
.
I’ve seen all four of these used sensibly in OCaml programs; the best one will depend on your particular application requirements / how much you care about tracking which parts of the program use which arguments.
Regarding your second point, AFAIK there’s no generic library for managing config files in OCaml (i.e. what Cosmiconfig provides for NPM). Every OCaml library that I’ve seen that uses one tends to roll their own logic for it. You could of course use Yojson or OCaml-Yaml to read a file in one of those formats, but you’ll end up managing the details yourself. The lightweight approach is to use environment variables, since Cmdliner will handle that boilerplate for you; managing config files is a pain, particularly w.r.t. things like respecting XDG_CONFIG
and it’s analogues on Windows.