Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.

Command line arguments with fortran

dnoack
Beginner
2,087 Views
With several programming languages one can specify command line arguments when starting up the program, e.g.
program arg1 arg2 ...
Is there also a way in fortran?
Till now, I have used a shell script to set environment variables and start the program and in fortran I get these variables using the function getenv("var",value). But this is cumbersome. On the other hand, if many jobs of the same program run concurrently with different environment variables, the variables can be exchanged.

If this is still not possible with fortran, I think it's a good idea for an enhancement. Since the program units subroutine(...) and function(...) have usually formal parameters, there is no reason why the program statement not also can have formal parameters, e.g. program(arg1,arg2,...).

Regards,
dnoack
0 Kudos
3 Replies
mecej4
Honored Contributor III
2,087 Views
F2003 provides two standard procedures: the command_argument_count() function and the get_command_argument() subroutine, and they are described in the language reference manual.
0 Kudos
IanH
Honored Contributor III
2,087 Views
[fortran]PROGRAM ByYourCommand
  IMPLICIT NONE
  CHARACTER(:), ALLOCATABLE :: full_command_line
  CHARACTER(:), ALLOCATABLE :: a_single_argument
  INTEGER :: l
  INTEGER :: stat
  !****
  CALL GET_COMMAND(LENGTH=l, STATUS=stat)
  IF (stat > 0) THEN
    STOP "Sorry, but I couldn't retrieve the command line"
  END IF
  
  IF (l == 0) THEN
    STOP "Sorry, but I couldn't work out the command length"
  END IF
  
  ALLOCATE(CHARACTER(l) :: full_command_line)
  CALL GET_COMMAND(COMMAND=full_command_line, STATUS=stat)
  IF (stat /= 0) THEN
    STOP 'Sorry, but GET_COMMAND failed unexpectedly'
  END IF
  PRINT "('The command line was:',A)", full_command_line
  
  IF (COMMAND_ARGUMENT_COUNT() == 0) THEN
    STOP "There were no command arguments"
  END IF
  
  CALL GET_COMMAND_ARGUMENT(1, LENGTH=l, STATUS=stat)
  IF (stat > 0) THEN
    STOP "Sorry, but I couldn't retrieve the first command argument"
  END IF
  
  IF (l == 0) THEN
    STOP "Sorry, but I couldn't work out the length of the first argument"
  END IF
  
  ALLOCATE(CHARACTER(l) :: a_single_argument)
  CALL GET_COMMAND_ARGUMENT(1, VALUE=a_single_argument, STATUS=stat)
  IF (stat /= 0) THEN
    STOP 'Sorry, but GET_COMMAND_ARGUMENT failed unexpectedly'
  END IF
  PRINT "('The first argument was:',A)", a_single_argument
  
END PROGRAM ByYourCommand
[/fortran]
0 Kudos
dnoack
Beginner
2,087 Views
Oops! Obviously my knowledge is a little out of date, and my literature too.

Thank you for both answers,
dnoack
0 Kudos
Reply