[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 1/2] mlstdutils/mltools: factorize the machine-readable option



Store the machine-readable flag globally, just like done for
verbose/debug/etc, and enhance create_standard_options to provide
--machine-readable automatically.
---
 common/mlstdutils/std_utils.ml  | 4 ++++
 common/mlstdutils/std_utils.mli | 7 +++++--
 common/mltools/tools_utils.ml   | 7 ++++++-
 common/mltools/tools_utils.mli  | 5 ++++-
 4 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/common/mlstdutils/std_utils.ml b/common/mlstdutils/std_utils.ml
index df443058f..6499b3535 100644
--- a/common/mlstdutils/std_utils.ml
+++ b/common/mlstdutils/std_utils.ml
@@ -645,6 +645,10 @@ let verbose = ref false
 let set_verbose () = verbose := true
 let verbose () = !verbose
 
+let machine_readable = ref false
+let set_machine_readable () = machine_readable := true
+let machine_readable () = !machine_readable
+
 let with_open_in filename f =
   let chan = open_in filename in
   protect ~f:(fun () -> f chan) ~finally:(fun () -> close_in chan)
diff --git a/common/mlstdutils/std_utils.mli b/common/mlstdutils/std_utils.mli
index c887249a5..cb72fef7d 100644
--- a/common/mlstdutils/std_utils.mli
+++ b/common/mlstdutils/std_utils.mli
@@ -374,8 +374,11 @@ val set_trace : unit -> unit
 val trace : unit -> bool
 val set_verbose : unit -> unit
 val verbose : unit -> bool
-(** Stores the colours ([--colours]), quiet ([--quiet]), trace ([-x])
-    and verbose ([-v]) flags in global variables. *)
+val set_machine_readable : unit -> unit
+val machine_readable : unit -> bool
+(** Stores the colours ([--colours]), quiet ([--quiet]), trace ([-x]),
+    verbose ([-v]), and machine readable ([--machine-readable]) flags
+    in global variables. *)
 
 val with_open_in : string -> (in_channel -> 'a) -> 'a
 (** [with_open_in filename f] calls function [f] with [filename]
diff --git a/common/mltools/tools_utils.ml b/common/mltools/tools_utils.ml
index 09f1bb544..04916b89a 100644
--- a/common/mltools/tools_utils.ml
+++ b/common/mltools/tools_utils.ml
@@ -229,7 +229,7 @@ let human_size i =
     )
   )
 
-let create_standard_options argspec ?anon_fun ?(key_opts = false) usage_msg =
+let create_standard_options argspec ?anon_fun ?(key_opts = false) ?(machine_readable = false) usage_msg =
   (** Install an exit hook to check gc consistency for --debug-gc *)
   let set_debug_gc () =
     at_exit (fun () -> Gc.compact()) in
@@ -249,6 +249,11 @@ let create_standard_options argspec ?anon_fun ?(key_opts = false) usage_msg =
         [ L"echo-keys" ],       Getopt.Unit c_set_echo_keys,       s_"Don’t turn off echo for passphrases";
         [ L"keys-from-stdin" ], Getopt.Unit c_set_keys_from_stdin, s_"Read passphrases from stdin";
       ]
+      else []) @
+      (if machine_readable then
+      [
+        [ L"machine-readable" ], Getopt.Unit set_machine_readable, s_"Make output machine readable";
+      ]
       else []) in
   Getopt.create argspec ?anon_fun usage_msg
 
diff --git a/common/mltools/tools_utils.mli b/common/mltools/tools_utils.mli
index dac6b4120..44fd20be3 100644
--- a/common/mltools/tools_utils.mli
+++ b/common/mltools/tools_utils.mli
@@ -64,13 +64,16 @@ val parse_resize : int64 -> string -> int64
 val human_size : int64 -> string
 (** Converts a size in bytes to a human-readable string. *)
 
-val create_standard_options : Getopt.speclist -> ?anon_fun:Getopt.anon_fun -> ?key_opts:bool -> Getopt.usage_msg -> Getopt.t
+val create_standard_options : Getopt.speclist -> ?anon_fun:Getopt.anon_fun -> ?key_opts:bool -> ?machine_readable:bool -> Getopt.usage_msg -> Getopt.t
 (** Adds the standard libguestfs command line options to the specified ones,
     sorting them, and setting [long_options] to them.
 
     [key_opts] specifies whether add the standard options related to
     keys management, i.e. [--echo-keys] and [--keys-from-stdin].
 
+    [machine_readable] specifies whether add the [--machine-readable]
+    option.
+
     Returns a new [Getopt.t] handle. *)
 
 val external_command : ?echo_cmd:bool -> string -> string list
-- 
2.17.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]