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
27 redef type OTHER: Message
29 readable var _location
: nullable Location
30 readable var _text
: String
32 redef fun <(other
: OTHER): Bool do
33 if location
== null then return true
34 if other
.location
== null then return false
36 return location
.as(not null) < other
.location
.as(not null)
39 redef fun to_s
: String
49 fun to_color_string
: String
52 var red
= "{esc}[0;31m"
53 var bred
= "{esc}[1;31m"
54 var green
= "{esc}[0;32m"
55 var yellow
= "{esc}[0;33m"
61 else if l
.file
== null then
62 return "{yellow}{l}{def}: {text}"
64 return "{yellow}{l}{def}: {text}\n{l.colored_line("1;31")}"
69 # Global context for tools
72 readable var _error_count
: Int = 0
75 readable var _warning_count
: Int = 0
77 # Directory where to generate log files
78 readable var _log_directory
: String = "logs"
81 var _messages
: Array[Message] = new Array[Message]
82 var _message_sorter
: ComparableSorter[Message] = new ComparableSorter[Message]
86 if _messages
.length
> 0 then
87 _message_sorter
.sort
(_messages
)
90 if opt_no_color
.value
then
93 stderr
.write
("{m.to_color_string}\n")
100 if error_count
> 0 then exit
(1)
104 fun error
(l
: nullable Location, s
: String)
106 _messages
.add
(new Message(l
,s
))
107 _error_count
= _error_count
+ 1
108 if opt_stop_on_first_error
.value
then check_errors
111 # Add an error, show errors and quit
112 fun fatal_error
(l
: nullable Location, s
: String)
119 fun warning
(l
: nullable Location, s
: String)
121 if _opt_warn
.value
== 0 then return
122 _messages
.add
(new Message(l
,s
))
123 _warning_count
= _warning_count
+ 1
124 if opt_stop_on_first_error
.value
then check_errors
128 fun info
(s
: String, level
: Int)
130 if level
<= verbose_level
then
135 # Global OptionContext
136 readable var _option_context
: OptionContext = new OptionContext
139 readable var _opt_warn
: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
142 readable var _opt_quiet
: OptionBool = new OptionBool("Do not show warnings", "-q", "--quiet")
145 readable var _opt_log
: OptionBool = new OptionBool("Generate various log files", "--log")
148 readable var _opt_log_dir
: OptionString = new OptionString("Directory where to generate log files", "--log-dir")
151 readable var _opt_help
: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
154 readable var _opt_version
: OptionBool = new OptionBool("Show version and exit", "--version")
157 readable var _opt_verbose
: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
159 # Option --stop-on-first-error
160 readable var _opt_stop_on_first_error
: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error")
163 readable var _opt_no_color
: OptionBool = new OptionBool("Do not use color to display errors and warnings", "--no-color")
166 readable var _verbose_level
: Int = 0
170 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
)
173 # Parse and process the options given on the command line
176 self.opt_warn
.value
= 1
179 option_context
.parse
(args
)
182 _verbose_level
= opt_verbose
.value
184 if self.opt_quiet
.value
then self.opt_warn
.value
= 0
186 if opt_log_dir
.value
!= null then _log_directory
= opt_log_dir
.value
.as(not null)
187 if _opt_log
.value
then
188 # Make sure the output directory exists