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 # Super class of all option's class
16 # Names for the option (including long and short ones)
17 readable var _names
: Array[String]
19 # Type of the value of the option
20 type VALUE: nullable Object
22 # Human readable description of the option
23 readable var _helptext
: String
25 # Is this option mandatory?
26 readable writable var _mandatory
: Bool
28 # Current value of this option
29 writable var _value
: nullable VALUE
31 # Current value of this option
32 fun value
: VALUE do return _value
.as(VALUE)
34 # Default value of this option
35 readable writable var _default_value
: nullable VALUE
38 init init_opt
(help
: String, default
: nullable VALUE, names
: nullable Array[String])
41 _names
= new Array[String]
47 _default_value
= default
51 # Add new aliases for this option
52 fun add_aliases
(names
: String...) do _names
.add_all
(names
)
54 # An help text for this option with default settings
55 redef fun to_s
do return pretty
(2)
57 # A pretty print for this help
58 fun pretty
(off
: Int): String
60 var text
= new Buffer.from
(" ")
61 text
.append
(_names
.join
(", "))
63 var rest
= off
- text
.length
64 if rest
> 0 then text
.append
(" " * rest
)
66 #text.append(pretty_default)
70 fun pretty_default
: String
72 var dv
= default_value
73 if dv
!= null then return " ({dv})"
77 # Consume parameters for this option
78 protected fun read_param
(it
: Iterator[String]) is abstract
83 init(text
: String) do init_opt
(text
, null, null)
85 redef fun pretty
(off
) do return to_s
87 redef fun to_s
do return helptext
92 redef type VALUE: Bool
94 init(help
: String, names
: String...) do init_opt
(help
, false, names
)
96 redef fun read_param
(it
) do value
= true
101 redef type VALUE: Int
103 init(help
: String, names
: String...) do init_opt
(help
, 0, names
)
105 redef fun read_param
(it
) do value
+= 1
108 # Option with one mandatory parameter
109 class OptionParameter
111 protected fun convert
(str
: String): VALUE is abstract
113 redef fun read_param
(it
)
116 value
= convert
(it
.item
)
123 init init_opt
(h
, d
, n
) do super
127 super OptionParameter
128 redef type VALUE: nullable String
130 init(help
: String, names
: String...) do init_opt
(help
, null, names
)
132 redef fun convert
(str
) do return str
136 super OptionParameter
137 redef type VALUE: Int
138 var _values
: Array[String]
140 init(values
: Array[String], help
: String, default
: Int, names
: String...)
142 assert values
.length
> 0
143 _values
= values
.to_a
144 init_opt
("{help} <{values.join(", ")}>", default
, names
)
147 redef fun convert
(str
)
149 var id
= _values
.index_of
(str
)
153 fun value_name
: String = _values
[value
]
155 redef fun pretty_default
157 if default_value
!= null then
158 return " ({_values[default_value.as(not null)]})"
166 super OptionParameter
167 redef type VALUE: Int
169 init(help
: String, default
: Int, names
: String...) do init_opt
(help
, default
, names
)
171 redef fun convert
(str
) do return str
.to_i
175 super OptionParameter
176 redef type VALUE: Array[String]
178 init(help
: String, names
: String...)
180 _values
= new Array[String]
181 init_opt
(help
, _values
, names
)
184 var _values
: Array[String]
185 redef fun convert
(str
)
193 readable var _options
: Array[Option]
194 readable var _rest
: Array[String]
196 var _optmap
: Map[String, Option]
206 if lmax
< l
then lmax
= l
210 print
(i
.pretty
(lmax
))
214 # Parse ans assign options everywhere is the argument list
215 fun parse
(argv
: Collection[String])
217 var it
= argv
.iterator
221 protected fun parse_intern
(it
: Iterator[String])
227 while parseargs
and it
.is_ok
do
231 rest
.add_all
(it
.to_a
)
234 if _optmap
.has_key
(str
) then
235 var opt
= _optmap
[str
]
246 fun add_option
(opts
: Option...)
255 _options
= new Array[Option]
256 _optmap
= new HashMap[String, Option]
257 _rest
= new Array[String]