Command Line Arguments in Wolfram Language

Command-line arguments are a common way to parameterize execution of programs. For example, WolframScript -script hello.wl uses -script and hello.wl arguments to the WolframScript program.

(* Command-Line Arguments *)

(* $CommandLine provides access to raw command-line arguments. 
   Note that the first value in this list is the path to the program, 
   and Rest[$CommandLine] holds the arguments to the program. *)
argsWithProg = $CommandLine
argsWithoutProg = Rest[$CommandLine]

(* You can get individual args with normal indexing. *)
arg = $CommandLine[[4]]

(* Print the results *)
Print[argsWithProg]
Print[argsWithoutProg]
Print[arg]

To experiment with command-line arguments, it’s best to save the script and run it from the command line using WolframScript.

$ wolframscript -script command-line-arguments.wl a b c d
{"/path/to/WolframKernel", "-script", "command-line-arguments.wl", "a", "b", "c", "d"}
{"a", "b", "c", "d"}
c

Next, we’ll look at more advanced command-line processing with options.