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 var location
: nullable Location
32 # Comparisons are made on message locations.
33 redef fun <(other
: OTHER): Bool do
34 if location
== null then return true
35 if other
.location
== null then return false
37 return location
.as(not null) < other
.location
.as(not null)
40 redef fun to_s
: String
50 fun to_color_string
: String
53 var red
= "{esc}[0;31m"
54 var bred
= "{esc}[1;31m"
55 var green
= "{esc}[0;32m"
56 var yellow
= "{esc}[0;33m"
62 else if l
.file
== null then
63 return "{yellow}{l}{def}: {text}"
65 return "{yellow}{l}{def}: {text}\n{l.colored_line("1;31")}"
70 # Global context for tools
73 var error_count
: Int = 0
76 var warning_count
: Int = 0
78 # Directory where to generate log files
79 var log_directory
: String = "logs"
82 private var messages
: Array[Message] = new Array[Message]
83 private var message_sorter
: ComparableSorter[Message] = new ComparableSorter[Message]
87 if messages
.length
> 0 then
88 message_sorter
.sort
(messages
)
91 if opt_no_color
.value
then
94 stderr
.write
("{m.to_color_string}\n")
101 if error_count
> 0 then exit
(1)
105 fun error
(l
: nullable Location, s
: String)
107 messages
.add
(new Message(l
,s
))
108 error_count
= error_count
+ 1
109 if opt_stop_on_first_error
.value
then check_errors
112 # Add an error, show errors and quit
113 fun fatal_error
(l
: nullable Location, s
: String)
120 fun warning
(l
: nullable Location, s
: String)
122 if opt_warn
.value
== 0 then return
123 messages
.add
(new Message(l
,s
))
124 warning_count
= warning_count
+ 1
125 if opt_stop_on_first_error
.value
then check_errors
129 fun info
(s
: String, level
: Int)
131 if level
<= verbose_level
then
136 # Global OptionContext
137 var option_context
: OptionContext = new OptionContext
140 var opt_warn
: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
143 var opt_quiet
: OptionBool = new OptionBool("Do not show warnings", "-q", "--quiet")
146 var opt_log
: OptionBool = new OptionBool("Generate various log files", "--log")
149 var opt_log_dir
: OptionString = new OptionString("Directory where to generate log files", "--log-dir")
152 var opt_help
: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
155 var opt_version
: OptionBool = new OptionBool("Show version and exit", "--version")
158 var opt_verbose
: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
160 # Option --stop-on-first-error
161 var opt_stop_on_first_error
: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error")
164 var opt_no_color
: OptionBool = new OptionBool("Do not use color to display errors and warnings", "--no-color")
167 var verbose_level
: Int = 0
171 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
)
174 # Parse and process the options given on the command line
177 self.opt_warn
.value
= 1
180 option_context
.parse
(args
)
183 verbose_level
= opt_verbose
.value
185 if self.opt_quiet
.value
then self.opt_warn
.value
= 0
187 if opt_log_dir
.value
!= null then log_directory
= opt_log_dir
.value
.as(not null)
188 if opt_log
.value
then
189 # Make sure the output directory exists