How can I get command line arguments in F# compiled program? Is it possible if I don't use default template with
[<EntryPoint>]
let Main(args) = ...
I've tried
let args = Sys.argv
and
let args = fsi.CommandLineArgs
but it doesn't work for me :-(
Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.
A command-line argument is an information that directly follows the program's name on the command line when it is executed. To access the command-line arguments inside a Java program is quite easy. They are stored as strings in the String array passed to main( ).
Command-line arguments are simple parameters that are given on the system's command line, and the values of these arguments are passed on to your program during program execution. When a program starts execution without user interaction, command-line arguments are used to pass values or files to it.
If you don't want to use the args array passed to your main function then you could use System.Environment.GetCommandLineArgs()
instead. Note this will include the name of the program being run as the first item, unlike the args array given to your main function.
open System
[<EntryPoint>]
let main(args) =
printfn "args: %A" args
printfn "env.cmdline: %A" <| Environment.GetCommandLineArgs()
0
Run as args.exe 1 2 3 4
:
args: [|"1"; "2"; "3"; "4"|]
env.cmdline: [|"args.exe"; "1"; "2"; "3"; "4"|]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With