Browse Source

Use command line argument

 - Use the value passed to the program to launch the
   corresponding command
    - TODO take up out of list value
Leo 10 years ago
parent
commit
2c5e845698
4 changed files with 44 additions and 29 deletions
  1. 0 2
      TODO.md
  2. 17 13
      src/default.ml
  3. 17 12
      src/exec_cmd.ml
  4. 10 2
      src/oclaunch.ml

+ 0 - 2
TODO.md

@@ -6,7 +6,6 @@
 ## Short term
 
 ### Commands
- + By default, pass a number and launch the corresponding command
  + Add command to modify configuration file
  + Allow to reinitialise tmp file from command line
 
@@ -20,4 +19,3 @@
 ### Misc
  + Documentation ;-)
  + Reuse atdgen for tmp file
- + Use In_channel.read_all

+ 17 - 13
src/default.ml

@@ -39,17 +39,21 @@ open Core.Std;;
 (* The module containing the step runned when the program is
  * used without argument *)
 
-let run () =
-
-  (* Obtain data from rc file *)
-  let rc_content = File_com.init_rc ~rc:Const.rc_file in
-
-  (* Obtain data from tmp file *)
-  let tmp_content = Tmp_file.init ~tmp:Const.tmp_file in
-
-  (* Execute each item (one by one)in config file *)
-  let open Settings_t in (* This prevent warning 40 for ~cmd_list:rc_content.progs *)
-  let cmd_to_exec = Exec_cmd.what_next ~cmd_list:rc_content.progs ~tmp:tmp_content in
-    Exec_cmd.execute ~tmp:tmp_content cmd_to_exec (* TODO Use display option in rc file *)
-    |> (fun a -> ()) (* Return nothing, because launched from oclaunch.ml *)
+let run ~rc:rc_content ~tmp:tmp_content cmd_number () =
+  (* cmd_number is the number of the command the user wants
+   * to execute *)
+  match cmd_number with
+    | None -> begin
+        (* Execute each item (one by one)in config file *)
+        let open Settings_t in (* This prevent warning 40 for ~cmd_list:rc_content.progs *)
+          let cmd_to_exec = Exec_cmd.what_next ~cmd_list:rc_content.progs ~tmp:tmp_content in
+            (* TODO Use display option in rc file *)
+            Exec_cmd.execute ~tmp:tmp_content cmd_to_exec;
+            () (* Return nothing, because launched from oclaunch.ml *)
+      end
+    | Some num -> begin
+        let cmd_to_exec = Exec_cmd.num_cmd_to_cmd ~cmd_list:rc_content.progs num in
+          Exec_cmd.execute ~tmp:tmp_content cmd_to_exec;
+          ()
+      end
 ;;

+ 17 - 12
src/exec_cmd.ml

@@ -36,24 +36,29 @@
 
 open Core.Std;;
 
+(* Function to return the corresponding command to a number *)
+let num_cmd_to_cmd ~cmd_list number =
+  (*Verify that the number is not out of the list *)
+  if (List.length cmd_list) < number
+  then
+    ""
+  else
+    begin
+      let cmd_to_exec = List.nth cmd_list number in
+        match cmd_to_exec with
+            | None -> ""
+            | Some x -> x
+    end
+;;
+
 (* Function to determinate what is the next command to
  * execute *)
 let what_next ~tmp ~cmd_list =
   let tmp_json = Yojson.Basic.from_file tmp in
   let open Yojson.Basic.Util in
   let num_next = tmp_json |> member "num" |> to_int in (* Number of the next cmd to run *)
-    (*Verify that the number is not out of the list *)
-    if (List.length cmd_list) < num_next
-    then
-      ""
-        else
-          begin
-            let cmd_to_exec = List.nth cmd_list num_next in
-              match cmd_to_exec with
-                | None -> ""
-                | Some x -> x
-          end
-;;
+    num_cmd_to_cmd ~cmd_list:cmd_list num_next
+  ;;
 
 (* Log when a program has been launched in a file in /tmp
    ~func is the function applied to the value *)

+ 10 - 2
src/oclaunch.ml

@@ -45,13 +45,21 @@ let version_number = "0.1.2";;
  * information *)
 let build_info = ( "Build with OCaml version " ^ (Sys.ocaml_version) ^ " on " ^ (Sys.os_type) );;
 
+(* Obtain data from rc file *)
+let rc_content = File_com.init_rc ~rc:Const.rc_file;;
+
+(* Obtain data from tmp file *)
+let tmp_content = Tmp_file.init ~tmp:Const.tmp_file;;
+
+
 (* Define commands *)
 let commands =
   Command.basic
     ~summary:"OcLaunch program is published under CeCILL licence. See https://gitlab.com/WzukW/oclaunch for details"
     ~readme:(fun () -> "See https://gitlab.com/WzukW/oclaunch for help")
-    Command.Spec.empty
-    Default.run
+    (* TODO if number is out of the mist, return error message *)
+    Command.Spec.(empty +> anon (maybe ("Command number" %: int)))
+    (Default.run ~rc:rc_content ~tmp:tmp_content)
 ;;
 
 let () =