Command Line Arguments in Standard ML
Command-line arguments are a common way to parameterize execution of programs. For example, sml program.sml
uses program.sml
as an argument to the sml
interpreter.
To experiment with command-line arguments, it’s best to save this code in a file (e.g., command_line_arguments.sml
) and run it with the SML interpreter.
In Standard ML, we use the CommandLine
structure to access command-line arguments. CommandLine.name()
gives us the program name, and CommandLine.arguments()
provides a list of the arguments.
Unlike in some other languages, Standard ML doesn’t have a direct equivalent to slicing a list from the second element onwards. Instead, we separate the program name and arguments into two different values.
To access a specific argument, we use List.nth()
, which is zero-indexed. Be careful when using this, as it will raise an exception if the index is out of bounds.
Next, we’ll look at more advanced command-line processing techniques in Standard ML.