Command-line arguments: Difference between revisions

From Rosetta Code
Content added Content deleted
(Filled out task, added example.)
(-> Tcl)
Line 14: Line 14:
SECOND=$2
SECOND=$2
FIFTH=$5
FIFTH=$5

==[[Tcl]]==
===[[Category: Tcl]]===

The pre-defined variable <tt>argc</tt> contains the number of arguments passed to the routine, <tt>argv</i> 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 <tt>[lindex $list 1]</tt> retrieves the second item in the list)

Revision as of 22:08, 13 February 2007

Template:Programming Task

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)