stdlib: Fix %branch -> %fedora-branch.
[goals.git] / src / cmdline.ml
index 53ab0f9..927dfdf 100644 (file)
@@ -23,7 +23,7 @@ open Utils
 
 (* See also "let id" in [lexer.mll]. *)
 let var_regexp =
-  Str.regexp "\\([a-zA-Z_][a-zA-Z0-9_]*\\)[ \t]*=[ \t]*\\(.*\\)"
+  Str.regexp "\\([a-zA-Z_][-a-zA-Z0-9_]*\\)[ \t]*=[ \t]*\\(.*\\)"
 
 let usage =
   "\
@@ -41,25 +41,33 @@ let print_version () =
 
 (* Get stdlib directory. *)
 let datadir =
-  try Sys.getenv "GOALS_DATADIR" with Not_found -> Config.datadir
+  try Sys.getenv "GOALS_DATADIR"
+  with Not_found -> Config.datadir // "goals"
 let stdlibdir = datadir // "stdlib"
-let prelude_file = stdlibdir // "prelude.gl"
-let () =
-  if not (is_directory stdlibdir) || not (Sys.file_exists prelude_file) then
-    failwithf "%s: cannot find the standard library directory, expected %s.  If the standard library directory is in a non-standard location then set GOALS_DATADIR.  If you can trying to run goals from the build directory then use ‘./run goals ...’"
-      Sys.executable_name stdlibdir
+let prelude_gl_file = stdlibdir // "prelude.gl"
+let prelude_sh_file = stdlibdir // "prelude.sh"
 
-let input_file, directory, includes, use_prelude, anon_vars, targets =
-  let args = ref [] in
-  let directory = ref "." in
-  let input_file = ref "Goalfile" in
-  let includes = ref [stdlibdir] in
-  let add_include dir = includes := dir :: !includes in
-  let use_prelude = ref true in
+let debug_flag = ref false
+let directory = ref "."
+let input_file = ref "Goalfile"
+let includes = ref [stdlibdir]
+let add_include dir = includes := dir :: !includes
+let nr_jobs = ref (nprocs ())
+let silent = ref false
+let use_prelude = ref true
+
+let parse () =
+  if not (is_directory stdlibdir) || not (Sys.file_exists prelude_gl_file) then
+    failwithf "%s: cannot find the standard library directory, expected %s.  If the standard library directory is in a non-standard location then set GOALS_DATADIR.  If you can trying to run goals from the build directory then use ‘./run goals ...’"
+      Sys.executable_name stdlibdir;
 
+  let jobshelp =
+    sprintf "jobs Set number of parallel jobs (default: %d)" !nr_jobs in
   let argspec = [
     "-C",          Arg.Set_string directory,
                    "directory Change to directory before running";
+    "-d",          Arg.Set debug_flag,
+                   " Print debug information.";
     "--directory", Arg.Set_string directory,
                    "directory Change to directory before running";
     "-f",          Arg.Set_string input_file,
@@ -70,23 +78,33 @@ let input_file, directory, includes, use_prelude, anon_vars, targets =
                    "dir Add include directory";
     "--include",   Arg.String add_include,
                    "dir Add include directory";
+    "-j",          Arg.Set_int nr_jobs,
+                   jobshelp;
+    "--jobs",      Arg.Set_int nr_jobs,
+                   jobshelp;
     "--no-prelude",Arg.Clear use_prelude,
                    " Do not automatically use prelude.gl from stdlib";
-    "-V",          Arg.Unit print_version,
+    "-s",          Arg.Set silent,
+                   " Silent operation";
+    "--silent",    Arg.Set silent,
+                   " Silent operation";
+    "--quiet",     Arg.Set silent,
+                   " Silent operation";
+    "-v",          Arg.Unit print_version,
                    " Print version and exit";
     "--version",   Arg.Unit print_version,
                    " Print version and exit";
   ] in
   let argspec = Arg.align argspec in
+  let args = ref [] in
   let anon_fun s = args := s :: !args in
   Arg.parse argspec anon_fun usage;
 
+  (* Check various params are sensible. *)
+  if !nr_jobs < 1 then
+    failwithf "%s: -j must be >= 1" Sys.executable_name;
+
   let args = List.rev !args in
-  let directory = !directory in
-  let input_file = absolute_path !input_file in
-  (* Don't reverse includes - we want newer -I options to take precedence. *)
-  let includes = List.map absolute_path !includes in
-  let use_prelude = !use_prelude in
 
   (* Get the anon var assignments and targets. *)
   let anon_vars, targets =
@@ -102,4 +120,21 @@ let input_file, directory, includes, use_prelude, anon_vars, targets =
         (name, expr)
     ) anon_vars in
 
-  input_file, directory, includes, use_prelude, anon_vars, targets
+  anon_vars, targets
+
+let debug_flag () = !debug_flag
+
+(* Create the debug function. *)
+let debug fs =
+  let display str = if debug_flag () then prerr_endline str in
+  ksprintf display fs
+
+let directory () = !directory
+let input_file () = !input_file
+
+(* Don't reverse includes - we want newer -I options to take precedence. *)
+let includes () = !includes
+
+let nr_jobs () = !nr_jobs
+let silent () = !silent
+let use_prelude () = !use_prelude