How can I access command line arguments in Nim?
The documentation shows only how to run the compiled Nim code with command line arguments
nim compile --run greetings.nim arg1 arg2
but I didn't find how to use their values in code.
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.
The VB.Net solution to your problem is to use Command() VB.Net function when you search to display command's line of currently executed process. The first outpout will display the complete command's line with name of program. The second output will display only the command's line without program's name.
To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line.
Here's an example that prints the number of arguments and the first argument:
import os
echo paramCount(), " ", paramStr(1)
Personally I find paramCount
and paramStr
a bit confusing to work with, because the paramCount
value differs from C conventions (see the document links).
Fortunately, there are additional convenient functions which do not require to be aware of the conventions:
commandLineParams
returns a seq
of only command line params.getAppFilename
returns the executable file name (what is argv[0]
in the C world).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