Command-line arguments: Difference between revisions

From Rosetta Code
Content added Content deleted
(Ada)
No edit summary
Line 26: Line 26:
end Print_Commands;
end Print_Commands;


==[[UNIX Shell]]==
==[[Perl]]==
[[Category:Perl]]
===[[sh]]===
'''Interpreter:''' [[Perl]] v5.x
To retrieve the entire list of arguments:

WHOLELIST="$@"
To retrieve the second and fifth arguments:
SECOND=$2
FIFTH=$5


==[[Tcl]]==
==[[Tcl]]==
Line 42: Line 39:


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

==[[UNIX Shell]]==
===[[sh]]===
To retrieve the entire list of arguments:
WHOLELIST="$@"
To retrieve the second and fifth arguments:
SECOND=$2
FIFTH=$5

Revision as of 14:21, 14 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"

Ada

Command line arguments are available through the pre-defined package Ada.Command_Line.

with Ada.Command_line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO

procedure Print_Commands is
begin
   -- The number of command line arguments is retrieved from the function Argument_Count
   -- The actual arguments are retrieved from the function Argument
   -- The program name is retrieved from the function Command_Name
   Put(Command_Name & " ");
   for Arg in 1..Argument_Count loop
      Put(Argument(Arg) & " ");
   end loop;
   New_Line;
end Print_Commands;

Perl

Interpreter: Perl v5.x


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)

UNIX Shell

sh

To retrieve the entire list of arguments:

WHOLELIST="$@"

To retrieve the second and fifth arguments:

SECOND=$2
FIFTH=$5