Search code examples
ocamlargc

How to get argument count in OCaml?


In my exploration of learning OCaml I am wondering how you would correctly get argc i.e., the argument count from the command line to check if enough arguments were specified.

From looking at the Sys module I am not seeing a function that will get argc.

I have tried

let argc_length = Sys.argv.length(Sys.argv);;

Yet, this does not work.

How would you get the argument count from the command line in OCaml?


Solution

  • Sys.argv is an array of string. To get its length : Array.length Sys.argv

    So to fix your code :

      let argc = Array.length Sys.argv;;