% -*- mode: Noweb; noweb-code-mode: caml-mode -*- % $Id: option.nw,v 1.6 2004-06-05 15:35:48 govereau Exp $ \section{Command Line Options} \label{sec:options} % --------------------------------------------------------------------------- \subsection{Options} % --------------------------------------------------------------------------- The [[Option]] module provides command line parsing and access to option settings. On startup, the [[parse_cmdline]] function parses the command line and stores the state of all user settable options. Other modules may access the value of options through the accessor functions. <>= val parse_cmdline : unit -> unit val print_ast : unit -> bool val print_ext : unit -> bool val print_lext : unit -> bool val use_unwind : unit -> bool val filename : unit -> string val channel : unit -> in_channel @ % --------------------------------------------------------------------------- \subsection{Option Implementation} % --------------------------------------------------------------------------- Each parameter is kept in a private variable, and the accessor functions simply return the values of these variables. <>= let ast = ref false let ext = ref false let lext = ref false let unwind = ref false let file = ref "" let inch = ref stdin let print_ast() = !ast let print_ext() = !ext let print_lext() = !lext let use_unwind() = !unwind let filename() = !file let channel() = !inch @ When an input file is specified by the user, the [[set_input]] function is called. <>= let set_input s = try file := s; inch := open_in s with Sys_error err -> raise (Arg.Bad ("could not open file " ^ err)) @ The command line is processed using the [[Arg]] module from the Ocaml standard library. First we define a set of option specs, and then call the [[Arg]] module. After processing, we check the options for consistency. <>= let rec usage() = Arg.usage options "Usage:";exit 0; and options = [ "-ast", Arg.Set ast, "\t\tprint Abstract Syntax Tree"; "-ext", Arg.Set ext, "\t\tprint Expression Trees"; "-lext", Arg.Set lext, "\tprint Linearized Expression Trees"; "-unwind", Arg.Set unwind, "\tuse unwind continuations for exceptions"; "-help", Arg.Unit usage, "\tprint this message"; ] let parse_cmdline() = Arg.parse options set_input "Usage:" @