Browse Source

Add '--yes' option

 + Allow to avoid question, answering yes every time.
 + Add OC_YES environment variable, to complete '--yes', see #20.
Leo 9 years ago
parent
commit
0a966e0e0e
4 changed files with 39 additions and 7 deletions
  1. 19 1
      src/command_def.ml
  2. 16 0
      src/const.ml
  3. 1 0
      src/const.mli
  4. 3 6
      src/messages.ml

+ 19 - 1
src/command_def.ml

@@ -49,9 +49,16 @@ type return_arg = {
 let shared_params =
 let shared_params =
   let open Param in
   let open Param in
   (* Way to treat common args *)
   (* Way to treat common args *)
-  return (fun verbosity no_color rc_file_name handle_signal ->
+  return (fun verbosity assume_yes no_color rc_file_name handle_signal ->
          (* Set the level of verbosity *)
          (* Set the level of verbosity *)
          Const.verbosity := verbosity;
          Const.verbosity := verbosity;
+         (* Ask question or not, see Const.ask for details *)
+         Const.ask := Option.(
+           merge
+             (some_if assume_yes true)
+             !Const.ask
+             ~f:( || )
+         );
          (* Do not use color *)
          (* Do not use color *)
          Const.no_color := no_color || !Const.no_color;
          Const.no_color := no_color || !Const.no_color;
          (* Use given rc file, preserving lazyness, since Const.rc_file is not
          (* Use given rc file, preserving lazyness, since Const.rc_file is not
@@ -67,6 +74,10 @@ let shared_params =
          (* Debugging *)
          (* Debugging *)
          let d = Messages.debug in
          let d = Messages.debug in
          d (sprintf "Verbosity set to %i" !Const.verbosity);
          d (sprintf "Verbosity set to %i" !Const.verbosity);
+         d (match !Const.ask with
+         | None -> "Assume nothing"
+         | Some false -> "Assume No"
+         | Some true -> "Assume Yes");
          d (sprintf "Color %s" (match !Const.no_color with true -> "off" | false -> "on"));
          d (sprintf "Color %s" (match !Const.no_color with true -> "off" | false -> "on"));
          begin
          begin
          match Option.try_with (fun () -> Lazy.force !Const.rc_file) with
          match Option.try_with (fun () -> Lazy.force !Const.rc_file) with
@@ -86,6 +97,13 @@ let shared_params =
         ~aliases:["--verbose" ; "-verbose"]
         ~aliases:["--verbose" ; "-verbose"]
         ~doc:"[n] Set verbosity level. \
         ~doc:"[n] Set verbosity level. \
               The higher n is, the most verbose the program is."
               The higher n is, the most verbose the program is."
+  (* Flag to assume yes *)
+  <*> flag "-y" no_arg
+        ~aliases:["--yes" ; "-yes"]
+        ~doc:" Assume yes, never ask anything. \
+        Set OC_YES environment variable to '1' is the same. \
+        Set it to '0' to assume no.
+        Set it to '-1' to be asked every time."
   (* Flag to set colors *)
   (* Flag to set colors *)
   <*> flag "--no-color" no_arg
   <*> flag "--no-color" no_arg
         ~aliases:["-no-color"]
         ~aliases:["-no-color"]

+ 16 - 0
src/const.ml

@@ -73,6 +73,22 @@ let verbosity =
   ref (get_var ~default:(lazy "4") (lazy "OC_VERB")
   ref (get_var ~default:(lazy "4") (lazy "OC_VERB")
        |> Lazy.force
        |> Lazy.force
        |> Int.of_string);;
        |> Int.of_string);;
+(* Whether we ask for confirmation, used by Messages module *)
+(* None -> ask, no preference defined,
+ * Some true -> assume Yes
+ * Some false -> assume No *)
+let ask_unset = -1;; (* Constant to leave preference unset *)
+let ask =
+  ref (get_var ~default:(lazy (Int.to_string ask_unset)) (lazy "OC_YES")
+       |> Lazy.force
+       |> Int.of_string
+       (* XXX Hacking with get_var, using
+        * -1 for None, 0 for Some false and 1 for Some true *)
+       |> function
+         | unset when unset = ask_unset -> None | 0 -> Some false | 1 -> Some true
+         | _ -> None
+  )
+;;
 (* Use do not use colors, 0 -> false, anything -> true *)
 (* Use do not use colors, 0 -> false, anything -> true *)
 let no_color =
 let no_color =
   ref (get_var ~default:(lazy "0") (lazy "OC_NOCOLOR")
   ref (get_var ~default:(lazy "0") (lazy "OC_NOCOLOR")

+ 1 - 0
src/const.mli

@@ -40,6 +40,7 @@ val editor : string lazy_t
 
 
 (* Settings *)
 (* Settings *)
 val verbosity : int ref
 val verbosity : int ref
+val ask : bool option ref
 val no_color : bool ref
 val no_color : bool ref
 
 
 (* Files *)
 (* Files *)

+ 3 - 6
src/messages.ml

@@ -147,19 +147,16 @@ let answer2str = function
 ;;
 ;;
 (* State of the program, if you should always answer yes, no or ask to the user
 (* State of the program, if you should always answer yes, no or ask to the user
  * (default)*)
  * (default)*)
-(* TODO Put it in Const *)
-let assume_yes = None;;
 (* Allow to assume yes or no like with a --yes option *)
 (* Allow to assume yes or no like with a --yes option *)
 let check_assume_yes ~f =
 let check_assume_yes ~f =
-  match assume_yes with
-  | Some true -> Yes (* --yes *)
-  | Some false -> No (* --no *)
+  match !Const.ask with (* See Const.ask for details *)
+  | Some true -> Yes
+  | Some false -> No
   | None -> f ()
   | None -> f ()
 ;;
 ;;
 
 
 (* Get confirmation
 (* Get confirmation
  * TODO:
  * TODO:
- * allow option like -y
  * test it (display, line return, etc...) *)
  * test it (display, line return, etc...) *)
 let rec confirm info =
 let rec confirm info =
   check_assume_yes ~f:(fun () ->
   check_assume_yes ~f:(fun () ->