In Python, it's very easy to work with command-line arguments. The arguments are stored in the list sys.argv and it contains the name of the executed program too at index position 0 (like C).
| Source codes
|
import sys
for i, arg in enumerate(sys.argv):
print(f"argv[{i}]: {arg}")
|
import std/os # command-line arguments
import std/strformat # &"Hello {name}!"
echo "argv[0]: ", getAppFilename()
for i in 1 .. paramCount():
echo &"argv[{i}]: ", paramStr(i)
|
| Same output
|
$ ./v2.py
argv[0]: ./v2.py
$ ./v2.py hello 42 END
argv[0]: ./v2.py
argv[1]: hello
argv[2]: 42
argv[3]: END
|
$ ./v2
argv[0]: .../a66-command-line-arguments/v2
$ ./v2 hello 42 END
argv[0]: .../a66-command-line-arguments/v2
argv[1]: hello
argv[2]: 42
argv[3]: END
|
Notice that in Nim paramCount() doesn't include that program's name. Similarly, the first argument that the user provided can be accessed with paramStr(1).
Example:
𝥶Here, paramCount() returns 3, paramStr(1) returns "hello".
Interestingly, paramStr(0) returns the executed file's name (Python: sys.argv[0]).
The documentation says the following: "Similarly to argv in C, it is possible to call paramStr(0) but this will return OS specific contents (usually the name of the invoked executable). You should avoid this and call getAppFilename() instead."