user5448913
user5448913

Reputation:

Verify parameters using Arg.parse in OCaml

Right now I'm able to check if I pass two parameters (both being integers from the height and width of a maze) and I wish to add flags before them, respectively -r (for rows) and -c (for columns).

Here'e what I have so far:

let usage = "[Usage]:\t./step -r height -c width"

let row = ref "-r"
let column = ref "-c"
let height = ref (-1)
let width = ref (-1)

let main () =
  begin
    Arg.parse [] (fun i ->
      if !height < 0
      then height := (int_of_string i)
      else width := (int_of_string i)) usage;
  end

let _ = main ()

Even after reading documentation about Arg.parse I'm not able to figure out how to add a verification to have the full format (-r height -c width) passed as parameter to my executable.

Any help would be greatly appreciated as I am learning OCaml.

Upvotes: 1

Views: 168

Answers (1)

ivg
ivg

Reputation: 35280

The grammar of a command line interface is specified by the first argument of the parse function. The second argument is a function that is called for each anonymous argument occurring on the command line, i.e., an argument that doesn't have a key before it. Here is the example:

Arg.parse Arg.[
  "-r", Set_int height, "<height> set height";
  "-c", Set_int width,  "<width>  set width";
] ignore usage

Upvotes: 1

Related Questions