4

If the input argument are not as expected I want to exit the program. How should I achieve that? Below is my attempt.

let () =
  if ((Array.length Sys.argv) - 1) <> 2 then                                                                                                                                              
    exit 0 ; ()                                                                                                                                                                           
  else
    ()

Thanks.

UnSat
  • 1,347
  • 2
  • 14
  • 28

1 Answers1

8

exit n is a right way to exit program, but your code has a syntax error. if ... then exit 0; () is parsed as (if ... then exit 0); (). Therefore you got a syntax error around else, since it is not correctly paired with then.

You should write:

let () =
  if ((Array.length Sys.argv) - 1) <> 2 then begin                                                                                                                                           
    exit 0 ; ()                                                                                                                                                                           
  end else
    ()

or simply,

let () = if Array.length Sys.argv - 1 <> 2 then exit 0
camlspotter
  • 8,990
  • 23
  • 27