Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Working with command-line arguments — the Nim way

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:

$ ./v2 hello 42 END

𝥶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."

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 April 10, 13:46