CS334 ML Command Line Options Cheat Sheet
ML programs can process command line arguments, provided you
structure them in the following way. You may find this handy if, for
example, you would like to write a script to generate many different
random art pictures.
The following example program shows how to access the command line:
(* command.sml *)
(* get the arguments *)
let val l = CommandLine.arguments() in
(* print them *)
map (fn x => (print x; print "\n")) l;
(* exit from the interpreter. Always include
this as the last line of your code. *)
OS.Process.exit OS.Process.success : unit
end;
Running
sml command.sml a b c
will print
Standard ML of New Jersey v110.60 [built: Tue Feb 19 11:44:38 2008]
[opening command.sml]
[autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[autoloading done]
command.sml
a
b
c
Here's another handy code snippet that converts a string to an
integer:
- fun stringToInt s =
case Int.fromString(s) of
NONE => raise Fail("Bad Number " ^ s)
| SOME(n) => n;
- stringToInt "3";
val it = 3: int
- stringToInt "-23";
val it = ~23;
- stringToInt "cow";
uncaught exception Fail [Fail: Bad Number: cow]