From 353de828e16083c9f27bb993166b6890f08332e7 Mon Sep 17 00:00:00 2001 From: Philippe Wang Date: Thu, 31 Mar 2016 16:11:05 +0200 Subject: [PATCH] make the binary more useful modified: glical_bin.ml --- glical_bin.ml | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/glical_bin.ml b/glical_bin.ml index 5ad1b60..409df6e 100644 --- a/glical_bin.ml +++ b/glical_bin.ml @@ -19,8 +19,11 @@ let filters = method add x = s <- SSet.add x s method mem x = SSet.mem x s method is_empty = s = SSet.empty + method get_filter = s end +let values_only = ref false + let assert_ a m = if not a then begin @@ -44,19 +47,24 @@ let () = [ ("-input", String(wrap(fun s -> inputs := open_in_bin s :: !inputs)), - "f use/add f as an input"); + "f Use/add f as an input"); ("-ical", Set(ical), - " output in iCalendar format"); + " Output in iCalendar format (this option has no effect at all for the moment)"); ("-tpl", String(wrap(fun s -> template := Some(open_in_bin s))), - "t output according to the template t"); + "t Output according to the template t (not yet implemented)"); ("-o", String(wrap(fun s -> out := open_out_bin s)), - "f use file f as output (default is stdout)"); + "f Output to file f (default is stdout)"); ("-f", String(filters#add), - " specify a label that should be kept"); + "l Specify label l that should be kept. \ + Use multiple -F for multiple labels. If no label is given, all are kept."); + ("-F", + String(fun s -> values_only := true; filters#add s), + "l Same as -f but only values will printed. \ + If at least one -F is used, all other -f will behave like -F."); ] (wrap(fun s -> inputs := open_in_bin s :: !inputs)) "glical takes some iCalendar data and allows you to play a little with it" @@ -79,10 +87,28 @@ let _ = List.iter (fun i -> Buffer.add_string b (channel_contents i)) !inputs; parse_ical(Lexing.lex_ical(Buffer.contents b)) in - fprintf - !out - "%s" - (to_string ~f:(fun _ -> None) data) + if !values_only then + let data = + Glical.extract_values ~ks:(filters#get_filter) data + in + List.iter (fun e -> + fprintf + !out + "%s\n" + (e.to_string()) + ) + data + else + let data = + if filters#is_empty then + data + else + Glical.extract_assocs ~ks:(filters#get_filter) data + in + fprintf + !out + "%s" + (to_string ~f:(fun _ -> None) data)