Springe direkt zu Inhalt

Command-line arguments

Command-line arguments allow data to be passed to a program, which can then be used to modify the behaviour of the program.

Often the same program needs to be run several times, but with different values of one or more parameters.  If, say, the program should be run for different values of temperature, T, the simplest approach is just to make multiple copies of the program and change the value of T in each copy.  However, with many programming languages it is possible to pass arguments on the command line to the program itself.  This way the program does not need to be copied or changed, but can still be run for arbitrarily many different values of, say, T:

./my_simulation 10

Within the program, the command-line arguments must be used to intialize the corresponding variables.

Being able to pass parameters als command-line arguments is particularly useful if one wants to make use of job arrays.

Examples

Note that in the following the arguments are interpreted as strings and thus need to be converted to the actual type required.

Python

import sys
T = int(sys.argv[1])

R

args = commandArgs(trailingOnly=TRUE)
T <- as.numeric(args[1])