From 7be3088f39942fc4d29274eafbb94ac14ff011cb Mon Sep 17 00:00:00 2001 From: Cuihtlauac ALVARADO Date: Mon, 6 May 2024 19:13:40 +0200 Subject: [PATCH 1/3] Cookbook getenv\n\nRecreate PR #2383 --- .../read-environment-variable/00-stdlib.ml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 data/cookbook/read-environment-variable/00-stdlib.ml diff --git a/data/cookbook/read-environment-variable/00-stdlib.ml b/data/cookbook/read-environment-variable/00-stdlib.ml new file mode 100644 index 0000000000..cf98513072 --- /dev/null +++ b/data/cookbook/read-environment-variable/00-stdlib.ml @@ -0,0 +1,16 @@ +--- +packages: [] +discussion: | + - **Understanding `Sys.getenv` and `Sys.getenv_opt`:** Both `Sys.getenv` and `Sys.getenv_opt` functions take a environment variable name and return its value. `Sys.getenv` returns directly the value, but raises a `Not_found` exception if the variable doesn't exist. `Sys.getenv_opt` returns an `option` type: `Some value` if the variable exists and `None` if not. +--- + +let path = Sys.getenv "PATH" +let () = + Printf.printf "The path is %s\n" path + +let () = + match Sys.getenv_opt "OPAM_SWITCH_PREFIX" with + | Some p -> + Printf.printf "The Opam switch prefix is %s\n" p + | None -> + print_string "The Opam switch prefix is not set.\n" From 74a46da7f193e4f54ece0eebe4f2f8021a0430dc Mon Sep 17 00:00:00 2001 From: sabine <6594573+sabine@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:40:10 +0200 Subject: [PATCH 2/3] Update data/cookbook/read-environment-variable/00-stdlib.ml --- .../read-environment-variable/00-stdlib.ml | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/data/cookbook/read-environment-variable/00-stdlib.ml b/data/cookbook/read-environment-variable/00-stdlib.ml index cf98513072..075508b80b 100644 --- a/data/cookbook/read-environment-variable/00-stdlib.ml +++ b/data/cookbook/read-environment-variable/00-stdlib.ml @@ -1,16 +1,23 @@ --- packages: [] -discussion: | - - **Understanding `Sys.getenv` and `Sys.getenv_opt`:** Both `Sys.getenv` and `Sys.getenv_opt` functions take a environment variable name and return its value. `Sys.getenv` returns directly the value, but raises a `Not_found` exception if the variable doesn't exist. `Sys.getenv_opt` returns an `option` type: `Some value` if the variable exists and `None` if not. --- +(* + 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 () = Printf.printf "The path is %s\n" path +(* + 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 "OPAM_SWITCH_PREFIX" with + match Sys.getenv_opt "API_KEY" with | Some p -> - Printf.printf "The Opam switch prefix is %s\n" p + Printf.printf "Ahpi key is %s\n" p | None -> - print_string "The Opam switch prefix is not set.\n" + print_string "Api key is not set.\n" + From b67e5709b6e98a53cdfcd16716138cf18fdad185 Mon Sep 17 00:00:00 2001 From: sabine <6594573+sabine@users.noreply.github.com> Date: Mon, 17 Jun 2024 15:41:28 +0200 Subject: [PATCH 3/3] Apply suggestions from code review --- data/cookbook/read-environment-variable/00-stdlib.ml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/data/cookbook/read-environment-variable/00-stdlib.ml b/data/cookbook/read-environment-variable/00-stdlib.ml index 075508b80b..7c9dd69257 100644 --- a/data/cookbook/read-environment-variable/00-stdlib.ml +++ b/data/cookbook/read-environment-variable/00-stdlib.ml @@ -9,7 +9,10 @@ packages: [] *) let path = Sys.getenv "PATH" let () = - Printf.printf "The path is %s\n" path + 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. @@ -17,7 +20,7 @@ let () = let () = match Sys.getenv_opt "API_KEY" with | Some p -> - Printf.printf "Ahpi key is %s\n" p + Printf.printf "Api key is %s\n" p | None -> print_string "Api key is not set.\n"