ppx_getenv icon indicating copy to clipboard operation
ppx_getenv copied to clipboard

Add getenv_strict variant

Open rizo opened this issue 1 year ago • 4 comments

Hi! I often find the getenv ppx very useful, but one thing that is missing from it is the ability to statically fail when the environment variable is not set.

This implements a new extension getenv_strict that encountering a missing environment variable will fail with:

File "src_test/test_ppx_getenv.ml", line 5, characters 36-55:
5 |   assert_equal "42" [%getenv_strict "PPX_GETENV_CHECK_MISSING"]
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^
Error: [%getenv_strict] not set: PPX_GETENV_CHECK_MISSING

If you think this is a good addition, I'm happy to document this in the README and add a test case.

rizo avatar Jul 05 '24 12:07 rizo

If you think this is a good addition, I'm happy to document this in the README and add a test case.

sorry for the lack of feedback. Yes i personally think it's a good addition. If you could add these i should be able to merge and make a release

kit-ty-kate avatar Nov 30 '24 11:11 kit-ty-kate

Thanks for looking into this @kit-ty-kate! I was just thinking about this PR last week and remembered it could be useful to also have a variant that uses a default value if the var lookup fails.

Maybe we could do something like this?

[%getenv "SOMETHING" {default="foo"}]
[%getenv "SOMETHING" {strict=true}]

Not sure if this is the most appropriate syntax. Let me know what you think.

rizo avatar Dec 02 '24 11:12 rizo

The syntax looks good. The default option sounds nice, maybe it could take either a string or None, in the latter case it would have the same behaviour as Sys.getenv_opt

kit-ty-kate avatar Dec 02 '24 12:12 kit-ty-kate

Great, I'll try to get this done soon.

rizo avatar Dec 02 '24 12:12 rizo