toolcontext: add `usage` and `tooldescription`
[nit.git] / src / toolcontext.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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>
6 #
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
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18
19 # Common command-line tool infractructure than handle options and error messages
20 module toolcontext
21
22 import opts
23 import location
24
25 class Message
26 super Comparable
27 redef type OTHER: Message
28
29 var location: nullable Location
30 var text: String
31
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
36
37 return location.as(not null) < other.location.as(not null)
38 end
39
40 redef fun to_s: String
41 do
42 var l = location
43 if l == null then
44 return text
45 else
46 return "{l}: {text}"
47 end
48 end
49
50 fun to_color_string: String
51 do
52 var esc = 27.ascii
53 var red = "{esc}[0;31m"
54 var bred = "{esc}[1;31m"
55 var green = "{esc}[0;32m"
56 var yellow = "{esc}[0;33m"
57 var def = "{esc}[0m"
58
59 var l = location
60 if l == null then
61 return text
62 else if l.file == null then
63 return "{yellow}{l}{def}: {text}"
64 else
65 return "{yellow}{l}{def}: {text}\n{l.colored_line("1;31")}"
66 end
67 end
68 end
69
70 # Global context for tools
71 class ToolContext
72 # Number of errors
73 var error_count: Int = 0
74
75 # Number of warnings
76 var warning_count: Int = 0
77
78 # Directory where to generate log files
79 var log_directory: String = "logs"
80
81 # Messages
82 private var messages: Array[Message] = new Array[Message]
83 private var message_sorter: ComparableSorter[Message] = new ComparableSorter[Message]
84
85 fun check_errors
86 do
87 if messages.length > 0 then
88 message_sorter.sort(messages)
89
90 for m in messages do
91 if opt_no_color.value then
92 stderr.write("{m}\n")
93 else
94 stderr.write("{m.to_color_string}\n")
95 end
96 end
97
98 messages.clear
99 end
100
101 if error_count > 0 then exit(1)
102 end
103
104 # Display an error
105 fun error(l: nullable Location, s: String)
106 do
107 messages.add(new Message(l,s))
108 error_count = error_count + 1
109 if opt_stop_on_first_error.value then check_errors
110 end
111
112 # Add an error, show errors and quit
113 fun fatal_error(l: nullable Location, s: String)
114 do
115 error(l,s)
116 check_errors
117 end
118
119 # Display a warning
120 fun warning(l: nullable Location, s: String)
121 do
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
126 end
127
128 # Display an info
129 fun info(s: String, level: Int)
130 do
131 if level <= verbose_level then
132 print "{s}"
133 end
134 end
135
136 # Global OptionContext
137 var option_context: OptionContext = new OptionContext
138
139 # Option --warn
140 var opt_warn: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
141
142 # Option --quiet
143 var opt_quiet: OptionBool = new OptionBool("Do not show warnings", "-q", "--quiet")
144
145 # Option --log
146 var opt_log: OptionBool = new OptionBool("Generate various log files", "--log")
147
148 # Option --log-dir
149 var opt_log_dir: OptionString = new OptionString("Directory where to generate log files", "--log-dir")
150
151 # Option --help
152 var opt_help: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
153
154 # Option --version
155 var opt_version: OptionBool = new OptionBool("Show version and exit", "--version")
156
157 # Option --verbose
158 var opt_verbose: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
159
160 # Option --stop-on-first-error
161 var opt_stop_on_first_error: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error")
162
163 # Option --no-color
164 var opt_no_color: OptionBool = new OptionBool("Do not use color to display errors and warnings", "--no-color")
165
166 # Verbose level
167 var verbose_level: Int = 0
168
169 init
170 do
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)
172 end
173
174 # Name, usage and synopsis of the tool.
175 # It is mainly used in `usage`.
176 # Should be correctly set by the client before calling `process_options`
177 # A multi-line string is recommmended.
178 #
179 # eg. `"Usage: tool [OPTION]... [FILE]...\nDo some things."`
180 var tooldescription: String writable = "Usage: [OPTION]... [ARG]..."
181
182 # print the full usage of the tool.
183 # It also could be called by the client.
184 fun usage
185 do
186 print tooldescription
187 option_context.usage
188 end
189
190 # Parse and process the options given on the command line
191 fun process_options
192 do
193 self.opt_warn.value = 1
194
195 # init options
196 option_context.parse(args)
197
198 # Set verbose level
199 verbose_level = opt_verbose.value
200
201 if self.opt_quiet.value then self.opt_warn.value = 0
202
203 if opt_log_dir.value != null then log_directory = opt_log_dir.value.as(not null)
204 if opt_log.value then
205 # Make sure the output directory exists
206 log_directory.mkdir
207 end
208 end
209 end