Browse Source

Allow to invoke without argument or with a number, for backward compatibility reasons

We are using a hack, to allow to call the program with a number as first
argument or without any argument. This way, the program tries to launch the
corresponding command or next command.
Leo 9 years ago
parent
commit
d2c50576f6
2 changed files with 32 additions and 4 deletions
  1. 5 2
      CHANGELOG.md
  2. 27 2
      src/command_def.ml

+ 5 - 2
CHANGELOG.md

@@ -7,8 +7,11 @@
  + Fix bug: in special circumstances, it was not possible to write in lock file.
    The program was crashing.
  + Rewrite command line parsing, in a cleaner and safer way. It now handles
-   `exit`, would be able to deal with autocompletion and display more accurate
-   help messages
+   `exit`, would be able to deal with auto completion and display more accurate
+   help messages. Though, for backward compatibility reasons, a hack has been
+   setted up, allowing to call the program with a number as first argument or
+   without any argument. This way, the program tries to launch the corresponding
+   command or the next one.
  + Code clean up
  + Add unit tests
  + Add licence warning

+ 27 - 2
src/command_def.ml

@@ -201,8 +201,25 @@ let default =
       Default.run ~rc n)
 
 let run ~version ~build_info () =
-  let exit_code =
-    match
+
+  (* XXX Hack to allow to run 'oclaunch 5' or 'oclaunch' as before, i.e. do not
+   * display help for sub commands but use the program directly *)
+  let hack_parse () =
+    let run_default () =
+      default
+      |> run ~version ~build_info
+    in
+    match Sys.argv with
+    | [| _ |] -> Result.Ok (run_default ()) (* Program called with nothing *)
+    | _ -> (* Program followed by a number *)
+        Or_error.try_with (fun () ->
+          (* Verify the fist argument is a number, not a subcommand (string) *)
+          ignore (Int.of_string (Sys.argv.(1)));
+          run_default ())
+  in
+
+  (* Parsing with subcommands *)
+  let parse_sub () =
     group
       ~summary:"OcLaunch program is published under CeCILL licence.\n \
       You may run the program with 'licence' command or see \
@@ -215,6 +232,14 @@ let run ~version ~build_info () =
       ; ("list", list) ; ("delete", delete) ; ("state", state)
       ; ( "reset-tmp", reset) ]
     |> run ~version ~build_info
+  in
+
+  let exit_code =
+    match
+      hack_parse ()
+      |> (function
+        Result.Ok () -> ()
+        | Error _ -> parse_sub ())
     with
     | () -> `Exit 0
     | exception message ->