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 # Manage 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 # Current value of this option
32 writable var _value
: nullable VALUE
34 # Current value of this option
35 fun value
: VALUE do return _value
.as(VALUE)
37 # Default value of this option
38 readable writable var _default_value
: nullable VALUE
41 init init_opt
(help
: String, default
: nullable VALUE, names
: nullable Array[String])
44 _names
= new Array[String]
50 _default_value
= default
54 # Add new aliases for this option
55 fun add_aliases
(names
: String...) do _names
.add_all
(names
)
57 # An help text for this option with default settings
58 redef fun to_s
do return pretty
(2)
60 # A pretty print for this help
61 fun pretty
(off
: Int): String
63 var text
= new Buffer.from
(" ")
64 text
.append
(_names
.join
(", "))
66 var rest
= off
- text
.length
67 if rest
> 0 then text
.append
(" " * rest
)
69 #text.append(pretty_default)
73 fun pretty_default
: String
75 var dv
= default_value
76 if dv
!= null then return " ({dv})"
80 # Consume parameters for this option
81 protected fun read_param
(it
: Iterator[String]) is abstract
86 init(text
: String) do init_opt
(text
, null, null)
88 redef fun pretty
(off
) do return to_s
90 redef fun to_s
do return helptext
95 redef type VALUE: Bool
97 init(help
: String, names
: String...) do init_opt
(help
, false, names
)
99 redef fun read_param
(it
) do value
= true
104 redef type VALUE: Int
106 init(help
: String, names
: String...) do init_opt
(help
, 0, names
)
108 redef fun read_param
(it
) do value
+= 1
111 # Option with one mandatory parameter
112 class OptionParameter
114 protected fun convert
(str
: String): VALUE is abstract
116 redef fun read_param
(it
)
119 value
= convert
(it
.item
)
126 init init_opt
(h
, d
, n
) do super
130 super OptionParameter
131 redef type VALUE: nullable String
133 init(help
: String, names
: String...) do init_opt
(help
, null, names
)
135 redef fun convert
(str
) do return str
139 super OptionParameter
140 redef type VALUE: Int
141 var _values
: Array[String]
143 init(values
: Array[String], help
: String, default
: Int, names
: String...)
145 assert values
.length
> 0
146 _values
= values
.to_a
147 init_opt
("{help} <{values.join(", ")}>", default
, names
)
150 redef fun convert
(str
)
152 var id
= _values
.index_of
(str
)
156 fun value_name
: String = _values
[value
]
158 redef fun pretty_default
160 if default_value
!= null then
161 return " ({_values[default_value.as(not null)]})"
169 super OptionParameter
170 redef type VALUE: Int
172 init(help
: String, default
: Int, names
: String...) do init_opt
(help
, default
, names
)
174 redef fun convert
(str
) do return str
.to_i
178 super OptionParameter
179 redef type VALUE: Array[String]
181 init(help
: String, names
: String...)
183 _values
= new Array[String]
184 init_opt
(help
, _values
, names
)
187 var _values
: Array[String]
188 redef fun convert
(str
)
196 readable var _options
: Array[Option]
197 readable var _rest
: Array[String]
199 var _optmap
: Map[String, Option]
209 if lmax
< l
then lmax
= l
213 print
(i
.pretty
(lmax
))
217 # Parse ans assign options everywhere is the argument list
218 fun parse
(argv
: Collection[String])
220 var it
= argv
.iterator
224 protected fun parse_intern
(it
: Iterator[String])
230 while parseargs
and it
.is_ok
do
234 rest
.add_all
(it
.to_a
)
237 if _optmap
.has_key
(str
) then
238 var opt
= _optmap
[str
]
249 fun add_option
(opts
: Option...)
258 _options
= new Array[Option]
259 _optmap
= new HashMap[String, Option]
260 _rest
= new Array[String]