Tech Tip: Command Line Arguments in NAGWare f95 Release 5.1 Code.

Question: How to access command line arguments in NAGWare f95 code?

Answer: The compiler provides a wide range of interfaces to Posix functions from built-in modules. In compiler versions up to 5.0 this was done by USEing the appropriate Posix module and calling the GETARG function. This facility is still available, but standard-conforming intrinsic routines to access command line arguments have been added to the Fortran 2003 language, and these have been implemented in Release 5.1.

Here is an example.

  PROGRAM arguments
    INTEGER, PARAMETER :: max_len = 80
    INTEGER arg_len, i, nargs, status
    CHARACTER(LEN=max_len) :: arg
    ! Get number of argumants
    nargs = COMMAND_ARGUMENT_COUNT()
    CALL GET_COMMAND_ARGUMENT(0,arg,arg_len,status)
    ! Get and print the command name
    IF (status==0) PRINT *,'Command = ',arg(:MIN(arg_len,max_len))
    DO i=1,nargs
       ! Get the i-th argument and print it
       CALL GET_COMMAND_ARGUMENT(i,arg,arg_len,status)
       IF (status==0) PRINT *,'Arg',i,'= ',arg(:MIN(arg_len,max_len))
    ENDDO
  END PROGRAM arguments

This is compiled with the -f2003 option to suppress the Extension messages about the Fortran 2003 features and outputs the command name followed by the arguments supplied.
 $ f95 -f2003 cl.f90
 $ ./a.out 1 222 33333
 Command = ./a.out
 Arg 1 = 1
 Arg 2 = 222
 Arg 3 = 33333


For specific technical advice in using NAG's products, please contact our technical experts.

Return to Technical Tips & Hints index page.

Website Feedback

If you would like to see any changes on the current page, feel free to leave us a message.

(If you're a human, don't change the following field)
Your first name.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.