Title here
Summary here
Command-line arguments are a common way to parameterize execution of programs. For example, dotnet run -- arg1 arg2
uses arg1
and arg2
as arguments to the program.
open System
[<EntryPoint>]
let main argv =
// Environment.GetCommandLineArgs() provides access to raw command-line
// arguments. Note that the first value in this array is the path to the
// program, and argv holds the arguments to the program.
let argsWithProg = Environment.GetCommandLineArgs()
let argsWithoutProg = argv
// You can get individual args with normal indexing.
let arg = if argv.Length > 2 then argv.[2] else ""
printfn "%A" argsWithProg
printfn "%A" argsWithoutProg
printfn "%s" arg
0 // return an integer exit code
To experiment with command-line arguments, it’s best to build the program first:
$ dotnet build
$ dotnet run -- a b c d
[|"/path/to/program"; "a"; "b"; "c"; "d"|]
[|"a"; "b"; "c"; "d"|]
c
In F#, the argv
parameter in the main
function already excludes the program name, which is different from some other languages. If you need the program name, you can use Environment.GetCommandLineArgs()
.
Next, we’ll look at more advanced command-line processing with flags.