Command-line arguments: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎[[Java]]: Use Java header instead)
m (Changed over to headers.)
Line 7: Line 7:
myprogram -c "alpha beta" -h "gamma"
myprogram -c "alpha beta" -h "gamma"


==[[Ada]]==
=={{header|Ada}}==
[[Category:Ada]]
Command line arguments are available through the pre-defined package Ada.Command_Line.
Command line arguments are available through the pre-defined package Ada.Command_Line.


Line 26: Line 25:
end Print_Commands;
end Print_Commands;


==[[C]]==
=={{header|C}}==
[[Category: C]]


Command line arguments are passed to main. Since the program name is also passed as "argument", the provided count is actually one more than the number of program arguments. Traditionally the argument count is named argc and the array of argument strings is called argv, but that's not mandatory; any (non-reserved) name will work just as well. It is, however, a good idea to stick to the conventional names.
Command line arguments are passed to main. Since the program name is also passed as "argument", the provided count is actually one more than the number of program arguments. Traditionally the argument count is named argc and the array of argument strings is called argv, but that's not mandatory; any (non-reserved) name will work just as well. It is, however, a good idea to stick to the conventional names.
Line 58: Line 56:
}
}


==[[Clean]]==
=={{header|Clean}}==
[[Category:Clean]]
<tt>getCommandLine</tt> from the module <tt>ArgEnv</tt> returns an array of command-line arguments (the first element is the name of the program).
<tt>getCommandLine</tt> from the module <tt>ArgEnv</tt> returns an array of command-line arguments (the first element is the name of the program).


Line 66: Line 63:
Start = getCommandLine
Start = getCommandLine


==[[E]]==
=={{header|E}}==
[[Category:E]]


interp.getArgs()
interp.getArgs()


==[[Forth]]==
=={{header|Forth}}==
[[Category:Forth]]
Access to command line arguments is not a standard feature of Forth, since it is designed to be used without an operating system. The popular GNU implementation gforth runs from a shell and can access command line arguments similar to C: variable '''argc''' contains the count (including the command itself) and '''arg''' is a function that returns the ''nth'' argument as a string.
Access to command line arguments is not a standard feature of Forth, since it is designed to be used without an operating system. The popular GNU implementation gforth runs from a shell and can access command line arguments similar to C: variable '''argc''' contains the count (including the command itself) and '''arg''' is a function that returns the ''nth'' argument as a string.


Line 92: Line 87:
$
$


==[[Haskell]]==
=={{header|Haskell}}==
[[Category:Haskell]]


Defined by the System module, getArgs :: IO [String] provides the command-line arguments in a list.
Defined by the System module, getArgs :: IO [String] provides the command-line arguments in a list.
Line 120: Line 114:
drop
drop


==[[Perl]]==
={{header|Perl}}==
[[Category:Perl]]
'''Interpreter:''' [[Perl]] v5.x
'''Interpreter:''' [[Perl]] v5.x


Line 130: Line 123:
my $fifth = $ARGV[4];
my $fifth = $ARGV[4];


==[[Pop11]]==
=={{header|Pop11}}==
[[Category:Pop11]]


variable poparglist contains list of command line arguments (as strings).
variable poparglist contains list of command line arguments (as strings).
Line 141: Line 133:
endfor;
endfor;


==[[Python]]==
=={{header|Python}}==
[[Category:Python]]
''sys.argv'' is a list containing all command line arguments, including the program name. Typically you slice the list to access the actual command line argument:
''sys.argv'' is a list containing all command line arguments, including the program name. Typically you slice the list to access the actual command line argument:


Line 152: Line 143:
When running a module by invoking Python, Python process and remove some of the arguments, and the module can not access them. To process command line arguments, run the module directly. sys.argv is a copy of the command line arguments; modifying sys.argv will not change the arguments seen by other processes, e.g. ps.
When running a module by invoking Python, Python process and remove some of the arguments, and the module can not access them. To process command line arguments, run the module directly. sys.argv is a copy of the command line arguments; modifying sys.argv will not change the arguments seen by other processes, e.g. ps.


==[[Raven]]==
=={{header|Raven}}==
[[Category:Raven]]


ARGS print
ARGS print
Line 165: Line 155:
5 => "gamma"
5 => "gamma"


==[[Ruby]]==
=={{header|Ruby}}==
[[Category:Ruby]]
Command line arguments are available in the constant Object::ARGV.
Command line arguments are available in the constant Object::ARGV.


Line 176: Line 165:
=> ["a","-h","b","c"]
=> ["a","-h","b","c"]


==[[Tcl]]==
=={{header|Tcl}}==
[[Category: Tcl]]


The pre-defined variable <tt>argc</tt> contains the number of arguments passed to the routine, <tt>argv</tt> 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</tt> contains the arguments as a list. Retrieving the second argument might look something like this:
Line 185: Line 173:
(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)


