args
2009-02-05
Introduction
This program prints out the command line arguments that were given to it. It shows how to access command line arguments and demonstrates iteration over all members of a sequence.
Ada
In Ada, the program can be written as follows:
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure Args is
begin
for N in 1..Argument_Count loop
Put_Line(Argument(N));
end loop;
end Args;
The package Ada.Command_Line
provides
access to command line arguments in Ada. It provides two functions:
Argument_Count
, which returns the number of arguments,
and Argument(N)
, which returns the N
th
argument as a String
.
Unlike the other programs on this page, the Ada program above does
not iterate over a sequence of command line arguments. To illustrate
the difference, suppose that Arguments
were an array of
command line arguments. The loop could then be written as follows:
for N in 1..Arguments'Range loop
Put_Line(Arguments(N));
end loop;
The program contains 23 words.
C
This is the C version:
#include <stdio.h>
int main(int argc, char **argv) {
int i;
for(i = 1; i < argc; i++) {
puts(argv[i]);
}
return 0;
}
When a C program is run, main
is called
with two arguments, the first being the number of command line
arguments, and the second being an array of strings, one per command
line argument. Traditionally, the first argument to main
is
called argc
, and the second one argv
.
Array indexing starts at 0
in C, so the first value in
argv
is argv[0]
, and the last value is
argv[argc - 1]
. However, argv[0]
holds the
name of the program, and here we're only interested in the arguments
that were passed to the program, so, indices 1
through
argc - 1
.
We visit each element of argv
by using a
for
loop and an integer variable i
. The
for
has the following syntax:
for(init; test; step) body
It first performs init
and then starts the
actual loop. In each iteration of the loop, it first evaluates
test
. If test returns 0
, it exits the loop.
Otherwise, it performs body
, and then step
.
In case of our program, i
will be set to 1
,
after which it will be checked that i
is less than
argc
. If this is the case, argv[i]
will be
printed, i
will be incremented by one (this is what
i++
means), and the program will loop back to the test.
This continues until i
is no longer less than
argc
, at which point the loop finishes.
The program contains 23 words.
OCaml
In OCaml, we can use the following program:
Array.iter print_endline
(Array.sub Sys.argv 1 ((Array.length Sys.argv) - 1))
Array.iter
iterates over all elements
of an array, and applies a function to each of them. The function is
given as the first argument (in this case: print_endline
),
and the array as the second argument.
In OCaml, command line arguments are provided in
Sys.argv
. As in C, the first argument is the name of the
program. Since we're only interested in the arguments being passed, we
have to get rid of the first argument. For this purpose, we use
Array.sub
, which creates a new array which is a subpart of
the original array. It takes three arguments: the original array, the
position of the first element that will be in the new array, and the
number of arguments. Since arrays are indexed starting from
0
in OCaml, we want all elements starting with
1
. The number of elements we want is equal to the number of
elements in the original array (Array.length Sys.argv
),
minus one.
The program contains 9 words.
Ruby
The Ruby program is very short:
puts ARGV
ARGV
contains the arguments to the program
(but not the name of the program itself). puts
, when given
an array, prints the elements of the array, each followed by a
newline. The program contains 2 words.
It's nice that the program is so short, but it doesn't actually
demonstrate how to do iteration in Ruby. The program below iterates over
the contents of ARGV
, calling puts
for each
one:
ARGV.each { |x| puts x }
ARGV.each
expects to receive a
block, and calls the block for each element in
ARGV
. The block, in this case, takes one parameter,
x
, which it passes to puts
. There are
actually two ways to write blocks in Ruby:
{ |var,…| expr … }
and
do |var,…| expr … end
Typically, the first is used for short blocks, and the second for longer blocks. There is also a difference in precedence between the two forms:
foo bar, baz do … end # block belongs to foo
foo bar, baz { … } # block belongs to baz
This version of the program takes 6 words.
Common Lisp
Common Lisp actually doesn't specify a way to get at command line arguments, probably because Lisp is traditionally used interactively from inside a Lisp environment, rather than by creating and running separate programs. Whatever the reason, the result is that, although most Lisp implementations can be used to produce stand alone programs that use command line arguments, the specifics differ from implementation to implementation. The following program works in SBCL:
(dolist (x (cdr *posix-argv*)) (format t "~A~%" x))
(dolist (var list) form …)
iterates
over list
, binding var
to each successive
element, then evaluating form …
. As SBCL's
*posix-argv*
starts with the name of the program, we will
want to skip the first element. (cdr *posix-argv*)
gives us
the tail of *posix-argv*
, that is, all elements after the
first one.
The program contains 9 words.