Browse Source

Add feature: --rc argument

 - Define command line interface
 - First changes to get it work
 - Update CHANGELOG
Leo 10 years ago
parent
commit
868afb612e
4 changed files with 17 additions and 9 deletions
  1. 2 1
      CHANGELOG.md
  2. 9 4
      src/command_def.ml
  3. 4 2
      src/const.ml
  4. 2 2
      src/file_com.ml

+ 2 - 1
CHANGELOG.md

@@ -1,7 +1,8 @@
 # Changelog of OcLaunch
 
 ## v0.2.1
- +
+ + Add new command line option:
+   + “-c file” allow to read configuration from custom file.
 
 ## v0.2.0
  + Add new command line option.

+ 9 - 4
src/command_def.ml

@@ -38,13 +38,14 @@ open Core.Std;;
 
 (* Module containing the definition of the interface of OcLaunch *)
 
-(* Obtain data from rc_file *)
-let rc_content = File_com.init_rc ();;
-
 (* Arguments *)
 let args =
     let open Command.Spec in
     (empty
+    (* Flag to use different rc file *)
+    +> flag "-c" (optional_with_default !Const.rc_file file)
+    ~aliases:["--rc" ; "-rc"]
+    ~doc:"file Read configuration from the given file and continue parsing."
     (* Flag to reset tmp file *)
     +> flag "-r" no_arg
         ~aliases:["-reset-tmp" ; "--reset-tmp"]
@@ -78,7 +79,11 @@ let commands =
     ~readme:(fun () -> "See https://gitlab.com/WzukW/oclaunch for help.")
     args
 
-    (fun reset_tmp list_commands add delete number num_cmd () ->
+    (fun rc_file_name reset_tmp list_commands add delete number num_cmd () ->
+       (* Use given rc file, should run the nth argument if present *)
+       Const.rc_file := rc_file_name;
+       (* Obtain data from rc_file *)
+       let rc_content = File_com.init_rc () in
        (* First try to list *)
        if list_commands then List_rc.run ~rc:rc_content
        (* To add command to rc file *)

+ 4 - 2
src/const.ml

@@ -44,7 +44,9 @@ let home = match (Sys.getenv "HOME") with
   | None -> failwith "Wrong value for home\n"
 ;;
 
-(* Some settings variables *)
-let rc_file = home ^ "/" ^ ".oclaunch_rc.json";;
+(* Default place to read settings *)
+let rc_file_default = home ^ "/" ^ ".oclaunch_rc.json";;
+(* Current place to read settings, maybe modified from command line argument *)
+let rc_file = ref rc_file_default;;
 (* Set tmp file, in witch stock launches, in biniou format *)
 let tmp_file = "/tmp/.oclaunch_trace.dat";; (* File where launch are logged *)

+ 2 - 2
src/file_com.ml

@@ -44,7 +44,7 @@ type t = Settings_v.rc_file;;
 (* Function to write the rc file *)
 let write (tmp_file:t) =
         (* Short name *)
-        let name = Const.rc_file in
+        let name = !Const.rc_file in
         (* Create string to be written *)
         let data = (Settings_j.string_of_rc_file tmp_file
         |> Yojson.Basic.prettify ~std:true) in
@@ -69,7 +69,7 @@ let create_rc_file ~name =
 ;;
 
 (* Function to read the rc file *)
-let rec init_rc ?(rc=Const.rc_file) () =
+let rec init_rc ?(rc=(!Const.rc_file)) () =
   (* Verify that file exist *)
   match (Sys.file_exists rc) with
     | `No -> create_rc_file ~name:rc; init_rc ~rc ();