Command-line arguments: Difference between revisions

From Rosetta Code
Content added Content deleted
(-> Tcl)
m (typo and small category cleanup)
Line 1: Line 1:
{{task}}
{{Programming Task}}


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


==[[Tcl]]==
==[[Tcl]]==
===[[Category: 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:
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:

Revision as of 22:09, 13 February 2007

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)