builder: improve the handling of list formats

Store them directly in List_entries, an adding helper function to
convert from string.

Also use Getopt.Symbol for them, so there is no need to manually reject
invalid formats.
This commit is contained in:
Pino Toscano
2016-07-18 11:42:22 +02:00
parent 99797a3d2f
commit 34cdb1145e
4 changed files with 43 additions and 15 deletions

View File

@@ -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";

View File

@@ -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;

View File

@@ -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 (

View File

@@ -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