==[[Toka]]==
=={{header|Toka}}==
[[Category:Toka]]


Arguments are stored into an array. The first element in the array
Arguments are stored into an array. The first element in the array
Line 196: Line 183:
1 #args [ i #= show-arg ] countedLoop
1 #args [ i #= show-arg ] countedLoop


==[[UNIX Shell]]==
=={{header|UNIX Shell}}==
===[[Bourne Shell]]===
===[[Bourne Shell]]===
To retrieve the entire list of arguments:
To retrieve the entire list of arguments:

Revision as of 17:29, 13 November 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;

C

Command line arguments are passed to main. Since the program name is also passed as "argument", the provided count is actually one more than the number of program arguments. Traditionally the argument count is named argc and the array of argument strings is called argv, but that's not mandatory; any (non-reserved) name will work just as well. It is, however, a good idea to stick to the conventional names.

Be careful on systems that use Unicode or other multibyte character sets. You may need to use a type of _wchar* and multi-byte-character-set-aware versions of printf.

 #include <stdio.h>
 
 int main(int argc, char* argv[])
 {
   int i;
   printf("This program is named %s.\n", argv[0]);
   for (i = 1; i < argc; ++i)
     printf("the argument #%d is %s\n", i, argv[i]);
 }

C++

Command line arguments are passed the same way as in C.

This example uses iostream. Traditional C I/O also works.

#include <iostream>

int main(int argc, char* argv[])
{
  std::cout << "This program is named " << argv[0] << "\n";
  std::cout << "There are " << argc-1 << " arguments given.\n";
  for (int i = 1; i < argc; ++i)
    std::cout << "the argument #" << i << " is " << argv[i] << "\n";
}

Clean

getCommandLine from the module ArgEnv returns an array of command-line arguments (the first element is the name of the program).

import ArgEnv

Start = getCommandLine

E

interp.getArgs()

Forth

Access to command line arguments is not a standard feature of Forth, since it is designed to be used without an operating system. The popular GNU implementation gforth runs from a shell and can access command line arguments similar to C: variable argc contains the count (including the command itself) and arg is a function that returns the nth argument as a string.

Interpreter: gforth 0.6.2

\ args.f: print each command line argument on a separate line
: main
  argc @ 0 do i arg type cr loop ;

main bye

Here is output from a sample run.

$ gforth args.f alpha "beta gamma" delta
gforth
args.f
alpha
beta gamma
delta
$

Haskell

Defined by the System module, getArgs :: IO [String] provides the command-line arguments in a list.

myprog.hs:

import System
main = getArgs >>= print
myprog a -h b c
=> ["a","-h","b","c"]

Java

public class Arguments {
  public static void main(String[] args) {
     System.out.println("There are " + args.length + " arguments given.");
     for(int i = 0; i < args.length; i++) 
        System.out.println("The argument #" + (i+1) + " is " + args[i] + "and is at index " + i);
  }
}

LSE64

argc , nl  # number of arguments (including command itself)
0         # argument
dup arg dup 0 = || ,t  1 + repeat
drop

Perl=

Interpreter: Perl v5.x

@ARGV is the array containing all command line parameters

my @params = @ARGV;
my $second = $ARGV[1];
my $fifth = $ARGV[4];

Pop11

variable poparglist contains list of command line arguments (as strings). One can use iteration over list to process then (for example print).

lvars arg;
for arg in poparglist do
   printf(arg, '->%s<-\n');
endfor;

Python

sys.argv is a list containing all command line arguments, including the program name. Typically you slice the list to access the actual command line argument:

import sys
program_name = sys.argv[0]
arguments = sys.argv[1:]
count = len(arguments)

When running a module by invoking Python, Python process and remove some of the arguments, and the module can not access them. To process command line arguments, run the module directly. sys.argv is a copy of the command line arguments; modifying sys.argv will not change the arguments seen by other processes, e.g. ps.

Raven

ARGS print
stack (6 items)
 0 => "raven"
 1 => "myprogram"
 2 => "-c"
 3 => "alpha beta"
 4 => "-h"
 5 => "gamma"

Ruby

Command line arguments are available in the constant Object::ARGV.

myprog:

 #! /usr/bin/env ruby
 p ARGV
 myprog a -h b c
 => ["a","-h","b","c"]

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)

Toka

Arguments are stored into an array. The first element in the array is the name of the program, the rest are the arguments in order. The number of arguments is provided by #args.

 [ arglist array.get type cr ] is show-arg
 [ dup . char: = emit space ] is #= 
 1 #args [ i #= show-arg ] countedLoop

UNIX Shell

Bourne Shell

To retrieve the entire list of arguments:

WHOLELIST="$@"

To retrieve the second and fifth arguments:

SECOND=$2
FIFTH=$5