1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008-2012 Jean Privat <jean@pryen.org>
5 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # Common command-line tool infractructure than handle options and error messages
28 redef type OTHER: Message
30 var location
: nullable Location
33 # Comparisons are made on message locations.
34 redef fun <(other
: OTHER): Bool do
35 if location
== null then return true
36 if other
.location
== null then return false
38 return location
.as(not null) < other
.location
.as(not null)
41 redef fun to_s
: String
51 fun to_color_string
: String
54 var red
= "{esc}[0;31m"
55 var bred
= "{esc}[1;31m"
56 var green
= "{esc}[0;32m"
57 var yellow
= "{esc}[0;33m"
63 else if l
.file
== null then
64 return "{yellow}{l}{def}: {text}"
66 return "{yellow}{l}{def}: {text}\n{l.colored_line("1;31")}"
71 # Global context for tools
74 var error_count
: Int = 0
77 var warning_count
: Int = 0
79 # Directory where to generate log files
80 var log_directory
: String = "logs"
83 private var messages
: Array[Message] = new Array[Message]
84 private var message_sorter
: ComparableSorter[Message] = new ComparableSorter[Message]
88 if messages
.length
> 0 then
89 message_sorter
.sort
(messages
)
92 if opt_no_color
.value
then
95 stderr
.write
("{m.to_color_string}\n")
102 if error_count
> 0 then exit
(1)
106 fun error
(l
: nullable Location, s
: String)
108 messages
.add
(new Message(l
,s
))
109 error_count
= error_count
+ 1
110 if opt_stop_on_first_error
.value
then check_errors
113 # Add an error, show errors and quit
114 fun fatal_error
(l
: nullable Location, s
: String)
121 fun warning
(l
: nullable Location, s
: String)
123 if opt_warn
.value
== 0 then return
124 messages
.add
(new Message(l
,s
))
125 warning_count
= warning_count
+ 1
126 if opt_stop_on_first_error
.value
then check_errors
130 fun info
(s
: String, level
: Int)
132 if level
<= verbose_level
then
137 # Global OptionContext
138 var option_context
: OptionContext = new OptionContext
141 var opt_warn
: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
144 var opt_quiet
: OptionBool = new OptionBool("Do not show warnings", "-q", "--quiet")
147 var opt_log
: OptionBool = new OptionBool("Generate various log files", "--log")
150 var opt_log_dir
: OptionString = new OptionString("Directory where to generate log files", "--log-dir")
153 var opt_help
: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
156 var opt_version
: OptionBool = new OptionBool("Show version and exit", "--version")
159 var opt_verbose
: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
161 # Option --stop-on-first-error
162 var opt_stop_on_first_error
: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error")
165 var opt_no_color
: OptionBool = new OptionBool("Do not use color to display errors and warnings", "--no-color")
168 var verbose_level
: Int = 0
172 option_context
.add_option
(opt_warn
, opt_quiet
, opt_stop_on_first_error
, opt_no_color
, opt_log
, opt_log_dir
, opt_help
, opt_version
, opt_verbose
)
175 # Name, usage and synopsis of the tool.
176 # It is mainly used in `usage`.
177 # Should be correctly set by the client before calling `process_options`
178 # A multi-line string is recommmended.
180 # eg. `"Usage: tool [OPTION]... [FILE]...\nDo some things."`
181 var tooldescription
: String writable = "Usage: [OPTION]... [ARG]..."
183 # Does `process_options` should accept an empty sequence of arguments.
184 # ie. nothing except options.
185 # Is `false` by default.
187 # If required, if should be set by the client before calling `process_options`
188 var accept_no_arguments
writable = false
190 # print the full usage of the tool.
191 # Is called by `process_option` on `--help`.
192 # It also could be called by the client.
195 print tooldescription
199 # Parse and process the options given on the command line
200 fun process_options
(args
: Sequence[String])
202 self.opt_warn
.value
= 1
205 option_context
.parse
(args
)
207 if opt_help
.value
then
212 if opt_version
.value
then
217 var errors
= option_context
.get_errors
218 if not errors
.is_empty
then
219 for e
in errors
do print
"Error: {e}"
220 print tooldescription
221 print
"Use --help for help"
225 if option_context
.rest
.is_empty
and not accept_no_arguments
then
226 print tooldescription
227 print
"Use --help for help"
232 verbose_level
= opt_verbose
.value
234 if self.opt_quiet
.value
then self.opt_warn
.value
= 0
236 if opt_log_dir
.value
!= null then log_directory
= opt_log_dir
.value
.as(not null)
237 if opt_log
.value
then
238 # Make sure the output directory exists
242 nit_dir
= compute_nit_dir
245 # The identified root directory of the Nit project
246 var nit_dir
: nullable String
248 private fun compute_nit_dir
: nullable String
250 # a environ variable has precedence
251 var res
= "NIT_DIR".environ
252 if not res
.is_empty
then return res
254 # find the runpath of the program from argv[0]
255 res
= "{sys.program_name.dirname}/.."
256 if res
.file_exists
and "{res}/src/nit.nit".file_exists
then return res
.simplify_path
258 # find the runpath of the process from /proc
259 var exe
= "/proc/self/exe"
260 if exe
.file_exists
then
262 res
= res
.dirname
.join_path
("..")
263 if res
.file_exists
and "{res}/src/nit.nit".file_exists
then return res
.simplify_path