Change -V to -v for compat with make.
[goals.git] / src / cmdline.ml
1 (* Goalfile command line
2  * Copyright (C) 2019 Richard W.M. Jones
3  * Copyright (C) 2019 Red Hat Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *)
19
20 open Printf
21
22 open Utils
23
24 (* See also "let id" in [lexer.mll]. *)
25 let var_regexp =
26   Str.regexp "\\([a-zA-Z_][a-zA-Z0-9_]*\\)[ \t]*=[ \t]*\\(.*\\)"
27
28 let usage =
29   "\
30 goals: Build software.
31
32  goals [-f Goalfile] ['var=value' ...] ['target' ...]
33
34 For detailed help see goals(1).
35
36 Options:"
37
38 let print_version () =
39   printf "%s %s\n" Config.package_name Config.package_version;
40   exit 0
41
42 (* Get stdlib directory. *)
43 let datadir =
44   try Sys.getenv "GOALS_DATADIR" with Not_found -> Config.datadir
45 let stdlibdir = datadir // "stdlib"
46 let prelude_file = stdlibdir // "prelude.gl"
47 let () =
48   if not (is_directory stdlibdir) || not (Sys.file_exists prelude_file) then
49     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 ...’"
50       Sys.executable_name stdlibdir
51
52 let input_file, directory, includes, use_prelude, anon_vars, targets =
53   let args = ref [] in
54   let directory = ref "." in
55   let input_file = ref "Goalfile" in
56   let includes = ref [stdlibdir] in
57   let add_include dir = includes := dir :: !includes in
58   let use_prelude = ref true in
59
60   let argspec = [
61     "-C",          Arg.Set_string directory,
62                    "directory Change to directory before running";
63     "--directory", Arg.Set_string directory,
64                    "directory Change to directory before running";
65     "-f",          Arg.Set_string input_file,
66                    "filename Set name of Goalfile";
67     "--file",      Arg.Set_string input_file,
68                    "filename Set name of Goalfile";
69     "-I",          Arg.String add_include,
70                    "dir Add include directory";
71     "--include",   Arg.String add_include,
72                    "dir Add include directory";
73     "--no-prelude",Arg.Clear use_prelude,
74                    " Do not automatically use prelude.gl from stdlib";
75     "-v",          Arg.Unit print_version,
76                    " Print version and exit";
77     "--version",   Arg.Unit print_version,
78                    " Print version and exit";
79   ] in
80   let argspec = Arg.align argspec in
81   let anon_fun s = args := s :: !args in
82   Arg.parse argspec anon_fun usage;
83
84   let args = List.rev !args in
85   let directory = !directory in
86   let input_file = absolute_path !input_file in
87   (* Don't reverse includes - we want newer -I options to take precedence. *)
88   let includes = List.map absolute_path !includes in
89   let use_prelude = !use_prelude in
90
91   (* Get the anon var assignments and targets. *)
92   let anon_vars, targets =
93     List.partition (
94       fun arg -> Str.string_match var_regexp arg 0
95     ) args in
96   let anon_vars =
97     List.map (
98       fun arg ->
99         ignore (Str.string_match var_regexp arg 0);
100         let name = Str.matched_group 1 arg in
101         let expr = Str.matched_group 2 arg in
102         (name, expr)
103     ) anon_vars in
104
105   input_file, directory, includes, use_prelude, anon_vars, targets