Command-line arguments

From Rosetta Code
Revision as of 22:09, 13 February 2007 by rosettacode>Sgeier (typo and small category cleanup)
Task
Command-line arguments
You are encouraged to solve this task according to the task description, using any language you may know.

Retrieve the list of command-line arguments given to the program.

Example command line:

myprogram -c "alpha beta" -h "gamma"

UNIX Shell

sh

To retrieve the entire list of arguments:

WHOLELIST="$@"

To retrieve the second and fifth arguments:

SECOND=$2
FIFTH=$5

Tcl

The pre-defined variable argc contains the number of arguments passed to the routine, argv contains the arguments as a list. Retrieving the second argument might look something like this:

 if { $argc > 1 } { puts [lindex $argv 1] }

(Tcl counts from zero, thus [lindex $list 1] retrieves the second item in the list)