stdlib: Report an error when getopts is given an argument to a flag option
This commit is contained in:
parent
079711d5f6
commit
a0f855e488
2 changed files with 16 additions and 4 deletions
|
@ -98,6 +98,7 @@ tag fail_ {
|
||||||
unrecognized_option(str);
|
unrecognized_option(str);
|
||||||
option_missing(str);
|
option_missing(str);
|
||||||
option_duplicated(str);
|
option_duplicated(str);
|
||||||
|
unexpected_argument(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fail_str(fail_ f) -> str {
|
fn fail_str(fail_ f) -> str {
|
||||||
|
@ -114,6 +115,9 @@ fn fail_str(fail_ f) -> str {
|
||||||
case (option_duplicated(?nm)) {
|
case (option_duplicated(?nm)) {
|
||||||
ret "option '" + nm + "' given more than once.";
|
ret "option '" + nm + "' given more than once.";
|
||||||
}
|
}
|
||||||
|
case (unexpected_argument(?nm)) {
|
||||||
|
ret "Option " + nm + " does not take an argument.";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,6 +177,9 @@ fn getopts(vec[str] args, vec[opt] opts) -> result {
|
||||||
}
|
}
|
||||||
alt (opts.(optid).hasarg) {
|
alt (opts.(optid).hasarg) {
|
||||||
case (no) {
|
case (no) {
|
||||||
|
if (!option::is_none[str](i_arg)) {
|
||||||
|
ret failure(unexpected_argument(name_str(nm)));
|
||||||
|
}
|
||||||
vec::push[optval](vals.(optid), given);
|
vec::push[optval](vals.(optid), given);
|
||||||
}
|
}
|
||||||
case (maybe) {
|
case (maybe) {
|
||||||
|
|
|
@ -9,6 +9,7 @@ tag fail_type {
|
||||||
unrecognized_option;
|
unrecognized_option;
|
||||||
option_missing;
|
option_missing;
|
||||||
option_duplicated;
|
option_duplicated;
|
||||||
|
unexpected_argument;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_fail_type(opt::fail_ f, fail_type ft) {
|
fn check_fail_type(opt::fail_ f, fail_type ft) {
|
||||||
|
@ -25,6 +26,9 @@ fn check_fail_type(opt::fail_ f, fail_type ft) {
|
||||||
case (opt::option_duplicated(_)) {
|
case (opt::option_duplicated(_)) {
|
||||||
assert (ft == option_duplicated);
|
assert (ft == option_duplicated);
|
||||||
}
|
}
|
||||||
|
case (opt::unexpected_argument(_)) {
|
||||||
|
assert (ft == unexpected_argument);
|
||||||
|
}
|
||||||
case (_) { fail; }
|
case (_) { fail; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -262,7 +266,10 @@ fn test_optflag_long_arg() {
|
||||||
auto opts = [opt::optflag("test")];
|
auto opts = [opt::optflag("test")];
|
||||||
auto res = opt::getopts(args, opts);
|
auto res = opt::getopts(args, opts);
|
||||||
alt (res) {
|
alt (res) {
|
||||||
case (opt::failure(?f)) { log_err opt::fail_str(f); }
|
case (opt::failure(?f)) {
|
||||||
|
log_err opt::fail_str(f);
|
||||||
|
check_fail_type(f, unexpected_argument);
|
||||||
|
}
|
||||||
case (_) { fail; }
|
case (_) { fail; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -511,9 +518,7 @@ fn main() {
|
||||||
|
|
||||||
test_optflag_long();
|
test_optflag_long();
|
||||||
test_optflag_long_missing();
|
test_optflag_long_missing();
|
||||||
// FIXME: Currently long flags will silently accept arguments
|
test_optflag_long_arg();
|
||||||
// when it should probably report an error
|
|
||||||
//test_optflag_long_arg();
|
|
||||||
test_optflag_long_multi();
|
test_optflag_long_multi();
|
||||||
|
|
||||||
test_optflag_short();
|
test_optflag_short();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue