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 Python 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).

Produce the same output
$ ./v1.py
argv[0]: ./v1.py

$ ./v1.py hello 42 END
argv[0]: ./v1.py
argv[1]: hello
argv[2]: 42
argv[3]: END
$ ./v1
argv[0]: .../a66-command-line-arguments/v1

$ ./v1 hello 42 END
argv[0]: .../a66-command-line-arguments/v1
argv[1]: hello
argv[2]: 42
argv[3]: END

Source codes:

Source codes
import sys


for i, arg in enumerate(sys.argv):
    print(f"argv[{i}]: {arg}")
import std/strformat   # &"Hello {name}!"

import jsystem    # !!! own module


for i, arg in sys.argv:
  echo &"argv[{i}]: {arg}"

As you can see, the source codes are almost identical.

The magic is achieved with the jsystem module (my module). Here it is:

import os

## Mimicking Python's ``sys`` module to some extent.
##
## To avoid confusion with the ``system`` module from the stdlib,
## it's called ``jsystem``, which stands for "jabba's system" module.


# #######
# Types #
# #######

type
  JabbaSys = tuple
    argv: seq[string]


# ######
# Vars #
# ######

proc getArgv(): seq[string] =
  result &= getAppFilename()
  for i in 1 .. paramCount():
    result &= paramStr(i)

var sys*: JabbaSys = (
  argv: getArgv()
)
  ## Mimics Python's ``sys.argv``. It contains the file name and the parameters,
  ## just like in C or Python.
  ##
  ## ``sys.argv`` can be modified in Python, that's why it's a ``var``.

Links

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:00