How to pass flags to dune?

I am trying to write a compiler for a simple C inspired language. My dune file looks like this

(ocamllex scanner)     
(ocamlyacc mylanguageparse) 
(executable
 (name mylanguage)         
 (libraries
  llvm
  llvm.analysis
 )
 (link_flags (-linkall))
)

I want to be able to pass in a “-a” as an argument to give the option of just printing my ast, so in top level compiler mylanguage.ml file I do

type action = Ast 

let () =
  let action = ref Compile in
  let set_action a () = action := a in
  let speclist = [
    ("-a", Arg.Unit (set_action Ast), "Print the AST"); ] 
  in  
  let usage_msg = "usage: ./mylanguage.native [-a|-s|-l|-c] [file.mylanguage]" in
  let channel = ref stdin in
  Arg.parse speclist (fun filename -> channel := open_in filename) usage_msg;
  
  let lexbuf = Lexing.from_channel !channel in
  let ast = mylanguage.program Scanner.token lexbuf in  
  match !action with
    Ast -> print_string (Ast.string_of_program ast)
  | _ -> let sast = Semant.check ast in
  Llvm_analysis.assert_valid_module m;
  print_string (Llvm.string_of_llmodule m)

Following the format in dune documentation I’ve tried variations of this

dune exec -a ./mylanguage.exe ../tests/test-hello.mylanguage
dune: unknown option `-a'.

How do I pass in my “-a” as an option? I’ve tried just a, --a, no difference

Cross-posted to https://stackoverflow.com/questions/70548687/how-to-pass-arguments-to-dune (now removed). And as commented there, please try to show some respect for the people whose free time you’re asking for by not immediately cross-posting, and when you do, link to the other posts so that people don’t spend a lot of time explaining something that has already been explained to you elsewhere.

I was not aware that cross posting was considered disrespectful and it is not my intention to be disrespectful. I have removed the stack overflow post per your comment.

You are passing -a to dune while you should be passing it to your program instead. Try

dune exec -- ./mylanguage.exe -a ../tests/test-hello.mylanguage

Cheers,
Nicolas

2 Likes

Thank you so much! A small change that made a huge difference :slight_smile:

Disrespect doesn’t require intent, just a lack of consideration. Which means you have to make some effort to consider how your actions affect other people. And that’s a skill, which has to be learnt, so some leeway needs to be given your way too of course. But at some point someone needs to point it out and explain the issue. So don’t take it too hard, and please don’t feel unwelcome, but I hope you’ll try to learn and trust you’ll become better at these things as well :slight_smile:

(This is a good reminder for me too btw, as I can be quite harsh in my responses sometimes. We all have a lot to learn about most things still, and if you, or anyone else, feels I’ve been inconsiderate, I’ll take this opportunity to apologize for that too.)

2 Likes

Yes thank you, I appreciate your input. I am relatively new at directly utilizing these discussion boards (though have been a lurker), so it is helpful to learn the culture

1 Like