1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008 Jean Privat <jean@pryen.org>
6 # This file is free software, which comes along with NIT. This software is
7 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
10 # is kept unaltered, and a notification of the changes is added.
11 # You are allowed to redistribute it and sell it, alone or is a part of
14 # Management of options on the command line
17 # Super class of all option's class
19 # Names for the option (including long and short ones)
20 var names
: Array[String]
22 # Type of the value of the option
23 type VALUE: nullable Object
25 # Human readable description of the option
28 # Gathering errors during parsing
29 var errors
: Array[String]
31 # Is this option mandatory?
32 var mandatory
: Bool writable
34 # Has this option been read?
35 var read
:Bool writable
37 # Current value of this option
38 var value
: VALUE writable
40 # Default value of this option
41 var default_value
: nullable VALUE writable
44 init init_opt
(help
: String, default
: nullable VALUE, names
: nullable Array[String])
47 self.names
= new Array[String]
49 self.names
= names
.to_a
54 default_value
= default
56 errors
= new Array[String]
59 # Add new aliases for this option
60 fun add_aliases
(names
: String...) do names
.add_all
(names
)
62 # An help text for this option with default settings
63 redef fun to_s
do return pretty
(2)
65 # A pretty print for this help
66 fun pretty
(off
: Int): String
68 var text
= new FlatBuffer.from
(" ")
69 text
.append
(names
.join
(", "))
71 var rest
= off
- text
.length
72 if rest
> 0 then text
.append
(" " * rest
)
74 #text.append(pretty_default)
78 fun pretty_default
: String
80 var dv
= default_value
81 if dv
!= null then return " ({dv})"
85 # Consume parameters for this option
86 protected fun read_param
(it
: Iterator[String])
94 init(text
: String) do init_opt
(text
, null, null)
96 redef fun pretty
(off
) do return to_s
98 redef fun to_s
do return helptext
103 redef type VALUE: Bool
105 init(help
: String, names
: String...) do init_opt
(help
, false, names
)
107 redef fun read_param
(it
)
116 redef type VALUE: Int
118 init(help
: String, names
: String...) do init_opt
(help
, 0, names
)
120 redef fun read_param
(it
)
127 # Option with one parameter (mandatory by default)
128 abstract class OptionParameter
130 protected fun convert
(str
: String): VALUE is abstract
132 # Is the parameter mandatory?
133 var parameter_mandatory
: Bool writable
135 redef fun read_param
(it
)
138 if it
.is_ok
and it
.item
.chars
.first
!= '-' then
139 value
= convert
(it
.item
)
142 if parameter_mandatory
then
143 errors
.add
("Parameter expected for option {names.first}.")
148 init init_opt
(h
, d
, n
)
151 parameter_mandatory
= true
156 super OptionParameter
157 redef type VALUE: nullable String
159 init(help
: String, names
: String...) do init_opt
(help
, null, names
)
161 redef fun convert
(str
) do return str
165 super OptionParameter
166 redef type VALUE: Int
167 var values
: Array[String]
169 init(values
: Array[String], help
: String, default
: Int, names
: String...)
171 assert values
.length
> 0
172 self.values
= values
.to_a
173 init_opt
("{help} <{values.join(", ")}>", default
, names
)
176 redef fun convert
(str
)
178 var id
= values
.index_of
(str
)
180 var e
= "Unrecognized value for option {names.join(", ")}.\n"
181 e
+= "Expected values are: {values.join(", ")}."
187 fun value_name
: String do return values
[value
]
189 redef fun pretty_default
191 if default_value
!= null then
192 return " ({values[default_value.as(not null)]})"
200 super OptionParameter
201 redef type VALUE: Int
203 init(help
: String, default
: Int, names
: String...) do init_opt
(help
, default
, names
)
205 redef fun convert
(str
) do return str
.to_i
209 super OptionParameter
210 redef type VALUE: Array[String]
212 init(help
: String, names
: String...)
214 values
= new Array[String]
215 init_opt
(help
, values
, names
)
218 private var values
: Array[String]
219 redef fun convert
(str
)
227 var options
: Array[Option]
228 var rest
: Array[String]
229 var errors
: Array[String]
231 private var optmap
: Map[String, Option]
241 if lmax
< l
then lmax
= l
245 print
(i
.pretty
(lmax
))
249 # Parse ans assign options everywhere is the argument list
250 fun parse
(argv
: Collection[String])
252 var it
= argv
.iterator
256 protected fun parse_intern
(it
: Iterator[String])
262 while parseargs
and it
.is_ok
do
266 rest
.add_all
(it
.to_a
)
269 # We're looking for packed short options
270 if str
.chars
.last_index_of
('-') == 0 and str
.length
> 2 then
271 var next_called
= false
272 for i
in [1..str
.length
] do
273 var short_opt
= "-" + str
.chars
[i
].to_s
274 if optmap
.has_key
(short_opt
) then
275 var option
= optmap
[short_opt
]
276 if option
isa OptionParameter then
280 option
.read_param
(it
)
283 if not next_called
then it
.next
285 if optmap
.has_key
(str
) then
286 var opt
= optmap
[str
]
297 for opt
in options
do
298 if opt
.mandatory
and not opt
.read
then
299 errors
.add
("Mandatory option {opt.names.join(", ")} not found.")
304 fun add_option
(opts
: Option...)
313 options
= new Array[Option]
314 optmap
= new HashMap[String, Option]
315 rest
= new Array[String]
316 errors
= new Array[String]
328 fun get_errors
: Array[String]
330 var errors
: Array[String] = new Array[String]
332 errors
.add_all
(errors
)