nitlanguage
/
nit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
tests: add some runtime error in nitin.input
[nit.git]
/
lib
/
opts.nit
diff --git
a/lib/opts.nit
b/lib/opts.nit
index
ffa5954
..
c93d39a
100644
(file)
--- a/
lib/opts.nit
+++ b/
lib/opts.nit
@@
-236,7
+236,13
@@
class OptionInt
super(help, default, names)
end
super(help, default, names)
end
- redef fun convert(str) do return str.to_i
+ redef fun convert(str)
+ do
+ if str.is_int then return str.to_i
+
+ errors.add "Expected an integer for option {names.join(", ")}."
+ return 0
+ end
end
# An option with a Float as parameter
end
# An option with a Float as parameter
@@
-281,14
+287,12
@@
class OptionContext
var rest = new Array[String]
# Errors found in the context after parsing
var rest = new Array[String]
# Errors found in the context after parsing
- var errors = new Array[String]
+ var context_errors = new Array[String]
private var optmap = new HashMap[String, Option]
# Add one or more options to the context
private var optmap = new HashMap[String, Option]
# Add one or more options to the context
- fun add_option(opts: Option...) do
- options.add_all(opts)
- end
+ fun add_option(opts: Option...) do options.add_all(opts)
# Display all the options available
fun usage
# Display all the options available
fun usage
@@
-309,9
+313,10
@@
class OptionContext
end
end
end
end
- # Parse and assign options everywhere in the argument list
- fun parse(argv: Collection[String])
+ # Parse and assign options in `argv` or `args`
+ fun parse(argv: nullable Collection[String])
do
do
+ if argv == null then argv = args
var it = argv.iterator
parse_intern(it)
end
var it = argv.iterator
parse_intern(it)
end
@@
-372,7
+377,7
@@
class OptionContext
for opt in options do
if opt.mandatory and not opt.read then
for opt in options do
if opt.mandatory and not opt.read then
- errors.add("Mandatory option {opt.names.join(", ")} not found.")
+ context_errors.add("Mandatory option {opt.names.join(", ")} not found.")
end
end
end
end
end
end
@@
-387,10
+392,10
@@
class OptionContext
end
# Options parsing errors.
end
# Options parsing errors.
- fun get_errors: Array[String]
+ fun errors: Array[String]
do
var errors = new Array[String]
do
var errors = new Array[String]
- errors.add_all(errors)
+ errors.add_all context_errors
for o in options do
for e in o.errors do
errors.add(e)
for o in options do
for e in o.errors do
errors.add(e)