Change command line options:
authorKim Nguyễn <kn@lri.fr>
Tue, 23 Jul 2013 07:38:39 +0000 (09:38 +0200)
committerKim Nguyễn <kn@lri.fr>
Tue, 23 Jul 2013 07:38:39 +0000 (09:38 +0200)
       * -d, --doc set the input document (default to stdin)
       * -o, --out set the output file (default to stdout)

Remakefile.in
src/options.ml
src/tatoo.ml

index e38e3b9..1416beb 100644 (file)
@@ -176,8 +176,8 @@ test_clean:
                        $(REMAKE) "$REF"
                        OUTPUT="$base".xml.results/"$q"_"$(PACKAGE)".xml
                        LOG="$base".xml.results/"$q"_"$(PACKAGE)".log
-                       src/@PACKAGE_TARNAME@.native@EXE@ -s "$base".xml \
-                               "$query" "$OUTPUT" > "$LOG" 2>&1
+                       src/@PACKAGE_TARNAME@.native@EXE@ -s -d "$base".xml \
+                               "$query" -o "$OUTPUT" > "$LOG" 2>&1
                        echo "Query: $q : $query" >> "$@"
                        cat  "$LOG" | grep '^STATS' >> "$@"
                        echo -n "Diff: " >> "$@"
index 61f0396..70c2b4d 100644 (file)
@@ -1,39 +1,28 @@
 open Arg
 
 let count = ref false
-let input_file = ref ""
+let input_file : string option ref = ref None
 let output_file : string option ref = ref None
 let query = ref ""
 let stats = ref false
 
+let set_string_option r s = r := Some s
+
 let specs = align [
   "-c", Set count, " write the number of results only";
   "--count", Set count, " ";
   "-s", Set stats, " display timing and various statistics";
   "--stats", Set stats, " ";
+  "-d", String (set_string_option input_file), " specify the input document file [default stdin]";
+  "--doc", String (set_string_option input_file), " ";
+  "-o", String (set_string_option output_file), " specify the output file [default stdout]";
+  "--out", String (set_string_option output_file), " ";
 ]
 
-let usage_msg = Printf.sprintf "usage: %s [options] input.xml query [output.xml]" Sys.argv.(0)
-
-let get_anon, anon_arg =
-  let args = ref []  in
-  (fun () -> !args),
-  (fun s -> args := s::!args)
+let usage_msg = Printf.sprintf "usage: %s [options] query" Sys.argv.(0)
 
 let usage () = usage specs usage_msg
 
 let parse () =
-  parse specs anon_arg usage_msg;
-  match List.rev (get_anon ()) with
-     input :: q :: maybe_output ->
-       input_file := input;
-       query := q;
-       begin
-         match maybe_output with
-           [] -> ()
-         | [ output ] -> output_file := Some output
-         | _ -> raise (Arg.Bad "too many arguments")
-       end
-  | [] | [ _ ] -> raise (Arg.Bad "not enough arguments")
-
+  parse specs (fun q -> query := q) usage_msg
 
index 1dff2a4..4d7e693 100644 (file)
@@ -27,9 +27,13 @@ let time f arg msg =
 let main () =
   let () = Options.parse () in
   let doc =
-    let fd = open_in !Options.input_file in
+    let fd, close_fd = match !Options.input_file with
+      None -> stdin, ignore
+    | Some input ->
+        let fd = open_in input in fd, fun () -> close_in fd
+    in
     let d = time Naive_tree.load_xml_file fd "parsing xml document" in
-    close_in fd; d
+    close_fd (); d
   in
   let query =
     time
@@ -78,6 +82,10 @@ let () =
   with
     Arg.Bad msg -> eprintf "Error: %s\n%!" msg; Options.usage (); exit 1
   | Sys_error msg -> eprintf "Error: %s\n%!" msg; exit 2
-  | Tree.Parse_error msg -> eprintf "Error: file %s, %s\n%!" !Options.input_file msg; exit 3
+  | Tree.Parse_error msg ->
+      eprintf "Error: %s, %s\n%!"
+        (match !Options.input_file with
+          Some s -> ("file " ^ s)
+        | None -> "[stdin]") msg; exit 3
   | Xpath.Ulexer.Error (s, e, msg) -> eprintf "Error: character %i-%i: %s\n%!" s e msg; exit 4
   | e -> eprintf "FATAL ERROR: %s\n%!" (Printexc.to_string e); exit 128