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 readable var _names
: Array[String]
22 # Type of the value of the option
23 type VALUE: nullable Object
25 # Human readable description of the option
26 readable var _helptext
: String
28 # Is this option mandatory?
29 readable writable var _mandatory
: Bool
31 # Has this option been read?
32 readable var _read
:Bool
34 # Current value of this option
35 writable var _value
: nullable VALUE
37 # Current value of this option
38 fun value
: VALUE do return _value
.as(VALUE)
40 # Default value of this option
41 readable writable var _default_value
: nullable VALUE
44 init init_opt
(help
: String, default
: nullable VALUE, names
: nullable Array[String])
47 _names
= new Array[String]
54 _default_value
= default
58 # Add new aliases for this option
59 fun add_aliases
(names
: String...) do _names
.add_all
(names
)
61 # An help text for this option with default settings
62 redef fun to_s
do return pretty
(2)
64 # A pretty print for this help
65 fun pretty
(off
: Int): String
67 var text
= new Buffer.from
(" ")
68 text
.append
(_names
.join
(", "))
70 var rest
= off
- text
.length
71 if rest
> 0 then text
.append
(" " * rest
)
73 #text.append(pretty_default)
77 fun pretty_default
: String
79 var dv
= default_value
80 if dv
!= null then return " ({dv})"
84 # Consume parameters for this option
85 protected fun read_param
(it
: Iterator[String])
93 init(text
: String) do init_opt
(text
, null, null)
95 redef fun pretty
(off
) do return to_s
97 redef fun to_s
do return helptext
102 redef type VALUE: Bool
104 init(help
: String, names
: String...) do init_opt
(help
, false, names
)
106 redef fun read_param
(it
)
115 redef type VALUE: Int
117 init(help
: String, names
: String...) do init_opt
(help
, 0, names
)
119 redef fun read_param
(it
)
126 # Option with one parameter (mandatory by default)
127 abstract class OptionParameter
129 protected fun convert
(str
: String): VALUE is abstract
131 # Is the parameter mandatory?
132 readable writable var _parameter_mandatory
: Bool
134 redef fun read_param
(it
)
137 if it
.is_ok
and it
.item
.first
!= '-' then
138 value
= convert
(it
.item
)
141 if _parameter_mandatory
then
142 # FIXME exit(1) is not a good way of handling the error
143 stderr
.write
("Error: parameter expected for option {names.first}\n")
149 init init_opt
(h
, d
, n
)
152 _parameter_mandatory
= true
157 super OptionParameter
158 redef type VALUE: nullable String
160 init(help
: String, names
: String...) do init_opt
(help
, null, names
)
162 redef fun convert
(str
) do return str
166 super OptionParameter
167 redef type VALUE: Int
168 var _values
: Array[String]
170 init(values
: Array[String], help
: String, default
: Int, names
: String...)
172 assert values
.length
> 0
173 _values
= values
.to_a
174 init_opt
("{help} <{values.join(", ")}>", default
, names
)
177 redef fun convert
(str
)
179 var id
= _values
.index_of
(str
)
183 fun value_name
: String = _values
[value
]
185 redef fun pretty_default
187 if default_value
!= null then
188 return " ({_values[default_value.as(not null)]})"
196 super OptionParameter
197 redef type VALUE: Int
199 init(help
: String, default
: Int, names
: String...) do init_opt
(help
, default
, names
)
201 redef fun convert
(str
) do return str
.to_i
205 super OptionParameter
206 redef type VALUE: Array[String]
208 init(help
: String, names
: String...)
210 _values
= new Array[String]
211 init_opt
(help
, _values
, names
)
214 var _values
: Array[String]
215 redef fun convert
(str
)
223 readable var _options
: Array[Option]
224 readable var _rest
: Array[String]
226 var _optmap
: Map[String, Option]
236 if lmax
< l
then lmax
= l
240 print
(i
.pretty
(lmax
))
244 # Parse ans assign options everywhere is the argument list
245 fun parse
(argv
: Collection[String])
247 var it
= argv
.iterator
251 protected fun parse_intern
(it
: Iterator[String])
257 while parseargs
and it
.is_ok
do
261 rest
.add_all
(it
.to_a
)
264 if _optmap
.has_key
(str
) then
265 var opt
= _optmap
[str
]
275 for opt
in _options
do
276 if opt
.mandatory
and not opt
.read
then
277 stderr
.write
("Error: mandatory option {opt.names.join(", ")} not found\n")
283 fun add_option
(opts
: Option...)
292 _options
= new Array[Option]
293 _optmap
= new HashMap[String, Option]
294 _rest
= new Array[String]