ccorrodi avatar ccorrodi committed a0b7dd5

pretty print using command line utility

Add an option (-p) to the main program to pretty print an ast.

Comments (0)

Files changed (1)

   let ast = O.parse_file (open_in s) in
   Printf.printf "%s" (O.ast_to_db_string ast)
 
+let usage_msg =
+  Printf.sprintf "usage:\n %s [options]\n\nOptions:" Sys.argv.(0)
+
+let dummy (s : string) () : unit =
+  Printf.printf "Dummy: %s\n" s
+
+let print_database (s : string) : unit =
+  let ast = O.parse_file (open_in s) in
+  Printf.printf "%s" (O.ast_to_db_string ast)
+
+(** Print useage information to stdout. *)
+let rec print_usage () : unit =
+  Arg.usage speclist usage_msg
+
+(** List of options for the command line. *)
+and speclist : (Arg.key * Arg.spec * Arg.doc) list =
+  let f = fun s -> Printf.printf "%s" s in
+
+  [
+    "-p", Arg.String(print_database), " Parse the given database and print it
+    to stdout.";
+    "-h", Arg.Unit(print_usage), " doc for k";
+    "-help", Arg.Unit print_usage, " doc for help";
+    "--help", Arg.Unit(print_usage), " doc for more help";
+  ]
+
 (** Process the makefile databases given as argument. *)
 let _ =
-  let speclist = [] in
-  Arg.parse
-    speclist
-    (fun x -> process x)
-    "usage...\n";
+  if Array.length (Sys.argv) == 1 then
+    print_usage ()
+  else begin
+    Arg.parse
+      (Arg.align speclist)
+      (fun x -> print_usage ())
+      usage_msg
+  end;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.