- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
[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]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oops! Obviously my knowledge is a little out of date, and my literature too.
Thank you for both answers,
dnoack
Thank you for both answers,
dnoack
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page