[m-rev.] diff: Fix value of maybe_string option when negated.

Peter Wang novalazy at gmail.com
Tue Nov 8 17:04:35 AEDT 2022


On Tue, 08 Nov 2022 16:26:05 +1100 Julien Fischer <jfischer at opturion.com> wrote:
> 
> Hi Peter,
> 
> On Tue, 8 Nov 2022, Peter Wang wrote:
> 
> > library/getopt.m:
> > library/getopt_io.m:
> >    Fix bug where a negation option (--no-foo) would set the value of a
> >    maybe_string option to maybe_int(no) instead of maybe_string(no)
> >    in the option table.
> 
> It looks like this will need to go on to the release branch as well.

Right.

> It would be good to have a regression test for this one as well.

Ok, how about this.

Peter

Add regression test for maybe_int/maybe_string options.

tests/hard_coded/.gitignore:
tests/hard_coded/Mmakefile:
tests/hard_coded/getopt_maybe_option.exp:
tests/hard_coded/getopt_maybe_option.m:
    Add test case for preceding fix for maybe_string values.

diff --git a/tests/hard_coded/.gitignore b/tests/hard_coded/.gitignore
index 635be799b..9a3e5e69c 100644
--- a/tests/hard_coded/.gitignore
+++ b/tests/hard_coded/.gitignore
@@ -64,6 +64,7 @@ free_free_mode
 func_and_pred
 func_ctor_ambig
 func_test
+getopt_maybe_option
 getopt_test
 ground_dd
 higher_order_func_test
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 2738422f9..04c0eb9dd 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -180,6 +180,7 @@ ORDINARY_PROGS = \
 	functor_ho_inst_excp \
 	functor_ho_inst_excp_2 \
 	functor_ho_inst_float_reg \
+	getopt_maybe_option \
 	getopt_test \
 	gh72 \
 	gh72a \
diff --git a/tests/hard_coded/getopt_maybe_option.exp b/tests/hard_coded/getopt_maybe_option.exp
new file mode 100644
index 000000000..c084b09d2
--- /dev/null
+++ b/tests/hard_coded/getopt_maybe_option.exp
@@ -0,0 +1,4 @@
+option i1: no
+option i2: yes(123)
+option s1: no
+option s2: yes("STR")
diff --git a/tests/hard_coded/getopt_maybe_option.m b/tests/hard_coded/getopt_maybe_option.m
new file mode 100644
index 000000000..f51fbe3d0
--- /dev/null
+++ b/tests/hard_coded/getopt_maybe_option.m
@@ -0,0 +1,75 @@
+%---------------------------------------------------------------------------%
+% vim: ts=4 sw=4 et ft=mercury
+%---------------------------------------------------------------------------%
+
+:- module getopt_maybe_option.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module getopt.
+:- import_module list.
+:- import_module maybe.
+
+:- type option
+    --->    i1
+    ;       i2
+    ;       s1
+    ;       s2.
+
+:- type option_table == option_table(option).
+
+:- pred short_option(character::in, option::out) is semidet.
+
+short_option('i', i1).
+
+:- pred long_option(string::in, option::out) is semidet.
+
+long_option("i1", i1).
+long_option("i2", i2).
+long_option("s1", s1).
+long_option("s2", s2).
+
+:- pred option_default(option::out, option_data::out) is multi.
+
+option_default(i1, maybe_int(no)).
+option_default(i2, maybe_int(no)).
+option_default(s1, maybe_string(no)).
+option_default(s2, maybe_string(no)).
+
+main(!IO) :-
+    OptionOps = option_ops_multi(short_option, long_option, option_default),
+    Args = [
+        "--no-i1",
+        "--i2", "123",
+        "--no-s1",
+        "--s2", "STR"
+    ],
+    getopt.process_options(OptionOps, Args, _NonOptionArgs, OptionResult),
+    (
+        OptionResult = ok(OptionTable),
+        getopt.lookup_maybe_int_option(OptionTable, i1, I1),
+        getopt.lookup_maybe_int_option(OptionTable, i2, I2),
+        getopt.lookup_maybe_string_option(OptionTable, s1, S1),
+        getopt.lookup_maybe_string_option(OptionTable, s2, S2),
+
+        io.write_string("option i1: ", !IO),
+        io.print_line(I1, !IO),
+        io.write_string("option i2: ", !IO),
+        io.print_line(I2, !IO),
+
+        io.write_string("option s1: ", !IO),
+        io.print_line(S1, !IO),
+        io.write_string("option s2: ", !IO),
+        io.print_line(S2, !IO)
+    ;
+        OptionResult = error(Error),
+        Msg = option_error_to_string(Error),
+        io.print_line(Msg, !IO)
+    ).


More information about the reviews mailing list