Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get access to command-line arguments in Nim?

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.

like image 469
Lucian Bredean Avatar asked Apr 12 '17 11:04

Lucian Bredean


People also ask

How do I access command line arguments?

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.

How do I get command line arguments in VB net?

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.

How do I find command line arguments in Visual Studio?

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.


2 Answers

Here's an example that prints the number of arguments and the first argument:

import os

echo paramCount(), " ", paramStr(1)
like image 57
Alex Boisvert Avatar answered Sep 17 '22 15:09

Alex Boisvert


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).
like image 40
bluenote10 Avatar answered Sep 17 '22 15:09

bluenote10