Skip to content

Commit

Permalink
(docs) Cookbook "Validate an Email Address" with re (#2407)
Browse files Browse the repository at this point in the history
* Cookbook getenv\n\nRecreate PR #2383

* Update data/cookbook/read-environment-variable/00-stdlib.ml

* Apply suggestions from code review

---------

Co-authored-by: Cuihtlauac ALVARADO <[email protected]>
Co-authored-by: sabine <[email protected]>
  • Loading branch information
3 people authored Jun 17, 2024
1 parent cc34b73 commit 12d358d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions data/cookbook/read-environment-variable/00-stdlib.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
packages: []
---

(*
Both `Sys.getenv` and `Sys.getenv_opt` are functions that take the name of an environment and read its value.
`Sys.getenv` returns the value directly, but raises a `Not_found` exception if the variable doesn't exist.
*)
let path = Sys.getenv "PATH"
let () =
try
Printf.printf "The path is %s\n" path
with Not_found ->
print_string "Api key is not set.\n"

(*
In contrast, `Sys.getenv_opt` returns a value of type `string option`: `Some value` if the variable exists and `None` if it doesn't.
*)
let () =
match Sys.getenv_opt "API_KEY" with
| Some p ->
Printf.printf "Api key is %s\n" p
| None ->
print_string "Api key is not set.\n"

0 comments on commit 12d358d

Please sign in to comment.