Browse Source

Make absent variable less bloquant

See #3
Leo 10 years ago
parent
commit
67fea8258f
1 changed files with 18 additions and 6 deletions
  1. 18 6
      src/const.ml

+ 18 - 6
src/const.ml

@@ -38,16 +38,28 @@
 
 open Core.Std;;
 
+(* General function to get environment variables
+ * exp: exception*)
+let get_var ?(exp=false) name =
+    let msg =
+        lazy (sprintf "ERROR: Couldn't get %s. Please consider setting it." name)
+    in
+    match exp with
+    | true -> Sys.getenv name |>
+        (function
+            Some x -> x
+            | None -> failwith (Lazy.force msg))
+    | false -> print_endline (Lazy.force msg); ""
+;;
+
 (* Get current home *)
-let home = match (Sys.getenv "HOME") with
-  | Some x -> x
-  | None -> failwith "Wrong value for home\n"
+let home =
+    get_var ~exp:true "HOME"
 ;;
 
 (* Get default editor *)
-let editor = match (Sys.getenv "EDITOR") with
-  | Some x -> x
-  | None -> failwith "Wrong value for $EDITOR\n"
+let editor =
+    get_var "EDITOR"
 ;;
 
 (* Level of verbosity, used by Messages module *)