diff --git a/builder/cmdline.ml b/builder/cmdline.ml index 49a57eeba..73ffd122b 100644 --- a/builder/cmdline.ml +++ b/builder/cmdline.ml @@ -43,7 +43,7 @@ type cmdline = { delete_on_failure : bool; format : string option; gpg : string; - list_format : [`Short|`Long|`Json]; + list_format : List_entries.format; memsize : int option; network : bool; ops : Customize_cmdline.ops; @@ -89,15 +89,13 @@ let parse_cmdline () = let format = ref "" in let gpg = ref "gpg" in - let list_format = ref `Short in - let list_set_long () = list_format := `Long in + let list_format = ref List_entries.Short in + let list_set_long () = list_format := List_entries.Long in let list_set_format arg = - list_format := match arg with - | "short" -> `Short - | "long" -> `Long - | "json" -> `Json - | fmt -> - error (f_"invalid --list-format type '%s', see the man page") fmt in + (* Do not catch the Invalid_argument that list_format_of_string + * throws on invalid input, as it is already checked by the + * Getopt handling of Symbol. *) + list_format := List_entries.list_format_of_string arg in let machine_readable = ref false in @@ -119,6 +117,9 @@ let parse_cmdline () = let sync = ref true in let warn_if_partition = ref true in + let formats = List_entries.list_formats + and formats_string = String.concat "|" List_entries.list_formats in + let argspec = [ [ L"arch" ], Getopt.Set_string ("arch", arch), s_"Set the output architecture"; [ L"attach" ], Getopt.String ("iso", attach_disk), s_"Attach data disk/ISO during install"; @@ -145,7 +146,7 @@ let parse_cmdline () = [ L"gpg" ], Getopt.Set_string ("gpg", gpg), s_"Set GPG binary/command"; [ S 'l'; L"list" ], Getopt.Unit list_mode, s_"List available templates"; [ L"long" ], Getopt.Unit list_set_long, s_"Shortcut for --list-format long"; - [ L"list-format" ], Getopt.String ("short|long|json", list_set_format), + [ L"list-format" ], Getopt.Symbol (formats_string, formats, list_set_format), s_"Set the format for --list (default: short)"; [ L"machine-readable" ], Getopt.Set machine_readable, s_"Make output machine readable"; [ S 'm'; L"memsize" ], Getopt.Int ("mb", set_memsize), s_"Set memory size"; diff --git a/builder/cmdline.mli b/builder/cmdline.mli index 854db618f..a0517a25d 100644 --- a/builder/cmdline.mli +++ b/builder/cmdline.mli @@ -30,7 +30,7 @@ type cmdline = { delete_on_failure : bool; format : string option; gpg : string; - list_format : [`Short|`Long|`Json]; + list_format : List_entries.format; memsize : int option; network : bool; ops : Customize_cmdline.ops; diff --git a/builder/list_entries.ml b/builder/list_entries.ml index 2f053e8a9..2a1aef4c8 100644 --- a/builder/list_entries.ml +++ b/builder/list_entries.ml @@ -21,11 +21,24 @@ open Common_utils open Printf +type format = + | Short + | Long + | Json + +let list_formats = [ "short"; "long"; "json" ] + +let list_format_of_string = function + | "short" -> Short + | "long" -> Long + | "json" -> Json + | fmt -> invalid_arg fmt + let rec list_entries ~list_format ~sources index = match list_format with - | `Short -> list_entries_short index - | `Long -> list_entries_long ~sources index - | `Json -> list_entries_json ~sources index + | Short -> list_entries_short index + | Long -> list_entries_long ~sources index + | Json -> list_entries_json ~sources index and list_entries_short index = List.iter ( diff --git a/builder/list_entries.mli b/builder/list_entries.mli index a3f35d39b..008b90623 100644 --- a/builder/list_entries.mli +++ b/builder/list_entries.mli @@ -16,4 +16,18 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) -val list_entries : list_format:([ `Short | `Long | `Json ]) -> sources:Sources.source list -> Index.index -> unit +type format = + | Short + | Long + | Json + +val list_formats : string list +(** The string representation of the available formats. *) + +val list_format_of_string : string -> format +(** Convert from a string to the corresponding format. + + Throw [Invalid_argument] if the string does not match any + valid format. *) + +val list_entries : list_format:format -> sources:Sources.source list -> Index.index -> unit