HP Fortran Programmer's Guide (B3908-90031; September 2011)

Writing HP-UX applications
Accessing command-line arguments
Chapter 7 175
Accessing command-line arguments
When invoking an HP Fortran executable program, you can include one or more arguments on the command
line. The operating system will make these available to your program. For example, the following
command line invokes the program fprog:
$ fprog arg1 "another arg" 222
and it also passes three character arguments to the program:
arg1
another arg
222
An HP Fortran program can access these arguments for internal use by calling the IGETARG and IARGC
intrinsics; IGETARG is available either as a function or a subroutine. The IGETARG intrinsic gets the
specified command-line argument; IARGC returns the number of arguments on the command line. You can
also use the GETARG intrinsic to return command-line arguments, as illustrated in the following example
program:
Example 7-1 get_args.f90
PROGRAM get_args
INTEGER, PARAMETER :: arg_num = 1
! arg_str is the character array to be written to
! by IGETARG
CHARACTER(LEN=30) :: arg_str
! IGETARG returns number of characters read within
! the specified parameter
! arg_num is the position of the desired argument in the
! the command line (the name by which the program
! was invoked is 0)
! arg_str is the character array in which the argument
! will be written
! 30 is the number of characters to write to arg_str
PRINT *, IGETARG(arg_num, arg_str, 30)
PRINT *, arg_str
! IARGC returns the total number of arguments on the
! command line
PRINT *, IARGC()
END PROGRAM get_args
When compiled and invoked with the following command lines: