Browse Source

NOT WORKING Add optional arguments for the reset command

 + Defined 3 functions for 3 use cases
   + Reset a command to a number
   + Reset all commands to a number
   + Reset all to nothing
 + We get the following error at runtime, we need to split in 2 commands
```

Uncaught exception:

  (Failure
   "the grammar [TARGET_NUMBER] [COMMAND_NUMBER] for anonymous arguments is not supported because there is the possibility for arguments ([COMMAND_NUMBER]) following a variable number of arguments ([TARGET_NUMBER]).  Supporting such grammars would complicate the implementation significantly.")

Raised at file "pervasives.ml", line 30, characters 22-33
Called from file "src/command.ml", line 1439, characters 14-22
Called from file "src/command_def.ml", line 90, characters 2-1023
```
Leo 9 years ago
parent
commit
aef8393b0a
3 changed files with 54 additions and 8 deletions
  1. 20 6
      src/command_def.ml
  2. 30 1
      src/tmp_file.ml
  3. 4 1
      src/tmp_file.mli

+ 20 - 6
src/command_def.ml

@@ -88,17 +88,31 @@ let shared_params =
 (* To reset tmp file *)
 let reset =
   basic
-    ~summary:"OUTDATED\n Reinitialises launches of a given [command] to [n]. \
+    ~summary:"command n Reinitialises launches for the command number [command] to [n]. \
+      All arguments are optionnal. \
+      With both the [command] and the [n] argumennt, the command number \
+      [command] is resetted to [n]. \
+      With only the [n] argument, every entry in current rc file is resetted to [n]. \
       If no [n] is given, the entry is deleted. \
-      With neither [command] nor [n], all entries are reseted."
+      Without any argument, everything is deleted."
     Spec.(
       empty
        +> shared_params
-       +> anon ("target_number" %: int)
-       +> anon ("command_number" %: int)
+       +> anon (maybe ("target_number" %: int))
+       +> anon (maybe ("command_number" %: int))
     )
-    (fun { rc } n cmd () ->
-      Tmp_file.reset ~rc n cmd
+    (fun { rc } num cmd () ->
+      (* Call the right function, according to optionnal argument.
+       * Since it's arguments passed on command line, we can not have
+       * num = None
+       * cmd = Some n
+       * cmd: number of the command to be reseted
+       * num: number to reset *)
+        match ( num, cmd ) with
+        | ( Some num, Some cmd ) -> Tmp_file.reset_cmd ~rc num cmd
+        | ( Some num, None ) -> Tmp_file.reset2num ~rc num
+        | ( None, None ) -> Tmp_file.reset_all ()
+        | ( None, Some _ ) -> assert false
     )
 ;;
 

+ 30 - 1
src/tmp_file.ml

@@ -167,7 +167,7 @@ let get_accurate_log ?rc_name ~tmp () =
 (* Reset number of launch for a given command
  * cmd: number of the command to be reseted
  * num: number to reset *)
-let reset ~rc num cmd =
+let reset_cmd ~rc num cmd =
   (* Debugging *)
   [(num,"num") ; (cmd,"cmd")]
     |> List.map ~f:(fun (i , str) -> str ^ ": " ^ (Int.to_string i))
@@ -196,3 +196,32 @@ let reset ~rc num cmd =
     sprintf "Reseted command '%s' to %i successfully" cmd_str num |> Messages.ok
 ;;
 
+(* Reset all commands to a number
+ * num: number to reset *)
+let reset2num ~rc num =
+  (* Debugging *)
+  "Num: " ^ (Int.to_string num)
+  |> Messages.debug;
+
+  let ac_log = get_accurate_log ~tmp:(init ()) () in
+
+  (* Erase number of launch for each command *)
+  List.iter ac_log ~f:(fun ( cmd, _ ) ->
+    log ~func:(fun _ -> num) ~cmd ())
+;;
+
+(* Reset all command *)
+let reset_all () =
+  let reset_without_ask () =
+    (* Make sure that file exists, otherwise strange things appears *)
+    let tmp = init () in
+    (* Get rc_file name *)
+    let name = Lazy.force !Const.rc_file in
+    write Tmp_biniou_t.{ tmp with rc = List.Assoc.add tmp.rc name [] }
+  in
+  Messages.confirm "You will lose number of launch for every command."
+    |> function
+      Messages.Yes -> reset_without_ask ()
+      | Messages.No -> ()
+;;
+

+ 4 - 1
src/tmp_file.mli

@@ -45,5 +45,8 @@ val is_prog_in_rc : 'a list -> 'a -> bool
 val log : cmd:string -> ?func:(int -> int) -> unit -> unit
 (** Return current state *)
 val get_current : unit -> int
-val reset : rc:Settings_t.rc_file -> int -> int -> unit
 val get_accurate_log : ?rc_name:string -> tmp:t -> unit -> (string * int) list
+(* Resetting command *)
+val reset_cmd : rc:Settings_t.rc_file -> int -> int -> unit
+val reset2num : rc:Settings_t.rc_file -> int -> unit
+val reset_all : unit -> unit