[m-rev.] diff: add --version option to mprof and mcov

Julien Fischer jfischer at opturion.com
Mon Sep 4 21:17:46 AEST 2023


Add --version option to mprof and mcov.

profiler/mercury_profile.m:
profiler/options.m:
slice/mcov.m:
      As above.

Julien.

diff --git a/profiler/mercury_profile.m b/profiler/mercury_profile.m
index 127dea2..2155d31 100644
--- a/profiler/mercury_profile.m
+++ b/profiler/mercury_profile.m
@@ -87,6 +87,16 @@ postprocess_options(Args, !IO) :-
          Args = [_ | _]
      ).

+%---------------------------------------------------------------------------%
+
+:- pred display_version(io.text_output_stream::in, io::di, io::uo) is det.
+
+display_version(OutputStream, !IO) :-
+    library.version(Version, FullArch),
+    io.format(OutputStream, "Mercury profiler, version %s, on %s\n",
+        [s(Version), s(FullArch)], !IO),
+    write_copyright_notice(OutputStream, !IO).
+
      % Display error message and then short usage message.
      %
  :- pred usage_error(string::in, io::di, io::uo) is det.
@@ -104,10 +114,7 @@ usage_error(ErrorMessage, !IO) :-

  short_usage(OutputStream, !IO) :-
      io.progname_base("mprof", ProgName, !IO),
-    library.version(Version, FullArch),
-    io.format(OutputStream, "mprof - Mercury profiler, version %s, on %s\n",
-        [s(Version), s(FullArch)], !IO),
-    write_copyright_notice(OutputStream, !IO),
+    display_version(OutputStream, !IO),
      io.format(OutputStream, "Usage: %s[<options>] [<files>]\n",
          [s(ProgName)], !IO),
      io.format(OutputStream, "Use `%s --help' for more information.\n",
@@ -160,13 +167,12 @@ write_copyright_notice(OutputStream, !IO) :-

  main_2(Args, !IO) :-
      globals.io_get_globals(Globals, !IO),
-    globals.lookup_bool_option(Globals, help, Help),
      io.stdout_stream(StdOut, !IO),
-    (
-        Help = yes,
+    ( if globals.lookup_bool_option(Globals, help, yes) then
          long_usage(StdOut, !IO)
-    ;
-        Help = no,
+    else if globals.lookup_bool_option(Globals, version, yes) then
+        display_version(StdOut, !IO)
+    else
          globals.lookup_bool_option(Globals, snapshots, Snapshots),
          (
              Snapshots = yes,
diff --git a/profiler/options.m b/profiler/options.m
index 8d66d0a..54970cf 100644
--- a/profiler/options.m
+++ b/profiler/options.m
@@ -50,7 +50,8 @@
      ;       snapshots_include_runtime
      ;       snapshots_recalc_size       % developers only
      % Miscellaneous Options
-    ;       help.
+    ;       help
+    ;       version.

  :- type option_table == option_table(option).

@@ -120,6 +121,7 @@ long_option("snapshots-recalc-size", snapshots_recalc_size).
  long_option("use-dynamic",          dynamic_cg).
  long_option("verbose",              verbose).
  long_option("very-verbose",         very_verbose).
+long_option("version",              version).

      % Verbosity Options
  option_default(verbose,                 bool(no)).
@@ -146,6 +148,7 @@ option_default(snapshots_recalc_size,   bool(yes)).

      % Miscellaneous Options
  option_default(help,                    bool(no)).
+option_default(version,                 bool(no)).

  special_handler(profile, string(WhatToProfile), !.OptionTable, Result) :-
      ( if valid_profile_option(WhatToProfile, CountFile) then
@@ -168,9 +171,11 @@ valid_profile_option("memory-cells", "Prof.MemoryCells").
  valid_profile_option("time", "Prof.Counts").

  options_help(Stream, !IO) :-
-    io.write_strings(Stream, [
-        "\t-?, -h, --help\n",
-        "\t\tPrint this usage message.\n"
+    io.write_prefixed_lines(Stream, "\t", [
+        "-?, -h, --help",
+        "\tPrint this usage message.",
+        "--version",
+        "\tPrint version information."
      ], !IO),
      io.nl(Stream, !IO),

diff --git a/slice/mcov.m b/slice/mcov.m
index bc18068..8a43823 100644
--- a/slice/mcov.m
+++ b/slice/mcov.m
@@ -59,6 +59,9 @@ main(!IO) :-
          ( if lookup_bool_option(OptionTable, help, yes) then
              io.stdout_stream(StdOutStream, !IO),
              long_usage(StdOutStream, !IO)
+        else if lookup_bool_option(OptionTable, version, yes) then
+            io.stdout_stream(StdOutStream, !IO),
+            display_version(StdOutStream, !IO)
          else
              do_coverage_testing(OptionTable, Args, !IO)
          )
@@ -431,15 +434,20 @@ write_path_port_for_user(OutStream, port_and_path(Port, Path), !IO) :-

  %---------------------------------------------------------------------------%

-:- pred short_usage(io.text_output_stream::in, io::di, io::uo) is det.
+:- pred display_version(io.text_output_stream::in, io::di, io::uo) is det.

-short_usage(OutStream, !IO) :-
-    io.progname_base("mcov", ProgName, !IO),
+display_version(OutStream, !IO) :-
      library.version(Version, FullArch),
      io.format(OutStream,
          "Mercury Coverage Testing Tool, version %s, on %s\n",
          [s(Version), s(FullArch)], !IO),
-    write_copyright_notice(OutStream, !IO),
+    write_copyright_notice(OutStream, !IO).
+
+:- pred short_usage(io.text_output_stream::in, io::di, io::uo) is det.
+
+short_usage(OutStream, !IO) :-
+    io.progname_base("mcov", ProgName, !IO),
+    display_version(OutStream, !IO),
      io.format(OutStream, "Usage: %s [<options>] [<files>]\n",
          [s(ProgName)], !IO),
      io.format(OutStream, "Use `%s --help' for more information.\n",
@@ -462,6 +470,8 @@ long_usage(OutStream, !IO) :-
          "-?, -h, --help",
          "\tPrint help about using mcov (on the standard output) and exit",
          "\twithout doing any further processing.",
+        "--version",
+        "\tPrint version information.",
          "-v, --verbose",
          "\tPrint the name of each trace count file as it is added to the union",
          "-d, --detailed",
@@ -498,6 +508,7 @@ write_copyright_notice(OutStream, !IO) :-

  :- type option
      --->    help
+    ;       version
      ;       verbose
      ;       detailed
      ;       modules
@@ -520,6 +531,7 @@ short_option('o', output_filename).
  :- pred long_option(string::in, option::out) is semidet.

  long_option("help",             help).
+long_option("version",          version).
  long_option("verbose",          verbose).
  long_option("detailed",         detailed).
  long_option("module",           modules).
@@ -532,6 +544,7 @@ long_option("ignore-mdbcomp",   ignore_mdbcomp).
  :- pred option_default(option::out, option_data::out) is multi.

  option_default(help,            bool(no)).
+option_default(version,         bool(no)).
  option_default(verbose,         bool(no)).
  option_default(detailed,        bool(no)).
  option_default(modules,         accumulating([])).


More information about the reviews mailing list