Read Command-Line Arguments

From Rosetta Code
Revision as of 05:25, 2 December 2007 by MikeMol (talk | contribs) (Created task, added C++ and Perl)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Read Command-Line Arguments
You are encouraged to solve this task according to the task description, using any language you may know.

Read the command line arguments, display the total number, and display each one, including any reference to the program itself. (i.e., if the language includes the name and path of the executable as one of the arguments, include it.)

C++

#include <iostream>;
int main( int argc, char argv[] )
{
     std::cout << "There are " << argc << " arguments." << std::endl;
     for (int argnum = 0; argnum < argc; ++argnum)
     {
          std::cout << "Argument #" << argnum << " is " << argv[argnum] << "." << std::endl;
     }

     return 0;  
}


Perl

#!/usr/bin/perl

print "There are $#ARGV arguments.\n";
foreach $argnum ( 0 .. $#ARGV )
{
        print "Argument $argnum is $ARGV[$argnum].\n";
}